/
index.html
129 lines (107 loc) · 3.24 KB
/
index.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
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: orange;
}
.bar:hover {
fill: lightpink;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</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/moment.js/2.10.6/moment.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
var svg = 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("https://data.cityofnewyork.us/resource/u8ac-289r.csv?$where=created_date<'2015-01-01T00:00:00' AND created_date>'2014-11-30T00:00:00' &$select=created_date", function(error, data) {
if (error) throw error;
var week = [0, 0, 0, 0, 0, 0, 0]; // initializes seven values for seven days of week
data.forEach(function (d) {
if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==0) {
week[0]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==1) {
week[1]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==2) {
week[2]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==3) {
week[3]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==4) {
week[4]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==5) {
week[5]++;
}
else if (moment(d["CREATED_DATE"], "M/DD/YYYY hh:mm:ss a").day()==6) {
week[6]++;
}
});
console.log("week:");
for (index = 0; index < week.length; index++) {
console.log(week[index]);
}
x.domain(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]);
console.log("most complaints in a day:", Math.max.apply(Math, week));
y.domain([0, Math.max.apply(Math, week)]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of GraffitiComplaints");
svg.selectAll(".bar")
.data(week)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d,i) {
return x(x.domain()[i]);
})
.attr("width", x.rangeBand())
.attr("y", y)
.attr("height", function(d) { return height - y(d); });
});
//function type(d) {
//d.frequency = +d.frequency;
//return d;
//}
</script>