D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jdistorey
Full window
Github gist
Comparison of two-wheeled traffic counts in the East Midlands from 2007 to 2011
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Two-wheeled vehicle traffic counts in the East Midlands from 2007 - 2011</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } rect:hover { fill: white; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .axis2 path, .axis2 line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis2 text { font-family: sans-serif; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } .y.axis2 path, .y.axis2 line { opacity: 0; } </style> </head> <body> <h1>Two-wheeled vehicle traffic counts in the East Midlands from 2007 - 2011</h1> <p>Comparison of bicycle and motorbike traffic counts in the East Midlands at different times of day in the period from 2007 - 2011. Source: <a href="https://data.gov.uk/dataset/gb-road-traffic-counts/resource/8d49272c-e525-495e-a60c-a93d62aedd6e">gov.uk</a></p> <script type="text/javascript"> var w = window.innerWidth; var h = window.innerHeight/1.25; var padding = [ 20, 10, 30, 120 ]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w/4 - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.4); var widthScale2 = d3.scale.linear() .range([ w/4 - padding[1] - padding[3], 0 ]); var widthScale3 = d3.scale.linear() .range([ w/4 - padding[1] - padding[3], 0 ]); var heightScale2 = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.4); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var xAxis2 = d3.svg.axis() .scale(widthScale3) .orient("bottom"); var yAxis2 = d3.svg.axis() .scale(heightScale2) .orient("right"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("traffic counts in the East Midlands 2007 - 2011 by hour on urban roads.csv", function(data) { widthScale.domain([ 0, d3.max(data, function(d) { return +d.bicycleCount; }) ]); heightScale.domain(data.map(function(d) { return d.Row; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Row); }) .attr("width", function(d) { return widthScale(d.bicycleCount); }) .attr("height", heightScale.rangeBand()) .attr("fill", function(d) { return d.bicycleColour }) .attr("stroke", function(d) { return d.bicycleColour }) .append("title") .text(function(d) { return d3.round(d.bicycleCount,1) + " bicycles were recorded on average in the hour after" + " " + d.Hour + ":00 " + "on" + " " + d.RCat + " " + "roads"; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .data(data) .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); svg.append("g") .append("text") .attr("transform", "translate(" + padding[3] + "," + (h/10 - padding[2]) + ")") .text("bicycles"); }); d3.csv("traffic counts in the East Midlands 2007 - 2011 by hour on urban roads.csv", function(data1) { widthScale2.domain([ d3.max(data1, function(d) { return +d.motorbikeCount; }), 0 ]); widthScale3.domain([ 0, d3.max(data1, function(d) { return +d.motorbikeCount; }) ]); heightScale2.domain(data1.map(function(d) { return d.Row; } )); var rects1 = svg.selectAll("rect1") .data(data1) .enter() .append("rect"); rects1.attr("x", function(d) { return (w/2 - widthScale2(d.motorbikeCount)); }) .attr("y", function(d) { return heightScale2(d.Row); }) .attr("width", function(d) { return widthScale2(d.motorbikeCount); }) .attr("height", heightScale2.rangeBand()) .attr("fill", function(d) { return d.motorbikeColour }) .attr("stroke", function(d) { return d.motorbikeColour }) .append("title") .text(function(d) { return d3.round(d.motorbikeCount,1) + " motorbikes were recorded on average in the hour after" + " " + d.Hour + ":00 " + "on" + " " + d.RCat + " " + "roads"; }); var xtranslation = w/4 + padding[1] + padding[3] var ytranslation = w/2 var ytranslation2 = w/2 - padding[3] + (padding[1] * 4) svg.append("g") .attr("class", "x axis2") .attr("transform", "translate(" + xtranslation + "," + (h - padding[2]) + ")") .call(xAxis2); svg.append("g") .data(data1) .attr("class", "y axis2") .attr("transform", "translate(" + ytranslation + ",0)") .call(yAxis2); svg.append("g") .append("text") .attr("transform", "translate(" + ytranslation2 + "," + (h/10 - padding[2]) + ")") .text("motorbikes"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js