D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tomshanley
Full window
Github gist
Caramel slice prototype
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> var colour = {}; colour.chocolate = "#8B4513" // CSS SaddleBrown colour.caramel = "#DAA520" // CSS GoldenRod colour.base = "#A0522D" // CSS Sienna var data = [ {"id": 1, "base": 10, "caramel": 50, "chocolate": 5, "location": "Cafe X"}, {"id": 2, "base": 15, "caramel": 45, "chocolate": 2, "location": "Cafe y"}, ]; data.forEach(function(d){ drawSlice(d.base, d.caramel, d.chocolate); }); function drawSlice(base, caramel, chocolate) { const margin = {"top": 10, "left": 10, "right": 10, "bottom": 10}; const width = 100; const height = width; var csHeight = base + caramel + chocolate; var csWidth = caramel; var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) var g = svg.append("g") .attr("transform", "translate(" + margin.left +"," + margin.top + ")") var caramelSlice = g.append("g") .attr("class", "caramel-slice") .attr("transform", "translate(" + ((width - csWidth)/2) +"," + ((height - csHeight)/2) + ")"); caramelSlice.append("rect") .attr("class", "chocolate-layer") .attr("x", 0) .attr("y", 0) .attr("width", csWidth) .attr("height", chocolate) .style("fill", colour.chocolate); caramelSlice.append("rect") .attr("class", "caramel-layer") .attr("x", 0) .attr("y", chocolate) .attr("width", csWidth) .attr("height", caramel) .style("fill", colour.caramel); caramelSlice.append("rect") .attr("class", "base-layer") .attr("x", 0) .attr("y", chocolate + caramel) .attr("width", csWidth) .attr("height", base) .style("fill", colour.base); }; </script> </body>
https://d3js.org/d3.v4.min.js