D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ElaineYu
Full window
Github gist
Metis Simple Stacked Bar Chart with Labels
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;position:fixed;top:0;right:0;bottom:0;left:0; } .bartext { font-size: 12px; } </style> </head> <body> <svg width="960" height="500"></svg> <script> var margin = {top: 10, right: 20, bottom: 20, left: 60}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var y = d3.scaleLinear() .rangeRound([height, 0]); var x = d3.scaleBand() .rangeRound([0, width]) .paddingInner(0.05) .align(0.1); var g = d3.select("svg") .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("us-unemployment-2017.csv", function(data) { console.log('Original data', data); data.sort(function(a, b) { return b.Rate - a.Rate; }); x.domain(data.map(function(d) { return d.StateCode; })); y.domain([0, d3.max(data, function(d) { return d.Rate; })]).nice(); g.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .attr("class", "axis") .call(d3.axisLeft(y).ticks(null, "s")) var bars = g.selectAll(".bar") .data(data) // exit bars.exit().remove(); // enter // Add things that don't depend on data change var enter = bars.enter().append('g'); enter.append("rect") // inherit the data .attr("class", "bar") .attr("x", function(d) { return x(d.StateCode); }) .attr("y", function(d) { return y(d.Rate); }) .attr("width", x.bandwidth()) // width of the rectangle .attr("height", function(d) { return height - y(d.Rate); }) .attr('fill', 'blue') enter.append('text') // bound on the parent .attr("class", "bartext") // enter + update // What's left on the screen once you've removed the extraneous. // Merge what is displayed -- what's added and what's remaining. // Add things that should be re-rendered with data change. bars = enter.merge(bars) bars.select('text') // include x and y values to position the text .attr('x', function(d) { return x(d.StateCode)}) .attr('y', function(d) { return y(d.Rate)} ) .text(function(d){ return d.Rate}) // Grouping with D3 nest var groupedByRegion = d3.nest().key(function(d) {return d.Region}).entries(data); console.log('groupedByRegion', groupedByRegion); var groupedBySubRegion = d3.nest().key(function(d) {return d.Subregion}).entries(data); var groupedByState = d3.nest().key(function(d) {return d.StateName}).entries(data); console.log('groupedByState', groupedByState) // Statistics var averageUnemploymentByRegion = d3.nest().key(function(d) {return d.Region}).rollup(function(v) { return d3.mean(v, function(d) { return parseInt(d.Rate);}); }).entries(data); var averageUnemploymentbySubregion = d3.nest().key(function(d) {return d.Subregion}).rollup(function(v) { return d3.mean(v, function(d) { return parseInt(d.Rate);}); }).entries(data); console.log('averageUnemploymentByRegion', averageUnemploymentByRegion); console.log('averageUnemploymentbySubregion', averageUnemploymentbySubregion); }); </script> </body>
https://d3js.org/d3.v4.min.js