D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Journathan
Full window
Github gist
MODULE3 Exercise: Water Consumption In The New York City / Data Bars
<!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-opacity: 0.5; stroke:none; } body { background-color: #6dcff6; } h1 { color: #ffffff; font-family:tahoma; font-weight:100; font-size:200%; letter-spacing:5pt; } h3 { color: #ffffff; font-family: tahoma; font-size: 110%; font-weight: 400; text-indent: 10px; letter-spacing: 5pt; position: absolute; top: -0.5em; left: 0em; width: 30em } } rect:hover { fill: #ffffff; } svg { background-color: #6dcff6; } </style> </head> <body> <script type="text/javascript"> d3.select("body") .append("h1") .text("IN THE NEW YORK CITY"); d3.select("body") .append("h3") .text("WATER CONSUMPTION"); var svg = d3.select("body") .append("svg") .attr("width", 800) .attr("height", 400); 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"); rects.attr("x", 0) .attr("y", function(d, i) { return i * 13; }) .attr("fill", "black") .attr("width", function(d) { return d.NYCConsumptionMilliongallonsperday / 2; }) .attr("height",9) .append("title") .text(function(d) { return d.NYCConsumptionMilliongallonsperday + " Millions of Gallons per Day in " + d.Year; }); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js