score:7
Since you have two points (coordinates), the task is simple:
For the x
and y
position, use the first point. For the width
and height
of the rectangle, just calculate x2 - x1
and y2 - y1
, respectively.
Here is a demo with three rectangles. In the data array, each object has 4 properties, corresponding to the two points (this is just an example, you may change the code according to your data structure):
var svg = d3.select("body")
.append("svg")
.attr("width", 400)
.attr("height", 200);
var data = [{x1: 20, x2: 60, y1: 30, y2: 50},
{x1: 50, x2: 80, y1: 100, y2: 150},
{x1: 200, x2: 400, y1: 10, y2: 100}];
var rects = svg.selectAll("foo")
.data(data)
.enter()
.append("rect")
.attr("x", d=> d.x1)
.attr("y", d=> d.y1)
.attr("width", d=> d.x2 - d.x1)
.attr("height", d=> d.y2 - d.y1)
.attr("fill", "teal");
<script src="https://d3js.org/d3.v4.min.js"></script>
Here is the same principle, but with the data containing two arrays, the first array being the first point and the second array being the second point:
var svg = d3.select("body")
.append("svg")
.attr("width", 400)
.attr("height", 200);
var data = [[[20,30],[40,50]], [[50,100],[80,150]], [[200,30],[400,100]]];
var rects = svg.selectAll("foo")
.data(data)
.enter()
.append("rect")
.attr("x", d=> d[0][0])
.attr("y", d=> d[0][1])
.attr("width", d=> d[1][0] - d[0][0])
.attr("height", d=> d[1][1] - d[0][1])
.attr("fill", "teal");
<script src="https://d3js.org/d3.v4.min.js"></script>
For both the above snippets to work, the data has to have the coordinates in this sequence: first the top-left point, then the bottom-right point. Of course, you can write a function to check this and swap the points if the order is not correct.
For drawing a diagonal, just choose the points you want. For instance, from the top-left to the bottom-right:
var svg = d3.select("body")
.append("svg")
.attr("width", 400)
.attr("height", 200);
var data = [{x1: 20, x2: 60, y1: 30, y2: 50},
{x1: 50, x2: 80, y1: 100, y2: 150},
{x1: 200, x2: 400, y1: 10, y2: 100}];
var rects = svg.selectAll("foo")
.data(data)
.enter()
.append("rect")
.attr("x", d=> d.x1)
.attr("y", d=> d.y1)
.attr("width", d=> d.x2 - d.x1)
.attr("height", d=> d.y2 - d.y1)
.attr("fill", "teal")
.attr("opacity", 0.3);
var lines = svg.selectAll("foo")
.data(data)
.enter()
.append("line")
.attr("x1", d=> d.x1)
.attr("y1", d=> d.y1)
.attr("x2", d=> d.x2)
.attr("y2", d=> d.y2)
.attr("stroke", "firebrick")
.attr("stroke-width", 2);
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- How to draw a rectangle in D3.js with only 2 coordinates
- How to draw arc with d3.js with only outer radius, or sector of circle?
- How to draw a rectangle with d3.js based on 2 diametrical points and no length or height values?
- How to draw line with arrow using d3.js
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to draw a *simple* line segment with d3.js?
- How to draw logarithmic line charts with nvd3
- d3.js visibility-zone calculations or how to draw geo rectangle
- How to draw circles around circular path with D3js
- how do you draw linear line in scatter plot with d3.js
- How to draw circles with radii given in kilometers accurately on world map
- How can I create a doughnut chart with rounded edges only on one end of each segment?
- How can I programmatically draw a nonlinear writing system with automatic graph relaxation (e.g. in d3.js)?
- Draw a map with D3.js: How to get the right scale() and translate()?
- D3 how to draw chart with a x data between two y data
- How to draw a scatterplot with superimposed pics?
- how to draw nvd3 simple barchart with x and y axis in angularjs
- How to draw an ellipse from degree and coordinates
- How to draw D3 Map with United States, Canada, Mexico and Puerto Rico
- In D3.js How to draw a map with full screen width?
- How can I draw an arc with array of points in d3 v4
- How to draw line charts with complex data structures in d3
- How to draw single Pixel lines with d3.js / SVG
- How to draw dots in the coordinates of slices in a d3 pie chart?
- How to Draw rectangles at 0,45,90,135,180 degree so on around an SVG Circle with D3.js
- How to draw rounded corner rectangle in React Native ART
- How to draw line automatically by reading coordinates from file?
- How to draw spiral bubble charts with d3.js
- How to align graph with respective rectangle node size in d3 js
- How to draw a diamond with the path "d" attribute?
More Query from same tag
- Create D3 Bar Chart from array of timestamps
- Adding an <a> tag before the primary data element in the enter() section of a selection
- In D3.JS, where is the data located after d3.selection.data(myData)?
- simple, exasperating d3.js example
- Assign different colours to vertical lines in a line chart based on property value
- How to create a tooltip for multiple points in a scatterplot with d3
- Pre-built directives for D3 in AngularJS
- Colons in field names messes up d3 code
- d3.select on inkscape:label="foo"
- D3 JS select multiple countries for highlighting issue
- Compose two rotations in D3 geo projection?
- D3 chart is duplicating on React.js component
- Can't get first array element to print
- D3 - legend won't visualize
- draw a rectangle with custom texts around an image in d3 dendrogram
- D3 - Identifying element by data
- Multi-series chart - time (year) interval x-axis overlaying multiple years of data
- Where is the difference between addEventListener (classic dom svg-elment) and .on (at d3js)
- How to use enter (data join) for d3 line graphs
- Compare data element with partial mean in d3
- D3.js SVG on OpenLayers3 interactive map
- d3: only two "on" possible?
- X-axis labels are not properly displaying in d3
- D3: How to add a margin between min/max from data and min/max from axis?
- width attribute not working with d3 style
- Why the graph lines are off axis Y and X?
- Unable to get the Zoom Effect in D3.js
- Horizontal pan with ordinal x axis in d3
- How to overwrite the array inside a function?
- Display text on a pie in Pie Charts using d3 at two places