-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-map.js
211 lines (171 loc) · 6.34 KB
/
main-map.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
makeMap();
function makeMap() {
// mapid is the id of the div where the map will appear
var map = L
.map('mapid')
.setView([40.730610, -73.935242], 11); // center position + zoom
// Add background to map (many diff options: https://leaflet-extras.github.io/leaflet-providers/preview/)
L.tileLayer(
'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
// Add a svg layer to the map
L.svg().addTo(map);
// Create color palettes
var violation = {
true: "red",
false: "green"
}
// Plot map + parse data
d3.json("hs-severity.json", function(data) {
d3.select("#mapid")
.select("svg")
.selectAll("myCircle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d){ return map.latLngToLayerPoint([d.la, d.lo]).x })
.attr("cy", function(d){ return map.latLngToLayerPoint([d.la, d.lo]).y })
.attr("r", function(d) {
if(d.s == 0) {
return (d.s /3) + 5;
}
else {
return (d.s ** (1/3)) * 4;
}
}) // change this to correlate with severity
.style("fill", function(d) {
return violation[d.v];
})
.attr("stroke", false)
.attr("fill-opacity", .8)
// data parsing
var parseMonthDayYear = d3.timeFormat("%d-%b"); // d3.utcParse() // fix this
var dataForTimeline = [],
dateToCrimeCount = {};
data.forEach(function(d, idx) {
d.a = parseMonthDayYear(new Date(d.a)) ;
// d.created_at = parseMonthDayYear(d.created_at);
if (!dateToCrimeCount[d.a]) {
var dataCount = [d.t, 1];
dateToCrimeCount[d.a] = dataCount;
}
dateToCrimeCount[d.a][0] += d.t;
dateToCrimeCount[d.a][1] += 1;
});
Object.keys(dateToCrimeCount).forEach(function(time) {
dateToCrimeCount[time] = dateToCrimeCount[time][0] / dateToCrimeCount[time][1];
dataForTimeline.push({ TIME: new Date(time), TEMP: dateToCrimeCount[time] });
});
dataForTimeline.sort(function(a,b) { return a.TIME - b.TIME; });
makeTimeline(data, dataForTimeline);
//makeLegend();
})
// If the user change the map (zoom or drag), update circle position:
map.on("moveend", update)
}
// Creates the event timeline and sets up callbacks for brush changes
function makeTimeline(dataForMap, dataForTimeline) {
var margin = { top: 10, right: 10, bottom: 20, left: 25 },
width = 600 - margin.left - margin.right,
height = 80 - margin.top - margin.bottom;
var timelineSvg = d3.select("#timeline-container").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var timeline = timelineSvg.append("g")
.attr("class", "timeline")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleTime()
.domain(d3.extent(dataForTimeline.map(function(d) { return d.TIME; })))
.range([0, width]);
var y = d3.scaleLinear()
.domain(d3.extent(dataForTimeline.map(function(d) { return d.TEMP; })))
.range([height, 0]);
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
var area = d3.area()
.x(function(d) { return x(d.TIME); })
.y0(height)
.y1(function(d) { return y(d.TEMP); });
timeline.append("path")
.datum(dataForTimeline)
.attr("class", "area")
.attr("d", area);
timeline.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
timeline.append("g")
.attr("class", "y axis")
.call(yAxis);
timeline.append("text")
.attr("transform", "rotate(-90)")
.attr("dy", "1em")
.style("text-anchor", "end")
.text("Temperature");
// Add brush to timeline, hook up to callback
var brush = d3.brushX()
.on("brush", function() { brushCallback(brush, dataForMap); });
timeline.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.append("rect")
.attr("y", -6)
.attr("height", height + 7);
//console.log(d3.)
//brush.event(timeline.select('g.x.brush')); // dispatches a single brush event
}
// this part isn't working
function brushCallback(brush, dataForMap) {
if (d3.event.selection == null) {
updateMapPoints([]);
} else {
var newDateRange = brush.extent(),
filteredData = [];
dataForMap.forEach(function(d) {
if (d.TIME >= newDateRange[0] && d.TIME <= newDateRange[1]) {
filteredData.push(d);
}
});
updateMapPoints(filteredData);
console.log(filteredData);
}
}
// Updates the points displayed on the map, to those in the passed data array
function updateMapPoints(data) {
/*
var circles = mapSvg.selectAll("circle").data(data, function(d) { return d.TIME + d.TOT; });
circles // update existing points
.on("mouseover", tipMouseover)
.on("mouseout", tipMouseout)
.attr("fill", function(d) { return colorScale(d.CR); })
.attr("cx", function(d) { return projection([+d.longitude, +d.latitude])[0]; })
.attr("cy", function(d) { return projection([+d.longitude, +d.latitude])[1]; })
.attr("r", function(d) { return radiusScale(+d.TOT); });
circles.enter().append("circle") // new entering points
.on("mouseover", tipMouseover)
.on("mouseout", tipMouseout)
.attr("fill", function(d) { return colorScale(d.CR); })
.attr("cx", function(d) { return projection([+d.longitude, +d.latitude])[0]; })
.attr("cy", function(d) { return projection([+d.longitude, +d.latitude])[1]; })
.attr("r", 0)
.transition()
.duration(500)
.attr("r", function(d) { return radiusScale(+d.TOT); });
circles.exit() // exiting points
.attr("r", function(d) { return radiusScale(+d.TOT); })
.transition()
.duration(500)
.attr("r", 0).remove();
*/
};
function update() {
d3.selectAll("circle")
.attr("cx", function(d){ return map.latLngToLayerPoint([d.la, d.lo]).x })
.attr("cy", function(d){ return map.latLngToLayerPoint([d.la, d.lo]).y })
};