-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.js
231 lines (199 loc) · 6.27 KB
/
b.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/********************
******** 2B ********
********************/
//Width and height
var w = 600;
var h = 300;
var padding = 70;
//Dynamic, random dataset//
var dataset = []; //Initialize empty array
var crimeCount = [];
var numDataPoints = 50; //Number of dummy data points to create
var maxRange = Math.random() * 1000; //Max range of new values
var dataToggler = 1;
// svg and svg variables
var xScale;
var yScale;
var rScale;
var xAxis;
var yAxis;
var svg;
// initiate scatterplot with data//
d3.csv("data/2003T.csv", function(data) {
dataset = data.map(function(d) { return [+d["PROSTITUTION"], +d["VEHICLE_THEFT"], +d["total"], d["PdDistrict"]]; });
d3.csv("data/2015T.csv", function(data) {
dataset2 = data.map(function(d) { return [+d["PROSTITUTION"], +d["VEHICLE_THEFT"], +d["total"], d["PdDistrict"]]; });
xScale = d3.scale.linear()
.domain([0, d3.max([d3.max(dataset, function(d) {
return d[0];
}), d3.max(dataset2, function(d) {
return d[0];
})])])
.range([padding, w - padding * 2]);
yScale = d3.scale.linear()
.domain([0, d3.max([d3.max(dataset, function(d) {
return d[1];
}), d3.max(dataset2, function(d) {
return d[1];
})])])
.range([h - padding, padding]);
rScale = d3.scale.linear()
.domain([0, d3.max([d3.max(dataset, function(d) {
return d[2];
}), d3.max(dataset2, function(d) {
return d[2];
})])])
.range([2, 15]);
xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5);
//Define Y axis
yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Create SVG element
svg = d3.select("#bVis")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create circles
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return rScale(d[2]);
});
svg.selectAll("text") // append labels
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d[3];
})
.attr("x", function(d) {
return xScale(d[0]) + 15;
})
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "13px")
.attr("fill", "black");
//Create X axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
svg.append("text") // text label for the x axis
.attr("x", w / 2)
.attr("y", h)
.style("text-anchor", "middle")
.text("PROSTITUTION");
//Create Y axis
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0)
.attr("x", 0 - (h / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("VEHICLE_THEFT");
});
});
//On click, toggle between datasets//
d3.select("#toggleDate")
.on("click", function() {
if (dataToggler == 1) {
transistionTo2015();
dataToggler = 2;
}
else {
transistionTo2003();
dataToggler = 1;
}
//New values for dataset
});
// transistion to 2015 data
var transistionTo2015 = function() {
d3.csv("data/2015T.csv", function(data) {
dataset = data.map(function(d) { return [+d["PROSTITUTION"], +d["VEHICLE_THEFT"], +d["total"], d["PdDistrict"]]; });
//Update all circles
svg.selectAll("circle")
.data(dataset)
.transition()
.duration(1000)
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return rScale(d[2]);
});
svg.selectAll("text") // append labels
.data(dataset)
.transition()
.duration(1000)
.text(function(d) {
return d[3];
})
.attr("x", function(d) {
return xScale(d[0]) + 15;
})
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "13px")
.attr("fill", "black");
});
}
// transistion to 2003 data//
var transistionTo2003 = function() {
d3.csv("data/2003T.csv", function(data) {
dataset = data.map(function(d) { return [+d["PROSTITUTION"], +d["VEHICLE_THEFT"], +d["total"], d["PdDistrict"]]; });
//Update all circles
svg.selectAll("circle")
.data(dataset)
.transition()
.duration(1000)
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return rScale(d[2]);
});
svg.selectAll("text") // append labels
.data(dataset)
.transition()
.duration(1000)
.text(function(d) {
return d[3];
})
.attr("x", function(d) {
return xScale(d[0]) + 15;
})
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "13px")
.attr("fill", "black");
});
}