D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
clairepapu
Full window
Github gist
Who Eats Apples & Bananas in the U.S.?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Apples & Bananas Consumption in the U.S.</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> h1 {font-family: cambria,serif; line-height: 26px; font-size: 30px; color: dimgray; margin: 6px 6px 6px 6px;} h2 {font-family: cambria,serif; line-height: 18px; font-size: 16px; color: dimgray; margin: 6px 6px 6px 6px;} blockquote {margin-left: 110px;} body { background-color: white; font-family: sans-serif; font-size: 11px; color: #4D4D4D; } p.bananas { margin-left: 100px;} p.apples { margin-left: 520px;} svg { background-color: white; padding-top: 5px; } circle { fill:#ff4444;} circle:hover { fill: #ffd700; } .axis path, .axis line { fill: none; stroke: dimgray; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; color: dimgray; } </style> </head> <body> <img src="https://d18l82el6cdm1i.cloudfront.net/solvable/0f529337aa.a5a239505e.dI2Laa.jpg" alt="apples and bananas" width="100" align=left padding-right="4"> <h1>Apples & Bananas Consumption<br /> in the United States</h1> <h2>Per Capita Consumption in Retail Pounds Per Year, 1999-2002</h2> <hr width="600" align="left"/> <p class="bananas"><em>Bananas</em></p> <script type="text/javascript"> var w = 700; var h = 520; var padding = [ 10, 10, 28, 130 ]; //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") .ticks(15); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(25) .tickFormat(function(d) { return d; }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("fruits-by-descriptor-uploaded-to-gist-3FINAL.csv", function(data) { xScale.domain([ d3.min(data, function(d) { return +d.Apples; }), d3.max(data, function(d) { return +d.Apples; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.Bananas; }), d3.min(data, function(d) { return +d.Bananas; }) ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.Apples); }) .attr("cy", function(d) { return yScale(d.Bananas); }) .attr("r", 0.1) .attr("fill", "#63B9AC", "0.5") .append("title") .text(function(d) {return d.Descriptor + "'s annual per capita consumption of APPLES, in retail pounds, is " + d.Apples + ". Annual per capita consumption of BANANAS, in retail pounds, is " +d.Bananas;}); circles.sort(function(a, b) { return d3.ascending(+a.Apples, +b.Apples); }) .transition() .delay(function(d, i) { return i * 50; // this is a per-element delay. For each circle, take the index value and set the delay for that circle's transision to whatever the index is times 50. }) .duration(1000) .attr("r", 6); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .attr("fill", "dimgray") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .attr("fill", "dimgray") .call(yAxis); }); </script> <p class="apples"><em>Apples</em></p></blockquote.bananas> <blockquote><p>Sources: Food Intakes Converted to Retail Commodities Databases (ARS) and Food Availability (Per Capita)<br /> Data System; <a href="https://www.ers.usda.gov/data-products/commodity-consumption-by-population-characteristics.aspx#27850">Table 1: Fruit Availablity;</a> United States Department of Agriculture, Economic Research Service, 1999-2002.</p></blockquote> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js