D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Journathan
Full window
Github gist
MODULE6 Exercise: Time-based Data (new)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>NY State Budget</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: #ffffff; } p { color: #666666; font-size: 11px; line-height: 0; margin: 15px 0 15px 10px; } h1 { color: #000000; font-family:tahoma; margin: -12px 0 20px 10px; font-weight:100; font-size:200%; letter-spacing:3pt; } h3 { color: #000000; font-family: tahoma; font-size: 14px; margin: 15px 0 5px 0; 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: #fafbfb; } .axis path, .axis line { fill: none; opacity: 0.5; stroke: #3e3b3b; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 7px; opacity: 0.5; } </style> </head> <body> <h3>NYC STATE AID</h3> <h1>INDEPENDENT BUDGET</h1> <p>Annual state categorical aid by purpose of grant, <strong style="color: #666666;">Housing</strong> vs <strong style="color: #666666;">Recreation budget</strong>, 1980-2013.</p> <p><em>Source: <a href="https://catalog.data.gov/dataset/nyc-independent-budget-office-ibo-state-and-federal-categorical-aid-fy-1980-2013-43b1a">DATA.GOV</a>, 2015.</em></p> <script type="text/javascript"> var w = 1100; var h = 500; var padding = [ 10, -50, 40, 70 ]; //Top, right, bottom, left //Set up date formatting and years var dateFormat = d3.time.format("%Y"); var xScale = d3.time.scale() .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) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var area = d3.svg.area() .x(function(d) { return xScale(dateFormat.parse(d.Year)); }) .y0(h - padding[2]) .y1(function(d) { return yScale(d.Budget); }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("NYCIndependentBudgetOffice_Housing.csv", function(housingdata) { d3.csv("NYCIndependentBudgetOffice_ParksRecreationandCulturalActivities.csv", function(parksdata) { var mergedData = housingdata.concat(parksdata); xScale.domain([ d3.min(mergedData, function(d) { return dateFormat.parse(d.Year); }), d3.max(mergedData, function(d) { return dateFormat.parse(d.Year); }) ]); yScale.domain([ d3.max(mergedData, function(d) { return +d.Budget; }), 0 ]); // var circles = svg.selectAll("circle") // .data(data) // .enter() // .append("circle"); // // circles.attr("cx", function(d) { // return xScale(dateFormat.parse(d.Year)); // }) // .attr("cy", function(d) { // return yScale(d.NYCConsumptionMilliongallonsperday); // }) // .attr("r", 4) // .attr("fill", "#3e3b3b") // .append("title") // .text(function(d) { // return d.Year + " Water consumption per day: " + d.NYCConsumptionMilliongallonsperday + " MG"; // }); svg.data([ housingdata ]) .append("path") .attr("class", "line percapita") .attr("d", area) .attr("fill", "#6dcff6") .attr("stroke", "none") .attr("stroke-width", 0) .attr("opacity", 0.8); ; svg.data([ parksdata ]) .append("path") .attr("class", "line nyc") .attr("d", area) .attr("fill", "#000000") .attr("stroke", "none") .attr("stroke-width", 0) .attr("opacity", 0.3); ; 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); }); //End China data load function }); //End USA data load function </script> <svg width="1100" height="35" style="border: 1px solid none; background-color: #fafbfb; opacity: 1; "> <line x1="130" y1="9" x2="130" y2="25" stroke="#cccccc" stroke-width="1" /> <rect x="70" y="10" width="12" height="12" fill="#6dcff6"/> <text x="88" y="20" fill="#8a8c8e" font-size="9" font-weight="light" font-family="Helvetica">Housing</text> <rect x="145" y="10" width="12" height="12" fill="#b2b2b2"/> <text x="163" y="20" fill="#8a8c8e" font-size="9" font-weight="light" font-family="Helvetica">Parks, Recreation and Cultural activities</text> </svg> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js