D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
EmbraceLife
Full window
Github gist
28. stacked bar (states on x-axis, age groups stacked)
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .bar { fill: steelblue; } .axis path { display: none; } </style> <svg width="960" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 40}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x = d3.scaleBand() .rangeRound([0, width]) .padding(0.1) .align(0.1);// check whether chart is displaying var y = d3.scaleLinear() .rangeRound([height, 0]); // ---- set 7 custom colors to scale-map data input // var color = d3.scaleOrdinal(d3.schemeCategory10); var z = d3.scaleOrdinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var stack = d3.stack(); d3.csv("data.csv", type, function(error, data) { // -test at bottom if (error) throw error; // -test- // console.log(data.map(function(d){return d.total})); data.sort(function(a, b) { return b.total - a.total; });// -test // --- test: sort() upon data using d.total // console.log(data.map(function(d){return d.total})); // return of map() and args of d3.max() x.domain(data.map(function(d) { return d.State; })); y.domain([0, d3.max(data, function(d) { return d.total; })]).nice(); // - test: nicely rounded // console.log(y.domain()); z.domain(data.columns.slice(1)); // ---- use of .slice(1) --- // console.log(data.columns.slice(1)); // console.log(data.columns); // ---- test: how to stack.keys()(data) // console.log(stack.keys(data.columns.slice(1))(data)); // console.log(stack.keys(data.columns.slice(1))(data).map(function(d){return d.key})); // ToDo: x-axis not state by age group g.selectAll(".serie") .data(stack.keys(data.columns.slice(1))(data)) // -test .enter().append("g") .attr("class", "serie") .attr("fill", function(d) { return z(d.key); }) .selectAll("rect") .data(function(d) { return d; }) .enter().append("rect") .attr("x", function(d) { return x(d.data.State); }) .attr("y", function(d) { return y(d[1]); }) //---- get logic clear: higher value from d[0] smaller px from y(d[0]) .attr("height", function(d) { return y(d[0]) - y(d[1]); }) // y(d[0])-y(d[1]) is always positive .attr("width", x.bandwidth()); // --- test ----- // console.log(stack.keys(data.columns.slice(1))(data)[1][0]); // var a1 = stack.keys(data.columns.slice(1))(data)[0][0]; // console.log(y(a1[0]) - y(a1[1])); // console.log(stack.keys(data.columns.slice(1))(data)[1][0]); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // --- test: y.ticks(10) --- // console.log(y.ticks(10)); // console.log(y.ticks(10).pop()); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y).ticks(10, "s")) .append("text") .attr("x", 2) .attr("y", y(y.ticks(10).pop()))// -test .attr("dy", "0.35em") .attr("text-anchor", "start") .attr("fill", "#000") .text("Population"); // -- test: -- // console.log(data.columns.slice(1).reverse()); var legend = g.selectAll(".legend") .data(data.columns.slice(1).reverse())// -test .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }) .style("font", "10px sans-serif"); legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .attr("fill", z); // -understand logic legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .attr("text-anchor", "end") .text(function(d) { return d; }); // ----- test: object["colname"] is working ----- // console.log(data); // console.log(data.columns); // // console.log(data[0].columns); // not working // var colnames = data.columns; // console.log(data[0]); // console.log(data[0].State); // console.log(data[0][colnames[0]]); // console.log(data[0][data.columns[1]]); }); // ---- columns acts as data.columns function type(d, i, columns) { for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]]; d.total = t; return d; } </script>
https://d3js.org/d3.v4.min.js