Built with blockbuilder.org
forked from Igathi's block: Rectangles using data
forked from Igathi's block: Rectangles using data Color
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" : "rgb(0, 254, 0)", "name" : "Green" },
{ "x_axis": 10, "y_axis": 40, "height": 30, "width":60, "color" : "rgb(255, 255, 1)", "name" : "Yellow" },
{ "x_axis": 5, "y_axis": 79, "height": 40, "width":40, "color" : "#01FFFD", "name" : "Bright Blue" },
{ "x_axis": 195, "y_axis": 3, "height": 30, "width":20, "color" : "#FE9900", "name" : "Orange" },
{ "x_axis": 63, "y_axis": 129, "height": 40, "width":40, "color" : "#329866", "name" : "Weird Green" },
{ "x_axis": 195, "y_axis": 46, "height": 30, "width":20, "color" : "#FF7C82", "name" : "Peachy Pink" },
{ "x_axis": 195, "y_axis": 85, "height": 30, "width":20, "color" : "#00CC67", "name" : "Medium Green" }
];
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
// Use of 'g' to group - only one data enter with group
// Note the object is in JSON format - attributes and style should use "", ""
// js uses '' : ''
var mainGroup = svg.append('g');
var rectGroup = mainGroup.selectAll('g')
.data(jsonRectangles)
.enter()
.append('g');
rectGroup.append('rect')
.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);
rectGroup.append('text')
.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", 18)
.attr("font-family", "monospace");
/*
barGroup.append('text')
.text(function(d) { return d; })
.style('text-anchor', 'start')
.attr({
dx: 10,
dy: -10,
transform: 'rotate(90)',
fill: 'white'
});
*/
</script>
</body>
https://d3js.org/d3.v4.min.js