D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
junngeronimo
Full window
Github gist
MidtermMultiSeries
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } svg { background-color: whitesmoke; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } p { font-size: 12px; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var translate = function(x, y) { return "translate(" + x + "," + y + ")"; }; var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var dateFormat = d3.time.format("%x"); var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("linear") .x(function(d) { return x(new Date(d.key)); }) .y(function(d) { return y(d.values); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", translate(margin.left, margin.top)); d3.csv("sfpd-property-crimes-2005-2015.csv", function(d) { var row = {}; row.date = dateFormat.parse(d["Date"]); row.category = d["Category"]; row.count = +d["Incidents"]; return row; }, function(error, rows) { if (error) throw error; data = d3.nest() .key(function(d) { return d.category; }) .key(function(d) { return d.date;}) .rollup(function(values) { return d3.sum(values, function(d) { return d.count; }) }) .entries(rows); var crimes = data.keys(); color.domain(crimes); var min = 100, max = 1; data.forEach(function(d) { d.values.forEach(function(c) { if (c.values > max) { max = c.values; } else if (c.values < min) { min = c.values; } }); }); // console.table(rows); // console.table(data); x.domain(d3.extent(rows, function(d) { return d.date; })); y.domain([min, max]); svg.append("g") .attr("class", "x axis") .attr("transform", translate(0, height)) .call(xAxis) .append("text") .attr("x", width - 10) .attr("y", 10) .attr("dy", ".71em") .style("text-anchor", "end") .text("Date"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Number of Incidents"); svg.append("text") .text("SF Property Crimes from 2005 to 2015") .attr("x", 450) .attr("y", 20) .attr("text-anchor", "middle") .attr("font-size", "18px"); var crime = svg.selectAll(".crime") .data(data) .enter() .append("g") .attr("class", "crime"); crime.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return color(d.key); }); var legend = svg.selectAll(".legend") .data(color.domain().slice().reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return translate(0, i * 20); }); legend.append("rect") .attr("x", 130) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", 120) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d; }); }); </script>
https://d3js.org/d3.v3.min.js