D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jenchristiansen
Full window
Github gist
Exercise4: d3 class, WEF Global Gender Gap data
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Global Gender Gap bart chart test, style</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 20px; margin: 0; color: orange; } p { font-size: 12px; margin: 20px 20 0 0; } svg { background-color: white; } rect:hover { fill: orange; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 9px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Global Gender Gap</h1> <p> Index values from the World Economic Forum's 2014 Global Gender Gap Report. Countries included in this chart have <a href="https://reports.weforum.org/global-gender-gap-report-2014/part-1/appendix-b-tracking-the-gender-gap-over-time/"/> continuous index vales</a> from 2000-2014. For <em>all</em> 142 countries with 2014 index values, see the WEF's <a href="https://reports.weforum.org/global-gender-gap-report-2014/part-1/the-global-gender-gap-index-results-in-2014"/>full report</a>.</p> <p style="margin: 0px 0 0 0; color:gray"> Highest score = 1 (equality)</p> <p style="margin: 0px 0 0 0; color:gray"> Lowest score = 0 (inequality)</p> <script type="text/javascript"> var w = 700; var h = 410; var padding = [ 7, 10, 30, 120 ]; //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("WEFGenderGap_selected.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a["2014"], +b["2014"]); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d["2014"]; }) ]); heightScale.domain(data.map(function(d) { return d.Country; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Country); }) .attr("width", function(d) { return widthScale(+d["2014"]); }) .attr("height", heightScale.rangeBand()) .attr("fill", "gray") .append("title") .text(function(d) { return d.Country + "'s life satisfaction score is " + d["2014"]; }); 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