D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
cmulbrandon
Full window
Github gist
Value of Agricultural Land: 1770–1810
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Capital</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> <style type="text/css"> body { margin: 0; background-color: white); font-family: Helvetica, Arial, sans-serif; } #container { width: 500px; margin-left: auto; margin-right: auto; margin-top: 30px; padding: 50px; background-color: white; box-shadow: 2px 2px 7px 3px #E5E5E5; } h1 { font-size: 26px; margin: 0; } p { font-size: 14px; margin: 15px 0 10px 0; } p.title { font-size: 14px; margin: 5px 0 10px 0; } a:link { text-decoration: none; color: gray; } a:hover { color: steelBlue; text-decoration: underline; } a:visited { color: gray; } a:active { color: steelBlue; } svg { background-color: white; } g.bar text { font-size: 30px; font-weight: bolder; text-anchor: end; opacity: 1; fill: white; } g.bar:hover rect { fill: darkorange; } g.bar:hover text { } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 13px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <div id="container"> <h1>Value of Agricultural Land: 1770–1810</h1> <p class="title">as a percentage of each country's annual national income</p> <div id="chart"></div> <p><strong>Source:</strong> <a href="https://piketty.pse.ens.fr/en/capital21c2"><i>Capital in the 21st century</i></a></p> </div> <script type="text/javascript"> var w = 500; var h = 450; var padding = [ 20, 10, 70, 150 ]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); //Now SVG goes into #chart instead of body var svg = d3.select("#chart") .append("svg") .attr("width", w) .attr("height", h); d3.csv("capitaloldvsnewworld.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.nationalWealth, +b.nationalWealth); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.nationalWealth; }) ]); heightScale.domain(data.map(function(d) { return d.country; } )); //Bind data to groups (not bars directly) var groups = svg.selectAll("g") .data(data) .enter() .append("g") .attr("class", "bar"); //Add a rect to each group var rects = groups.append("rect") .attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.country); }) .attr("width", 0) .attr("height", heightScale.rangeBand()) .attr("fill", "#042029"); //Add a text element to each group groups.append("text") .attr("x", function(d) { return padding[3] + widthScale(d.nationalWealth) - 5; }) .attr("y", function(d) { return heightScale(d.country) + 45; }) .text(function(d) { return d.nationalWealth + "%"; }); //Change color of United States (South) and United States (North) rects.filter(function(d) { return d.nationalWealth < 600 }) .attr("fill", "gainsboro"); rects.transition() .delay(function(d, i) { return i * 70; }) .duration(1000) .attr("width", function(d) { return widthScale(d.nationalWealth); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis) .append("text") .attr("x", w/2-10) .attr("y", 40) .attr("dy", ".1em") .style("text-anchor", "end") .text("% of National Income"); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js