D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
samirgambhir
Full window
Github gist
Module 4 exercise - Global refugee data chart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Global refugee data</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Book Antiqua; font-weight: bold; margin-left: 50px; } h1 { font-family: Book Antiqua; font-size: 24px; margin: 0; color: #323232; } p { font-family: Book Antiqua; font-size: 14px; color: #323232; margin: 10px 0 0 0; } svg { background-color: #323232; } rect:hover { fill: #ff9900; } .axis path, .axis line { fill:none; stroke: white; shape-rendering: crispEdges; } .axis text { font-family: Book Antiqua; fill: white; font-size: 10px; } .y.axis path, .y.axis line { opacity:0; } </style> </head> <body> <h1>Global Refugee Data Chart</h1> <p>Average annual refugees (2009-2013) per GDP at PPP 2013 for selected countries. Source: <a href="https://popstats.unhcr.org/#_ga=1.152203577.1033056182.1429126874">UNHCR</a> and <a href="https://data.worldbank.org/indicator/NY.GDP.PCAP.PP.CD">World Bank</a></p><br/> <script type="text/javascript"> var w=900; var h=750; var padding = [ 20, 10, 30, 175 ]; //Top, right, bottom, left var wScale = d3.scale.linear() .range ([0, w-padding[1]-padding[3]]); var hScale=d3.scale.ordinal() .rangeRoundBands([padding[0], h-padding[2]], 0.1); var xAxis = d3.svg.axis() .scale(wScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(hScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("RefugeeData.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.ref_p_gdp, +b.ref_p_gdp); }); wScale.domain([0,d3.max(data, function(d) { if (d.ref_p_gdp>10){ return +d.ref_p_gdp; } }) ]); hScale.domain(data.map(function(d) { if (d.ref_p_gdp>10){ return d.country;} })); var rects = svg.selectAll("rect").data(data).enter().append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { if (d.ref_p_gdp>10){ return hScale(d.country); } }) .attr("width", function(d) { if (d.ref_p_gdp>10){ return wScale(d.ref_p_gdp); } }) .attr("height", hScale.rangeBand()) .attr("fill","#ffc04c"); 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] + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js