D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
kvnphllps
Full window
Github gist
Comparison of Uranium ore/concentrate production: a look at years 2000 and 2010
<!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; } circle:hover { fill: pink; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: futura; font-size: 11px; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } </style> <script type="text/javascript" src = "https://d3js.org/d3.v3.js"></script> </head> <body> <h2>Uranium ore & concentrate production: comparison of year 2000 and 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 = 650; // x-axis var currSVG_h = 550; // y-axis // Define padding var padding = [20, 20, 20, 122]; // Top Right Bottom Left (like in CSS) // Create a fn that scales data along x-axis, range specifies output scale pxls. var xScale = d3.scale.linear() .range([ padding[3] , currSVG_w - padding[1] ]); // Create a fn that scales data along y-axis, range specifies output scale in pxls. var yScale = d3.scale.linear() .range([ padding[0], currSVG_h - padding[2] ]); // Create a fn that will draw our x-axis var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(yScale) .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); // Define identity line var line = d3.svg.line() .x(function(d) { return xScale(d.yr_0000); }) .y(function(d) { return yScale(d.yr_0000); }); //Load in contents of CSV file & plot d3.csv("uranium_data_12APR15.csv", function(data) { var temp1 = d3.max(data, function(d) { return +d.yr_2000; }); var temp2 = d3.max(data, function(d) { return +d.yr_2010; }); if (temp1>temp2){ var uLimit = temp1; } else{ var uLimit = temp2; } xScale.domain([ 0, uLimit ]); // input y-axis (ordinal) scale of our data // yScale.domain([ d3.max(data, function(d){ // return +d.yr_2010; // }), 0 ]); yScale.domain([ uLimit, 0 ]); // initialize rectangles that will comprise our bar chart var circles = currSVG.selectAll("circle") .data(data) // data gets bound .enter() // data enters the stage .append("circle"); //what we are drawing with circles.attr("cx", function(d){ return xScale(d.yr_2000); }) .attr("cy", function(d) { return yScale(d.yr_2010); }) .attr("r",1000) .attr("fill","gold") // fill color of the bars .append("title") .text(function(d) { return d.country + " year 2000 production: " + d.yr_2000 +" [metric tons]"+ ", year 2010 production: " + d.yr_2010 +" [metric tons]"; }); // Transition circles.transition() .duration(2000) .attr("r",10); // Draw the x-axis currSVG.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (currSVG_h - padding[2]) + ")") .call(xAxis); currSVG.append("g") .attr("class", "y axis") .attr("transform", "translate("+ padding[3] +",0)") .call(yAxis); currSVG.append("path") .datum(data) .style("stroke-dasharray", ("5, 5")) .attr("class", "line") .attr("d", line); console.log(data); }); </script> </body> </html>