D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
etachov
Full window
Github gist
Tachovsky Assignment 6
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Political Stability in Africa</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Garamond, sans-serif } h1 { font-size: 30px; margin: 12px 0 0 75px; } p { font-size: 20px; margin: 10px 0 0 75px; } a:link{ color: steelblue; } svg { background-color: white; } /*highlight style for the average line*/ g.highlight path { stroke: steelblue; stroke-width: 5; stroke-opacity: 1; } .axis path, .axis line { fill: none; stroke: black; stroke-width: 1; shape-rendering: crispEdges; } .axis text { font-family: Garamond, sans-serif; font-size: 14px; } </style> </head> <body> <h1>Political Stability Across Africa, 2002-2011</h1> <p>Data Source: <a href= "data.worldbank.org" target="_blank">World Bank</a>. Higher scores indicates greater stability.</br> Mouseover to highlight individual countries.</p> <script type="text/javascript"> var w = 800; var h = 420; var padding = [ 20, 50, 50, 70]; //Top, right, bottom, left var dateFormat = d3.time.format("%Y"); var xScale = d3.time.scale() .range([padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(10) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(+d.score); }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("polStaWide.csv", function(data) { var years = ["2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"]; var dataset = []; for (var i = 0; i < data.length; i++) { dataset[i] = { country: data[i].country, stability: [] }; for (var j = 0; j < years.length; j++ ) { if(data[i][years[j]]) { dataset[i].stability.push({ year: years[j], score: data[i][years[j]] }); } } } xScale.domain([ d3.min(years, function(d){ return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.stability, function(d) { return +d.score; }); }), d3.min(dataset, function(d) { return d3.min(d.stability, function(d) { return +d.score -.1; }); }) ]); var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .classed("highlight", function(d) { if (d.country == "Average") { return true; } else { return false; } }); groups.append("title") .text(function(d) { return d.country; }); groups.selectAll("path") .data(function(d) { return [ d.stability ]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("stroke", "grey") .attr("fill", "none") .attr("stroke-width", 2) .attr("stroke-opacity", .5) .on("mouseover", function() { d3.select(this) .transition() .duration(25) .attr("stroke", "orange") .attr("stroke-width", 5); }) .on("mouseout", function(d) { d3.select(this) .transition() .duration(200) .attr("stroke", "grey") .attr("stroke-width", 2) .attr("stroke-opacity", .5); }); // Axes svg.append("g") .attr("class", "y 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) // Adding a single label in a hacky way svg.append("text") .attr("transform", "rotate(-90") .attr("x", 728) .attr("y", 157) .style("text-anchor", "middle") .style("fill", "steelblue") .text("Africa Average"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js