D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Paperpanic
Full window
Github gist
Module 6 exercise
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Module 6 exercise</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> path:hover { stroke: white; stroke-width: 3px; } body { background-color: white; } svg { background-color: white; background-image: url("https://orig09.deviantart.net/8e3d/f/2015/115/b/2/image_by_paperpanic-d8r2eba.jpg"); opacity: 0.50; } h1 { font-size: 29px; margin: 0; font-family: arial; } p { font-family: arial; font-size: 20px; margin: 10px 0 0 0; } path { stroke: blue; stroke-width: 1; } g.highlight path { stroke: yellow; stroke-width: 4; } .axis path, .axis line { fill: none; stroke: black; stroke-width: 1; shape-rendering: crispEdges; } .axis text { font-family: arial; font-size: 10px; color: Gainsboro } </style> </head> <body> <h1>TOTAL FELONIES ON NEW YORK FROM 2000 TO 2011</h1> <p>Major an Minor crimes Index “Total Felonies” by year. Source: <a href="https://data.cityofnewyork.us/Public-Safety/Historical-New-York-City-Crime-Data/hqhv-9zeg">NYC open Data</a>, 2011</p> <script type="text/javascript"> var w = 800; var h = 600; var padding = [ 20, 10, 50, 100 ]; 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(10) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(+d.amount); }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("FelonyOffensesVSArrests-2000-2011.csv", function(data) { var years = ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"]; var dataset = []; for (var i = 0; i < data.length; i++) { dataset[i] = { country: data[i].Felonies, TotalCrimes: [] }; for (var j = 0; j < years.length; j++) { if (data[i][years[j]]) { dataset[i].TotalCrimes.push({ year: years[j], amount: data[i][years[j]] }); } } } xScale.domain([ d3.min(years, function(d) { return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.TotalCrimes, function(d) { return +d.amount; }); }), 0 ]); var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .classed("highlight", function(d) { if (d.country == "TOTALFELONYOFFENSES" || d.country == "TOTALARRESTS") { return true; } else { return false; } }); groups.append("title") .text(function(d) { return d.country; }); groups.selectAll("path") .data(function(d) { return [ d.TotalCrimes ]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("fill", "none") .attr("stroke-width", 2); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .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