D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jab0122
Full window
Github gist
Air and GHG emissions - Module 4
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Module 4</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; } .y.axis path, .y.axis line { opacity: 0; } /* taking of the y axix */ rect:hover { fill: orange; } /* fill with some color when the mouse is passing through */ </style> </head> <body> <h1> Air and GHG emissions </h1> <p> This bar chart represents the Air and GHG emissions for each country in tonnes/capita at the year of 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 = 1000; //padding (margins): setting a room for the axis var padding = [20,10,20,200]; // Top, right, bottom, left //setting the domain var wscale = d3.scale.linear().range([ 0, w - padding[1] - padding[3] ]); var hscale = d3.scale.ordinal().rangeRoundBands([ padding[0], h - padding[2] ], 0.1); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); var xAxis = d3.svg.axis().scale(wscale).orient("bottom"); var yAxis = d3.svg.axis().scale(hscale).orient("left"); d3.csv("Air and GHG emissions.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.year2012, +b.year2012); }); //setting the domains wscale.domain([ 0, d3.max( data, function(d){ return +d.year2012; }) ]); //setting the domains + add the function map in order to creat and array with all country names // the contry name will be retuned in the y axis hscale.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) { //set where each bar is going to beggining return hscale(d.Country); }) .attr("width", function(d) { return wscale(d.year2012); }) .attr("height", hscale.rangeBand()) //set the thickness of the bar .attr("fill", "firebrick") .append("title") .text(function(d) { return d.Country + "'s air and GHG emissions in 2012: " + d.year2012; }) 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