D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
DanielleW
Full window
Github gist
GDP per capita of 10 largest economies
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>GDP per capita of 10 largest economies</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <link rel="stylesheet" type="text/css" href="styles_lines.css"> </head> <body> <p><h2>GDP per capita of 10 largest economies</h2></p> <div id="box">The line chart below shows the GDP per capita of the 10 largest economies. The <strong><font color="red">United States, Japan and China</font></strong> are highlighted in red. The United States GDP per capita is a fairly smooth increasing line. Japan by contrast is much more jagged with strong rise from mid 1980's until the mid 1990's followed by strong drop and volatility. Currently China is the second largest economy in the world and yet still has a long way to go to achieve the same GDP per capita as the United States, Japan or Western European countries.<div> <script type="text/javascript"> //Dimensions and padding var w = 1000; var h = 600; var padding = [20, 10, 50, 100]; //Top, right, bottom, left //Set up date formatting and years //Define the format, in our case YYYY //For all options, see: https://github.com/mbostock/d3/wiki/Time-Formatting var dateFormat = d3.time.format("%Y"); //time scale. var xScale = d3.time.scale() .range([padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding [0], h - padding [2] ]); // configure axis generators var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .tickFormat(function (d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); // Configure line generator (draws line) //needs where to draw x and y points //d take data value and return the year parsed into date format and then hand it off to xScale // var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(d.amount); }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("WDIGDPpercap_top10.csv", function(data) { //Data is loaded in, but we need to restructure it. //Remember, each line requires an array of x/y pairs; //that is, an array of arrays, like so: // // [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ] // //We, however, are using 'year' as x and 'amount' as y. //We also need to know which country belongs to each //line, so we will build an array of objects that is //structured like this: /* [ { country: "Australia", emissions: [ { year: 1961, amount: 90589.568 }, { year: 1962, amount: 94912.961 }, { year: 1963, amount: 101029.517 }, … ] }, { country: "Bermuda", emissions: [ { year: 1961, amount: 176.016 }, { year: 1962, amount: 157.681 }, { year: 1963, amount: 150.347 }, … ] }, … ] */ //Note that this is an array of objects. Each object //contains two values, 'country' and 'emissions'. //The 'emissions' value is itself an array, containing //more objects, each one holding 'year' and 'amount' values. //New array with all the years, for referencing later var years = ["1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"]; //Create a new, empty array to hold our restructured dataset var dataset = []; //Loop once for each row in data for (var i = 0; i < data.length; i++) { //Create new object with this country's name and empty array dataset[i] = { country: data[i].country, percap: [] }; //Loop through all the years for (var j = 0; j < years.length; j++) { // If value is not empty if (data[i][years[j]]) { //Add a new object to the percap data array //for this country dataset[i].percap.push({ year: years[j], amount: data[i][years[j]] }); } } } //Uncomment to log the original data to the console //console.log(data); //Uncomment to log the newly restructured dataset to the console console.log(dataset); //Set Scale domains 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.percap, function(d) { return +d.amount; }); }), 0 ]); //Make group for each country var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .classed("highlight", function(d) { if (d.country == "Japan" || d.country == "China" || d.country == "United States") { return true; } else { return false; } }); //Append a title with the country name on rollover groups.append("title") .text(function(d) { return d.country; }); //Within each group, create new line or path binding just the per capita dat to each one groups.selectAll("path") .data(function(d) { return [d.percap]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("fill", "none") .attr("stroke", "gray") .attr("stroke-width", .5); //Axes 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); //add text label for x axis svg.append("text") .attr("class", "labelaxis") .attr("transform", "translate(0," + (h - 20) + ")") .attr("x", w/2 + 10) .attr("dy", "1em") .style("text-anchor", "middle") .text("Year"); //add text label for y axis svg.append("text") .attr("class", "labelaxis") .attr("transform", "rotate(-90)") .attr("y", 0-padding[4]) .attr("x", 50 - (h/2)) .attr("dy", "1em") .style("text-anchor", "middle") .text("GDP per capita (current US$)"); }); </script> <p><b>Source:</b><a href="https://data.worldbank.org/indicator/NY.GDP.PCAP.CD" target="_blank"> World Bank - World Development Indicators</a> </p> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js