D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tmcw
Full window
Github gist
Bike Peak Data, ways of looking at rates of growth
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } path.line { stroke-width:4; fill:none; stroke:darkblue; } path.linear { stroke-width:2; fill:none; stroke:gray; stroke-dasharray:2,2; } </style> <body> <script src="https://d3js.org/d3.v3.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 400 - margin.left - margin.right, height = 250 - margin.top - margin.bottom; var parseDate = d3.time.format("%Y").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var x2 = d3.time.scale() .range([0, width]); var y2 = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var x2Axis = d3.svg.axis() .scale(x2) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var y2Axis = d3.svg.axis() .scale(y2) .orient("left"); var absolute = 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 + ")"); var line = d3.svg.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.peak); }); var line2 = d3.svg.line() .x(function(d) { return x2(d.date); }) .y(function(d) { return y2(d.peak); }); var added = 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("peak.csv", function(error, data) { data.forEach(function(d) { d.date = parseDate(d.year); d.peak = +d['Peak Hour']; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain(d3.extent(data, function(d) { return d.peak; })); var linear = absolute.selectAll(".linear") .data([[{ date: x.domain()[0], peak: y.domain()[0] }, { date: x.domain()[1], peak: y.domain()[1] }]]) .enter().append("g") .attr("class", "linear"); linear.append("path") .attr("class", "linear") .attr("d", line); var l = absolute.selectAll(".browser") .data([data]) .enter().append("g") .attr("class", "browser"); l.append("path") .attr("class", "line") .attr("d", line); absolute.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); absolute.append("g") .attr("class", "y axis") .call(yAxis); var added_data = []; for (var i = 1; i < data.length; i++) { added_data.push({ date: data[i].date, peak: data[i].peak - data[i - 1].peak }); } x2.domain(d3.extent(added_data, function(d) { return d.date; })); y2.domain(d3.extent(added_data, function(d) { return d.peak; })); added.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(x2Axis); added.append("g") .attr("class", "y axis") .call(y2Axis); var ladd = added.selectAll(".foo") .data([added_data]) .enter().append("g") .attr("class", "foo"); ladd.append("path") .attr("class", "line") .attr("d", line2); }); </script>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js