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": 20, "width":30, "color" : "green", "name" : "Green" },
{ "x_axis": 40, "y_axis": 40, "height": 30, "width":60, "color" : "purple", "name" : "Purple" },
{ "x_axis": 133, "y_axis": 79, "height": 40, "width":40, "color" : "red", "name" : "Red" },
{ "x_axis": 195, "y_axis": 24, "height": 30, "width":20, "color" : "lightblue", "name" : "Light 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; });
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; })
.attr("y", function (d) { return d.y_axis + d.height; })
.attr("font-size", 16)
.attr("font-family", "monospace");
</script>
</body>
https://d3js.org/d3.v4.min.js