-
Notifications
You must be signed in to change notification settings - Fork 0
/
bike_collisions.js
77 lines (66 loc) · 2.27 KB
/
bike_collisions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var m = [19, 20, 20, 19], // top right bottom left margin
w = 960 - m[1] - m[3], // width
h = 136 - m[0] - m[2], // height
z = 17; // cell size
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1"),
format = d3.time.format("%Y-%m-%d");
var color = d3.scale.quantize()
.domain([6, 0])
.range(d3.range(6));
var svg = d3.select("#chart").selectAll("svg")
.data(d3.range(2002, 2011))
.enter().append("svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + (m[3] + (w - z * 53) / 2) + "," + (m[0] + (h - z * 7) / 2) + ")");
svg.append("text")
.attr("transform", "translate(-6," + z * 3.5 + ")rotate(-90)")
.attr("text-anchor", "middle")
.text(String);
var rect = svg.selectAll("rect.day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("rect")
.attr("class", "day")
.attr("width", z)
.attr("height", z)
.attr("x", function(d) { return week(d) * z; })
.attr("y", function(d) { return day(d) * z; })
.datum(format);
rect.append("title")
.text(function(d) { return d; });
svg.selectAll("path.month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("path")
.attr("class", "month")
.attr("d", monthPath);
d3.csv("./data/bike_collisions.csv", function(csv) {
var data = d3.nest()
.key(function(d) { return d.ACCDATE; })
.rollup(function(d) { return sumIncidents(d) })
.map(csv);
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day q" + color(data[d]) + "-6"; })
.select("title")
.text(function(d) { return d + ": " + data[d]; });
});
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * z + "," + d0 * z
+ "H" + w0 * z + "V" + 7 * z
+ "H" + w1 * z + "V" + (d1 + 1) * z
+ "H" + (w1 + 1) * z + "V" + 0
+ "H" + (w0 + 1) * z + "Z";
}
function sumIncidents(data) {
var s = 0;
for (i=0;i<data.length;i++) {
s += 1;
}
return s;
}