Built with blockbuilder.org
forked from Igathi's block: Rectangles using data
forked from Igathi's block: Rectangles using data Color
forked from Igathi's block: Rectangles data Color Jason
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
var jsonRectangles = [
{ "x_axis": 10, "y_axis": 10, "height": 34, "width":43, "color" : "rgb(0, 254, 0)", "name" : "Green" },
{ "x_axis": 10, "y_axis": 90, "height": 53, "width":60, "color" : "rgb(255, 255, 1)", "name" : "Yellow" },
{ "x_axis": 5, "y_axis": 176, "height": 88, "width":94, "color" : "#01FFFD", "name" : "Bright Blue" },
];
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var rectangles = svg.selectAll("rect")
.data(jsonRectangles)
.enter()
.append("rect");
// rectangles.attr("x", function (d) { return d.x_axis; }) will also work
// var rectangleAttributes =
rectangles
.attr("x", function (d) { return d.x_axis; })
.attr("y", function (d) { return d.y_axis; })
.attr("height", function (d) { return d.height; })
.attr("width", function (d) { return d.width; })
.style("fill", function(d) { return d.color; })
.style("stroke", "black")
.style("stroke-width",1);
var texts = svg.selectAll("text")
.data(jsonRectangles)
.enter()
.append("text");
var textsAttributes = texts
.text(function (d) { return d.name; })
.attr("x", function (d) { return (d.x_axis + d.width + 5)/2; })
.attr("y", function (d) { return d.y_axis + (d.width/2); })
.attr("font-size", 18)
.attr("font-family", "monospace");
</script>
</body>
https://d3js.org/d3.v4.min.js