D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Mavromatika
Full window
Github gist
GDP line chart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>GDP per capita in Europe</title> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: orange; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .country path{ stroke: #b9b9b9; opacity:0.4; } .high path{ stroke:steelblue; opacity:1; } #country_list{ position:absolute; left:650px; margin-top:20px; width: 170px; } #country_list p{ margin: 5px; padding: 3px; cursor: pointer; background-color:#dddddd; } #country_list p:hover{background-color:steelblue;} .axistitle{ font-size: 12px; } </style> </head> <body> <h1>Long-term evolution of GDP per capita in Europe</h1> <p>Gross Domestic Product (GDP) per capita for European countries between 1870 and 2010.<br> <strong>Move your mouse over the name of a country in the list to highlight it's curve.</strong><br> Based on <a href="https://www.ggdc.net/maddison/maddison-project/home.htm">Angus Maddison's data</a>, updated in 2013.</p> <div id="country_list"></div> <script type="text/javascript"> //Dimensions and padding var w = 700; var h = 500; var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left //Set up date formatting and years var dateFormat = d3.time.format("%Y"); //Set up scales 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") .ticks(15) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); //Configure line generator var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(+d.amount); }); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("gdppercapita.csv", function(data) { //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, gdp: [] }; //Loop through all the years for (var j = 1870; j < 2011; j++) { // If value is not empty if (data[i][j.toString()]) { //Add a new object to the GDP data array //for this country dataset[i].gdp.push({ year: j.toString(), amount: data[i][j.toString()] }); } } } //Set scale domains xScale.domain([dateFormat.parse("1870"), dateFormat.parse("2010")]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.gdp, function(d) { return +d.amount; }); }), 0 ]); //Make a group for each country var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .attr("class", "country") .attr("id", function(d, i){return "country_"+i.toString();}); //Append a title with the country name (so we get easy tooltips) groups.append("title") .text(function(d) { return d.country; }); //Within each group, create a new line/path, //binding just the emissions data to each one groups.selectAll("path") .data(function(d) { return [ d.gdp ]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("fill", "none") .attr("stroke-width", 2); //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); svg.append("text") .attr("class", "axistitle") .attr("text-anchor", "end") .attr("x", w-padding[3]) .attr("y", h - 55) .text("Years"); svg.append("text") .attr("class", "axistitle") .attr("text-anchor", "end") .attr("y", padding[2] + 62) .attr("x", -(padding[2] - 30)) .attr("transform", "rotate(-90)") .text("GDP per capita (1990 Int. GK$)"); d3.select("#country_list").selectAll("p") .data(dataset) .enter() .append("p") .text(function(d){return d.country;}) .attr("id", function(d,i){return i;}) .on("mouseover",function(){ d3.selectAll(".high").classed("high", false); var i="#country_"+ d3.select(this).attr("id"); d3.select(i).classed("high", true); } ) .on('mouseout', function(){ d3.selectAll('.high').classed('high',false); }); }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js