-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhisto.html
121 lines (103 loc) · 3.03 KB
/
histo.html
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Time Histogram</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js?2.8.0"></script>
</head>
<body>
<div class="container">
<div id="bar-chart"></div>
<hr>
<footer>
<p>A <a href="http://northernlightsdata.com">Northern Lights Data Lab</a> project</p>
</footer>
</div> <!-- /container -->
<!-- the histogram code -->
<script type="text/javascript">
function drawBars(data) {
// start the bar chart
var padding = 50;
var barWidth = 50;
var width = (barWidth) * data.length;
var height = 400;
// the scales
var x = d3.scale.linear().domain([0, data.length]).range([0, width]);
var y = d3.scale.linear().domain([0, d3.max(data, function(d) { return d.value; })]).rangeRound([0, height]);
// add the canvas to the DOM
var histogram = d3.select("#bar-chart").
append("svg:svg").
attr("width", width).
attr("height", height + padding);
// the rectangles
histogram.selectAll("rect").
data(data).
enter().
append("svg:rect").
attr("x", function(d, i) { return x(i); }).
attr("y", function(d) { return height - y(d.value); }).
attr("height", function(d) { return y(d.value); }).
attr("width", barWidth).
attr("fill", "#2d578b").
attr("stroke", "#000000");
// the bar labels
histogram.selectAll("text").
data(data).
enter().
append("svg:text").
attr("y", function(d) { return height - y(d.value); }).
attr("x", function(d, i) { return x(i); }).
attr("dy", 15).
attr("dx", barWidth/2).
attr("text-anchor", "middle").
text(function(d) { return d.value;}).
attr("fill", "white");
// the category types
histogram.selectAll("text.yAxis").
data(data).
enter().append("svg:text").
attr("x", function(d, i) { return x(i); }).
attr("y", height).
attr("dx", barWidth/2).
attr("text-anchor", "middle").
attr("style", "font-size: 12; font-family: Helvetica, sans-serif").
text(function(d) { return d.key + ":00";}).
attr("transform", "translate(0, 18)").
attr("class", "yAxis");
}
// reads the data from a csv file
d3.csv("./data/bike_collisions.csv", function(csv) {
var data = d3.nest()
.key(function(d) { return convertTime(d.ACCTIME); })
.rollup(function(d) { return sumIncidents(d) })
.map(csv);
// executes the function to write the vis
drawBars(d3.entries(data).slice(0,24));
});
// a function used to sum crashes by hour
// used in the .rollup
function sumIncidents(data) {
var s = 0;
for (i=0;i<data.length;i++) {
s += 1;
}
return s;
}
// Extracts the hour from time of accident
function convertTime(data) {
var hour = '';
if(data.length == 4) {
hour = data.substring(0,2);
} else if (data.length == 3) {
hour = "0" + data.substring(0,1);
} else if (data.length < 3) {
hour = "00";
}
return parseInt(hour, 10);
}
</script>
</body>
</html>