D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Journathan
Full window
Github gist
MODULE5 Exercise: Scatterplot
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Water Consumption NY</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { font-family: Helvetica, Arial, sans-serif; background-color: #6dcff6; } p { color: #3e3b3b; font-size: 11px; line-height: 0; margin: 15px 0 15px 10px; } h1 { color: #ffffff; font-family:tahoma; margin: -9px 0 0 10px; font-weight:100; font-size:200%; letter-spacing:3pt; } h3 { background-color: #ffffff; color: #666666; font-family: tahoma; font-size: 14px; margin: 15px 0 5px 10px; font-weight:100; text-indent:10px; letter-spacing:3pt; } em { fill: #000000; opacity: 0.7; font-style: italic; } circle:hover { fill: #ffffff; transition: all 0.4s ease-in-out 0s; } svg { background-color: #6dcff6; } .axis path, .axis line { fill: none; opacity: 1; stroke: #3e3b3b; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 7px; opacity: 1; } </style> </head> <body> <h3>WATER CONSUMPTION</h3> <h1>IN NEW YORK CITY</h1> <p>A brief History of Water Consumption: New York City Consumption (Millions of Gallons) VS Per capita (Gallons per person).</p> <p><em>Source: <a href="https://catalog.data.gov/dataset/water-consumption-in-the-new-york-city-3d2f0">DATA.GOV</a>, 2015.</em></p> <script type="text/javascript"> var w = 650; var h = 350; var padding = [ 10, 20, 20, 35 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ 40, w - padding[0] - padding[0] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ], 0.1); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks("10") .tickFormat(function(d) { return d + " MG"; }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks("10") .tickFormat(function(d) { return d + " Gl"; }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("WaterConsumptionInTheNewYorkCity.csv", function(data) { xScale.domain([ d3.min(data, function(d) { return +d.NYCConsumptionMilliongallonsperday; }), d3.max(data, function(d) { return +d.NYCConsumptionMilliongallonsperday; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.PerCapitaGallonsperpersonperday; }), d3.min(data, function(d) { return +d.PerCapitaGallonsperpersonperday; }) ]); var circle = svg.selectAll("circle") .data(data) .enter() .append("circle"); circle.attr("cx", function(d) { return xScale(d.NYCConsumptionMilliongallonsperday); }) .attr("cy", function(d) { return yScale(d.PerCapitaGallonsperpersonperday); }) .attr("r", 12) .attr("fill", "#6dcff6") .append("title") .text(function(d) { return d.NYCConsumptionMilliongallonsperday + " Millions of gallons" + ", and " + d.PerCapitaGallonsperpersonperday + " Gallons per person. " + "Per day in " + d.Year; }); circle.sort(function(a, b) { return d3.ascending(+a.Year, +b.Year); }) .transition() .delay(function(d, i) { return i * 150; }) .duration(1000) .attr("fill", "#3e3b3b") .attr("r", 4); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 5) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3]) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js