D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Franny711
Full window
Github gist
Sugar Consumption Around the World
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Sugar Consumption Around The World</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: red; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>How Has Sugar Consumption Increased or Decreased Around the World</h1> <p>In this dataset, per capita sugar consumption was tracked over the course of 43 years in 145 countries. The Y axis represents 2004 and X axis represents 1961.<p> This scatterplot shows an increasing trend in the consumption of sugar.<p> <b>HOWEVER, 23 countries in the dataset DECREASED consumption.</b> <p> It's hard to see those countries in this form of visualization. A scatterplot using lines for each country would be preferable. <p> To develop the interpretation of this dataset further - I would want to: <ul style="list-style-type:square"> <li>Describe how the data were collected</li> <li>Describe what the sources of sugar were</li> <li>Refine the data into increased, decreased, and stayed same.</li> <li>Bind the data to a geomap to tell the story of this data more clearly</li> </ul> <p> <b>COUNTRY DATA AT-A-GLANCE</b><br> 119 increased consumption<br> 23 decreased consumption<br> 3 stayed the same <p> Source: FAOSTAT - Food and Agriculture Organization of the United Nations Statistics Division.<br></p> <script type="text/javascript"> var w = 700; var h = 600; var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("sugarconsumption.csv", function(data) { xScale.domain([ d3.min(data, function(d) { return +d.y1961; }), d3.max(data, function(d) { return +d.y1961; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.y2004; }), d3.min(data, function(d) { return +d.y2004; }) ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.y1961); }) .attr("cy", function(d) { return yScale(d.y2004); }) .attr("r", 0.1) .attr("fill", "pink") .append("title") .text(function(d) { return d.Country + "'s per capita sugar consumption was " + d.y1961 + " grams in 1961, and " + d.y2004 + " grams in 2004"; }); circles.sort(function(a, b) { return d3.ascending(+a.y1961, +b.y1961); }) .transition() .delay(function(d, i) { return i * 10; }) .duration(2000) .attr("r", 7); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js