D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
piggysmacks
Full window
Github gist
RRSP Median Contributions by Province - 2013
<!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"> body { background-color: white; } svg { background-color: white; } </style> </head> <body> <p> <b> Median RRSP Contributions by Province/Territory 2013 </b> </p> <script type="text/javascript"> var provinces = ['Alberta','British Columbia','Saskatchewan','Manitoba','Ontario','Quebec','New Brunswick','Newfoundland and Labrador','Northwest Territories','Nova Scotia','Nunavut','Prince Edward Island','Yukon']; var provincesLength = provinces.length; var svg = d3.select("body") .append("svg") .attr("width", 300) .attr("height", 400); 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.area == 'Canada'){ return 1; } if(+a.medianRRSPContributionsDollars < +b.medianRRSPContributionsDollars){ return 1; } if(+a.medianRRSPContributionsDollars > +b.medianRRSPContributionsDollars){ return -1; } return 0; //If your numeric values aren't sorting properly, //try commenting out the line above, and instead using: // //return d3.descending(+a.lifeSatisfaction, +b.lifeSatisfaction); // //Data coming in from the CSV is saved as strings (text), //so the + signs here force JavaScript to treat those //strings instead as numeric values, thereby fixing the //sort order (hopefully!). }); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect").filter(function(d) { //return false; //return d.area == 'Canada' for (var i = 0; i < provincesLength; i++) { if(d.area == provinces[i] && d.year == 2013){ return true; } } return false; }); rects.attr("x", 0) .attr("y", function(d, i) { return i * 15; }) .attr("width", function(d) { return d.medianRRSPContributionsDollars / 16; }) .attr("height", 12) .attr("fill", "green") .append("title") .text(function(d) { return d.area + "'s median RRSP Contribution for " + d.year + " is $"+ d.medianRRSPContributionsDollars; }); var areas = svg.selectAll("text") .data(data) .enter() .append("text") .filter(function(d) { //return false; //return d.area == 'Canada' for (var i = 0; i < provincesLength; i++) { if(d.area == provinces[i] && d.year == 2013){ return true; } } return false; }); areas.attr("x", 0) .attr("y", function(d,i) { return i * 15 + 9; }) .attr("fill", "#FFFFFF") .attr("font-size", "9px") .attr("font-weight", "bold") .attr("font-family", "sans-serif") .text(function(d) { return d.area}); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js