D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
MelanieJulien
Full window
Github gist
Greenhouse gases emissions
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Module 3 exercice</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: #EDEDED; } h { font-size: 24px; margin: 2; } p { font-size: 13px; font-family: arial; margin: 15px 0 0 0; } svg { background-color: #EDEDED; } rect:hover { fill: #635C5C; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: arial; font-size: 10px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Who is the world's biggest emitter of GHG in 2012?<h1/> <p>Here is a list of countries ranked by greenhouse gases emissions (GHG) per capita (kilograms, thousands), based on <a href="https://stats.oecd.org/">OECD data.<a/></p> <script type="text/javascript"> var w = 800; var h = 500; var padding = [1, 10, 30, 95 ]; //top - 0, right - 1, bottom - 2, left - 3 var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands( [ padding[0], h - padding[2] ], 0.2); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("melaniejuliendata.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.GHGin2012, +b.GHGin2012); }); widthScale.domain( [ 0, d3.max(data, function(d) { return +d.GHGin2012; }) ]); heightScale.domain(data.map(function(d) {return d.Country; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(d.Country); }) .attr("width", function(d) { return widthScale(d.GHGin2012); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#D11919") .append("title") .text(function(d) { return d.Country + "'s total GHG emissions in 2012 is " + d.GHGin2012 +" kg per capita (thousands)"; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 5) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js