D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
piggysmacks
Full window
Github gist
2013 RRSP Contributions by Province/Territory
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>RRSP Contributions</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } rect:hover { fill: lightgreen; } body { background-color: white; } svg { background-color: white; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>Canadian Retirement Savings</h1> <p><b>Median RRSP Contributions by Province/Territory 2013, Source: <a href="https://open.canada.ca/data/en/dataset/5d866493-6a11-443f-8ea4-5289289cb3b3I">Government of Canada</a></b></p> <script type="text/javascript"> var w = 550; var h = 375; var padding = [ 20, 10, 20, 155 ]; //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"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("RRSPData.csv", function(data) { data.sort(function(a, b) { //return d3.ascending(a.year, b.year); if(a.year > b.year){ return 1; } if(a.year < b.year){ return -1; } if(+a.medianRRSPContributionsDollars < +b.medianRRSPContributionsDollars){ return 1; } if(+a.medianRRSPContributionsDollars > +b.medianRRSPContributionsDollars){ return -1; } return 0; }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.medianRRSPContributionsDollars; }) ]); heightScale.domain(data.map(function(d) { return d.area; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); <!--heightScale.domain(data.map(function(d) { return d.area; } ));--> rects.attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(d.area); }) .attr("width", function(d) { return widthScale(d.medianRRSPContributionsDollars); }) .attr("height", heightScale.rangeBand()) .attr("fill", "green") .append("title") .text(function(d) { return d.area + "'s median RRSP Contribution for " + d.year + " is $"+ d.medianRRSPContributionsDollars; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 5) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js