D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ElaineYu
Full window
Github gist
Metis Unemployment Rate Bar Chart Nesting and Labels Example
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 { fill: white; font: 20px Helvetica, Verdana, Tahoma; } </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 + ")"); g.append("text") .attr("x", 125) .attr("y", 20) .style("font-size", "25px") .style("text-decoration", "underline") .text("Average United States Unemployment Rate (%) 2017 by Subregion") d3.csv("us-unemployment-2017.csv", function(data) { console.log('Original data', 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); averageUnemploymentbySubregion.sort(function(a, b) { return b.value - a.value; }); x.domain(averageUnemploymentbySubregion.map(function(d) { return d.key; })); y.domain([0, d3.max(averageUnemploymentbySubregion, function(d) { return d.value; })]) g.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // Append y-axis (optional) // g.append("g") // .attr("class", "axis") // .call(d3.axisLeft(y).ticks(null, "s")) var bars = g.selectAll(".bar") .data(averageUnemploymentbySubregion) var enter = bars.enter().append('g') enter.append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.key); }) .attr("y", function(d) { return y(d.value) }) .attr("width", x.bandwidth()) .attr("height", function(d) { return height - y(d.value); }) .attr('fill', 'steelblue') 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.key) + data.length/2}) .attr('y', function(d) { return y(d.value) + 25} ) .text(function(d){ return d.value.toFixed(1)}) // 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