D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jab0122
Full window
Github gist
China and Brazil Air and GHG Emissions
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Module 6</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: beige; } svg { background-color: white; } h1 { font-size: 24px; font-family: sans-serif; margin: 0; } p { font-family: sans-serif; font-size: 14px; margin: 10px 0 0 0; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 9px; } circle:hover { fill: blue; } /* fill with some color when the mouse is passing through */ </style> </head> <body> <h1>China and Brazil Air and GHG Emissions- Module 6</h1> <p>The following graphic represents the Air and GHG Emissions for China and Brazil in tonnes per capita between the years 2000 and 2012. Source: <a href="https://data.oecd.org/air/air-and-ghg-emissions.htm">OEDC</a>.</p> <script type="text/javascript"> var w = 700; var h = 600; var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left //Set up date formatting and years 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] ]); //creating a new variable to store the line, where x and y will determine the coordinates for the drawing of the path var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(d.Emissions); }); 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"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("China Emissions.csv", function(chinaData) { d3.csv("Brazil Emissions.csv", function(brazilData) { var mergedData = chinaData.concat(brazilData); xScale.domain([ d3.min(mergedData, function(d) { return dateFormat.parse(d.year); }), d3.max(mergedData, function(d) { return dateFormat.parse(d.year); }) ]); yScale.domain([ d3.max(mergedData, function(d) { return +d.Emissions; }), 0 ]); //the circles can be "un commented" if you want to check if the points really correspond to the line //var circles = svg.selectAll("circle") // .data(data) // .enter() // .append("circle"); //circles.attr("cx", function(d) { // return xScale(dateFormat.parse(d.year)); // }) // .attr("cy", function(d) { // return yScale(d.ChinaEmissions); // }) // .attr("r", 4) // .attr("fill", "firebrick") // .append("title") // .text(function(d) { // return d.year + " emissions: " + d.ChinaEmissions; // }); svg.data([ chinaData ]) .append("path") .attr("class", "line china") .attr("d", line) .attr("fill", "none") .attr("stroke", "firebrick") .attr("stroke-width", 2) .append("title") .text("China"); svg.data([ brazilData ]) .append("path") .attr("class", "line brazil") .attr("d", line) .attr("fill", "none") .attr("stroke", "orange") .attr("stroke-width", 2) .append("title") .text("Brazil"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .call(yAxis); }); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js