D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Journathan
Full window
Github gist
MODULE4 Exercise: Adding Scales and Axes
<!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"> rect:hover { fill: #000000; opacity: 0.5; } 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; } rect:hover { fill: #000000; opacity: 0.2; } svg { background-color: #6dcff6; } .axis path, .axis line { fill: none; opacity: 0.1; stroke: #3e3b3b; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 7px; opacity: 0.6; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h3>WATER CONSUMPTION</h3> <h1>IN NEW YORK CITY</h1> <p>A brief history of water consumption in the New York City Water Supply System (Based on New York City Census population).</p> <p><em>Millions of gallons per day. 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 = [ 0, 20, 20, 35 ]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("WaterConsumptionInTheNewYorkCity.csv", function(data) { data.sort(function(a, b) { return d3.descending(a.Year, b.Year); //If your numeric values aren't sorting properly, //try commenting out the line above, and instead using: // //return d3.descending(+a.lifeSatisfaction, +b.lifeSatisfaction); // //Data coming in from the CSV is saved as strings (text), //so the + signs here force JavaScript to treat those //strings instead as numeric values, thereby fixing the //sort order (hopefully!). }); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); widthScale.domain([ 0, d3.max(data, function(d) { return +d.NYCConsumptionMilliongallonsperday; }) ]); heightScale.domain(data.map(function(d) { return d.Year; } )); rects.attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(d.Year); }) .attr("width", function(d) { return widthScale(d.NYCConsumptionMilliongallonsperday); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#3e3b3b") .append("title") .text(function(d) { return d.NYCConsumptionMilliongallonsperday + " Millions of Gallons per Day in " + d.Year; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 2) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js