forked from bricedev's block: Traffic flow London
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.year {
font: 500 196px "Helvetica Neue";
fill: #aaa;
}
.overlay {
fill: none;
pointer-events: all;
cursor: ew-resize;
}
.q0-9 { fill:rgb(247,251,255); }
.q1-9 { fill:rgb(222,235,247); }
.q2-9 { fill:rgb(198,219,239); }
.q3-9 { fill:rgb(158,202,225); }
.q4-9 { fill:rgb(107,174,214); }
.q5-9 { fill:rgb(66,146,198); }
.q6-9 { fill:rgb(33,113,181); }
.q7-9 { fill:rgb(8,81,156); }
.q8-9 { fill:rgb(8,48,107); }
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script>
var width = 960,
height = 700;
var quantile = d3.scale.quantile()
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var path = d3.geo.path()
.projection(null);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var label = svg.append("text")
.attr("class", "year")
.attr("text-anchor", "middle")
.attr("y", height - 24)
.attr("x", width/2)
.text(1993);
var box = label.node().getBBox();
var yearScale = d3.scale.linear()
.domain([1993,2014])
.range([box.x + 10, box.x + box.width - 10])
.clamp(true);
var yearDomain = d3.range(1993,2014);
queue()
.defer(d3.json, "borough.json")
.defer(d3.csv, "flows.csv")
.await(ready);
function ready(error, borough,flows) {
if (error) throw error;
quantileDomain = [];
flows.forEach(function(d){
yearDomain.forEach(function (year) {
quantileDomain.push(+d[year]);
});
});
quantileDomain.sort(d3.ascending);
quantile.domain(quantileDomain);
var borough = svg.append("g")
.selectAll("path")
.attr("class", "borough")
.data(topojson.feature(borough, borough.objects.borough).features)
.enter().append("path")
.attr("class", function(d) { return quantile(flows.filter(function(flows) { return d.properties.code===flows["LA Code"]; })[0]["1993"]); })
.style("stroke","black")
.style("stroke-width",".5px")
.attr("d", path)
borough.append("title")
.text(function(d) { return d.properties.name; });
var overlay = svg.append("rect")
.attr("class", "overlay")
.attr("x", box.x)
.attr("y", box.y)
.attr("width", box.width)
.attr("height", box.height)
.on("mouseover", enableInteraction);
svg.transition()
.duration(15000)
.ease("linear")
.tween("year", tweenYear)
.each("end", enableInteraction);
function tweenYear() {
var year = d3.interpolateNumber(1993, 2014);
return function(t) { displayYear(year(t)); };
};
function displayYear(year) {
label.text(Math.round(year));
borough.attr("class",function(d) { return quantile(flows.filter(function(flows) { return d.properties.code===flows["LA Code"]; })[0][Math.round(year)]); })
};
function enableInteraction() {
svg.transition().duration(0);
overlay
.on("mousemove", mousemove)
.on("touchmove", mousemove);
function mousemove() {
displayYear(Math.round(yearScale.invert(d3.mouse(this)[0])));
}
};
};
d3.select(self.frameElement).style("height", height + "px");
</script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js
https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js