D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lmelgar
Full window
Github gist
Week 9: "Grouped Bars."
<!DOCTYPE html> <meta charst="utf-8"> <html> <style> body { font: 12px sans-serif; padding: 50px; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .bar { fill: steelblue; } .x.axis path { display: none; } </style> <body> <h2>Deaths from Illnesses in 2013 in Latin America and Caribbean countries</h2> <p><a href="https://apps.who.int/gho/data/node.main.ghe100-by-cause?lang=en">WHO data</a>, deaths of children 0-4 years old in Latin America and Caribbean countries.</p> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> <script> var margin = {top: 20, right: 200, bottom: 90, left: 30}; var width = 1050 - margin.left - margin.right; var height = 600 - margin.top - margin.bottom; var x0 = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var x1 = d3.scale.ordinal(); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.ordinal() .range(["#8C703A", "#ABACDB", "#B574B5", "#DBABC4", "#DBBFAB", "#ACD7E3", "#E6E687", "#FA9D91", "#D6C789", "#72A886", "#8A5F76", "#92A147", "#E3E2AF", "#A5BBD1"]); var xAxis = d3.svg.axis() .scale(x0) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(d3.format(".2s")); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("diarrhoeal.csv", function(error, data) { if (error) { console.log(error); } data2013 = []; data.forEach(function (d) { if(d.year === "2013" && d.region === "South Asia") { data2013.push(d); } }); console.log(data2013); var illnessNames = d3.keys(data2013[0]).filter(function(key) { return key !== "country" && key !== "region" && key !== "year"; }); console.log(illnessNames); /*data2013.sort(function(a, b) { return a.country - b.country; });*/ data2013.forEach(function(d) { d.illnesses = illnessNames.map(function(name) { return { name: name, value: +d[name] }; }); }); x0.domain(data2013.map(function(d) { return d.country; })); x1.domain(illnessNames).rangeRoundBands([0, x0.rangeBand()]); y.domain([0, d3.max(data2013, function(d) { return d3.max(d.illnesses, function(d) { return d.value; }); })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll("text") .attr("dy", ".5em") .attr("transform", "rotate(-30)") .style("text-anchor", "end"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Deaths"); var state = svg.selectAll(".country") .data(data2013) .enter().append("g") .attr("class", "g") .attr("transform", function(d) { return "translate(" + x0(d.country) + ",0)"; }); state.selectAll("rect") .data(function(d) { return d.illnesses; }) .enter().append("rect") .attr("width", x1.rangeBand()) .attr("x", function(d) { return x1(d.name); }) .attr("y", function(d) { return y(d.value); }) .attr("height", function(d) { return height - y(d.value); }) .style("fill", function(d) { return color(d.name); }); drawLegend(); function drawLegend() { var legend = svg.selectAll(".legend") .data(color.domain().slice().reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d,i){ return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width + 15) .attr("width", 15) .attr("height", 15) .style("fill", color); legend.append("text") .attr("x", width + 44) .attr("y", 6) .attr("dy", ".5em") .style("text-anchor", "start") .text(function(d, i) { return d; }); } /*var legend = svg.selectAll(".legend") .data(illnessNames.slice().reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 30 + ")"; }); legend.append("rect") .attr("x", width - 18) .attr("width", 15) .attr("height", 15) .style("fill", color); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d; });*/ }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js