D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
nikhilkardale
Full window
Github gist
CO2 emissions visualisation for one year (Horizontal bar graph with vertical axis labeling)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Horizontal Bar Graph</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; } svg { background-color: white; } </style> </head> <body> <svg width="1200" height="40"> <text fill="charcoal" font-size="20" font-weight="bold" font-family="Helvetica"> <tspan x="160" y="30">CO2 gas emissions (million tonnes) by various countries for the year 1999</tspan> </text> </svg> <script type="text/javascript"> var strYear = '1999'; d3.csv("co2_emissions.csv", function(data) { var svg = d3.select("body") .append("svg") .attr("x", 0) .attr("y", 0) .attr("width", 1200) .attr("height", 1100); data.sort(function(a, b) { return d3.descending(+a[strYear], +b[strYear]); }); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", 160) .attr("y", function(d, i) { return 10 + (i * 20); }) .attr("width", function(d) { return d[strYear] / 5; }) .attr("height", 10) .attr("fill", "rgb(100, 100, 100)") .append("title") .text(function(d) { return "CO2 emission by " + d.Country + " in the year " + strYear + " was " + d[strYear] + " million tonnes."; }); svg.selectAll("text") .data(data) .enter() .append("svg:text") .attr("x", 0) .attr("y", function(d, i) { return 10 + (i * 20); }) .attr("dy", 10) .attr("text-anchor", "left") .attr("style", "font-size: 4; font-family: Helvetica, sans-serif") .text(function(d) { return d.Country; }); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js