D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
kvnphllps
Full window
Github gist
d3 plot of uranium ore production by country
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>Uranium ores & concentrates production</title> <style type="text/css"> h1,h2,h3,p { font-family: futura; } h2,p { text-indent: 120px; color: steelblue; } svg { background-color: white; } rect:hover { fill: pink; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: futura; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } </style> <script type="text/javascript" src = "https://d3js.org/d3.v3.js"></script> </head> <body> <h2>Uranium ore & concentrate production in 2010 by country </h2> <p>Production in metric tons = 1000 kg. Source: <a href="https://data.un.org/Data.aspx?d=ICS&f=cmID:13000-1" >United Nations Industrial Commodity Statistics Database</a> </p> <script type="text/javascript"> // Define width / height of SVG element. var currSVG_w = 1000; // x-axis var currSVG_h = 550; // y-axis // Define padding var padding = [0, 20, 50, 120]; // Top Right Bottom Left (like in CSS) // Create a fn that scales data along x-axis, range specifies output scale pxls. var widthScale = d3.scale.linear() .range([ 0 , currSVG_w - padding[1] - padding[3] ]); // Create a fn that scales data along y-axis, range specifies output scale in pxls. var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], currSVG_h - padding[2] ],0.2); // Create a fn that will draw our x-axis var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); // Define our working SVG area of the page var currSVG = d3.select("body") .append("svg") .attr("width", currSVG_w) .attr("height", currSVG_h); //Load in contents of CSV file & plot d3.csv("uranium_data_12APR15.csv", function(data) { //CSV converted to JSON objects. //Log 'data' to the console, for verification. // console.log(data); data.sort(function(a, b) { return d3.descending(+a.yr_2010, +b.yr_2010); }); // input x-axis scale of our data widthScale.domain([ 0, d3.max(data, function(d) { return +d.yr_2010; }) ]); // input y-axis (ordinal) scale of our data heightScale.domain(data.map(function(d) { return d.country; } )); // initialize rectangles that will comprise our bar chart var rects = currSVG.selectAll("rect") .data(data) // data gets bound .enter() // data enters the stage .append("rect"); //what we are drawing with rects.attr("x", padding[3]) // each bar starts at same x position .attr("y", function(d) { //"d" here is a single data point, while "i" is the index return heightScale(d.country); }) .attr("width", function(d){ return widthScale(d.yr_2010); }) .attr("height",heightScale.rangeBand()) .attr("fill","gold") // fill color of the bars .append("title") .text(function(d) { return d.country + " produced " + d.yr_2010 + " metric tons of Uranium ores and concentrates in 2010."; }); // Draw the x-axis currSVG.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (currSVG_h - padding[2]) + ")") .call(xAxis); currSVG.append("g") .attr("class", "y axis") .attr("transform", "translate("+ padding[3] +",0)") .call(yAxis); }); </script> </body> </html>