D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
missmee
Full window
Github gist
Women education and women representation in parliament worldwide
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Women in Parliament</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; color:black; } h1 { font-size: 24px; font-family: georgia, serif; margin: 10px 0 0 0; } p { font-size: 1em; margin: 10px 0 0 0; } svg { background-color: white; padding-top: 5px; padding-left: 5px; } circle:hover { fill: #F2BF30; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 0.8em; } </style> </head> <body> <h1>Women's representation in national parliaments and girls primary school enrollment </h1> <p>Proportion of seats held by women in national parliaments in 2014 - source: <a href="https://mdgs.un.org/unsd/mdg/Data.aspx">United Nations</a><br> Primary school gross enrollment for girls between 2008 and 2012 - source : <a href="https://data.unicef.org/education/primary">UNICEF</a></p> <script type="text/javascript"> // 1. setting the size of the svg var w = 900; var h = 600; var padding = [20, 10, 50, 50 ]; // 0 top, 1 right, 2 bottom, 3 left // 2. setting range ie pixel scales // 2.1. horizontal (x) var xScale = d3.scale.linear() .range([padding[3], w - padding[1] - padding[3] ]); //2.2 vertical (y) var yScale = d3.scale.linear() .range([padding[0], h - padding[2] ]); // 3. setting the axis var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .tickFormat(function(d) { return d + "%"; }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .tickFormat(function(d) { return d + "%"; }); // 4. appending the svg to the page var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //5. fecthing the data d3.csv("WomenData.csv", function(data) { // 6. sorting the data data.sort(function(a, b) { return d3.descending(+a.Seats, +b.Seats); //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!). }); // 7. setting the domain ie width scale to the data - set to smallest and highest values instead of zero to highest xScale.domain([ d3.min(data, function(d) { return +d.Seats; }), d3.max(data, function(d) { return +d.Seats; }) ]); // 8. setting the domain ie height scale to the data yScale.domain([ d3.max(data, function(d) { return +d.Schools; }), d3.min(data, function(d) { return +d.Schools; }) ]); // 9. drawing the dots with the data var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); //10. positionning the dots depending on our two values we are comparing circles.attr("cx", function(d) { return xScale(d.Seats); }) .attr("cy", function(d) { return yScale(d.Schools); }) .attr("r", 0.1) .attr("fill", "#B2ACB5") .append("title") .text(function(d) { return d.Country + " has " + d.Seats + "% of women in parliament and " + d.Schools + "% for primary school gross enrollment ratio for girls"; }); circles.sort(function(a, b) { return d3.ascending(+a.Seats, +b.Seats); }) .transition() .delay(function(d, i) { return i * 15; }) .duration(1500) .attr("r", 5) .attr("fill", "#573469"); // 11. drawing the axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (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