This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from yurukov/dc.leaflet.js
-
Notifications
You must be signed in to change notification settings - Fork 24
/
markerChart.js
325 lines (284 loc) · 8.61 KB
/
markerChart.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
dc_leaflet.markerChart = function(parent, chartGroup) {
var _chart = dc_leaflet.leafletBase(dc.MarginMixin);
var _renderPopup = true;
var _cluster = false; // requires leaflet.markerCluster
var _clusterOptions=false;
var _rebuildMarkers = false;
var _brushOn = true;
var _filterByArea = false;
var _clickEvent = false;
var _featureGroup = false;
var _filter;
var _innerFilter=false;
var _zooming=false;
var _layerGroup = false;
var _markerList = [];
var _currentGroups=false;
var _fitOnRender = true;
var _fitOnRedraw = false;
var _disableFitOnRedraw = false;
var _showMarkerTitle = true;
var _location = function(d) {
return _chart.keyAccessor()(d);
};
var _marker = function(d, map) {
var marker = new L.Marker(_chart.toLocArray(_chart.locationAccessor()(d)), {
title: _showMarkerTitle ? _chart.title()(d) : '',
alt: _showMarkerTitle ? _chart.title()(d) : '',
icon: _icon(d, map),
clickable: _chart.renderPopup() || (_chart.brushOn() && !_filterByArea),
draggable: false
});
return marker;
};
var _icon = function(d, map) {
return new L.Icon.Default();
};
var _popup = function(d, marker) {
return _chart.title()(d);
};
_chart._postRender = function() {
if (_chart.brushOn()) {
if (_filterByArea) {
_chart.filterHandler(doFilterByArea);
}
_chart.map().on('zoomend moveend', zoomFilter, this );
if (!_filterByArea)
_chart.map().on('click', zoomFilter, this );
_chart.map().on('zoomstart', zoomStart, this);
}
if (_cluster) {
_layerGroup = new L.MarkerClusterGroup(_clusterOptions?_clusterOptions:null);
}
else {
_layerGroup = new L.LayerGroup();
}
_chart.map().addLayer(_layerGroup);
};
_chart._doRedraw = function() {
var groups = _chart._computeOrderedGroups(_chart.data()).filter(function (d) {
return _chart.valueAccessor()(d) !== 0;
});
if (_currentGroups && _currentGroups.toString() === groups.toString()) {
return;
}
_currentGroups=groups;
if (_rebuildMarkers) {
_markerList=[];
}
_layerGroup.clearLayers();
var addList=[];
_featureGroup = false;
groups.forEach(function(v, i) {
var key = _chart.keyAccessor()(v);
var marker = null;
if (!_rebuildMarkers && key in _markerList) {
marker = _markerList[key];
}
else {
marker = createmarker(v, key);
}
if (!_chart.cluster()) {
_layerGroup.addLayer(marker);
}
else {
addList.push(marker);
}
});
if (_chart.cluster() && addList.length > 0) {
_layerGroup.addLayers(addList);
}
if (addList.length > 0) {
if (_fitOnRender || (_fitOnRedraw && !_disableFitOnRedraw)) {
_featureGroup = new L.featureGroup(addList);
_chart.map().fitBounds(_featureGroup.getBounds());
}
}
_disableFitOnRedraw = false;
_fitOnRender = false;
};
_chart.locationAccessor = function(_) {
if (!arguments.length) {
return _location;
}
_location= _;
return _chart;
};
_chart.marker = function(_) {
if (!arguments.length) {
return _marker;
}
_marker= _;
return _chart;
};
_chart.featureGroup = function(_) {
if (!arguments.length) {
return _featureGroup;
}
_featureGroup= _;
return _chart;
};
_chart.icon = function(_) {
if (!arguments.length) {
return _icon;
}
_icon= _;
return _chart;
};
_chart.popup = function(_) {
if (!arguments.length) {
return _popup;
}
_popup= _;
return _chart;
};
_chart.clickEvent = function(_) {
if (!arguments.length) {
return _clickEvent;
}
_clickEvent= _;
return _chart;
};
_chart.renderPopup = function(_) {
if (!arguments.length) {
return _renderPopup;
}
_renderPopup = _;
return _chart;
};
_chart.cluster = function(_) {
if (!arguments.length) {
return _cluster;
}
_cluster = _;
return _chart;
};
_chart.clusterOptions = function(_) {
if (!arguments.length) {
return _clusterOptions;
}
_clusterOptions = _;
return _chart;
};
_chart.rebuildMarkers = function(_) {
if (!arguments.length) {
return _rebuildMarkers;
}
_rebuildMarkers = _;
return _chart;
};
_chart.brushOn = function(_) {
if (!arguments.length) {
return _brushOn;
}
_brushOn = _;
return _chart;
};
_chart.filterByArea = function(_) {
if (!arguments.length) {
return _filterByArea;
}
_filterByArea = _;
return _chart;
};
_chart.fitOnRender = function(_) {
if (!arguments.length) {
return _fitOnRender;
}
_fitOnRender = _;
return _chart;
};
_chart.fitOnRedraw = function(_) {
if (!arguments.length) {
return _fitOnRedraw;
}
_fitOnRedraw = _;
return _chart;
};
_chart.showMarkerTitle = function(_) {
if (!arguments.length) {
return _showMarkerTitle;
}
_showMarkerTitle = _;
return _chart;
};
_chart.markerGroup = function() {
return _layerGroup;
};
var createmarker = function(v, k) {
var marker = _marker(v, _chart.map());
marker.key = k;
if (_chart.renderPopup()) {
_chart.bindPopupWithMod(marker, _chart.popup()(v, marker));
}
if (_chart.brushOn() && !_filterByArea) {
marker.on("click", selectFilter);
}
if (_clickEvent)
marker.on("click", _clickEvent)
_markerList[k]=marker;
return marker;
};
var zoomStart = function(e) {
_zooming=true;
};
var zoomFilter = function(e) {
if (e.type === "moveend" && (_zooming || e.hard)) {
return;
}
_zooming=false;
if (_filterByArea) {
var filter;
if (_chart.map().getCenter().equals(_chart.center()) && _chart.map().getZoom() === _chart.zoom()) {
filter = null;
}
else {
filter = _chart.map().getBounds();
}
dc.events.trigger(function () {
_chart.filter(null);
if (filter) {
_innerFilter=true;
_chart.filter(filter);
_innerFilter=false;
}
dc.redrawAll(_chart.chartGroup());
});
} else if (_chart.filter() && (e.type === "click" ||
(_markerList.indexOf(_chart.filter()) !== -1 &&
!_chart.map().getBounds().contains(_markerList[_chart.filter()].getLatLng())))) {
dc.events.trigger(function () {
_chart.filter(null);
if (_renderPopup) {
_chart.map().closePopup();
}
dc.redrawAll(_chart.chartGroup());
});
}
};
var doFilterByArea = function(dimension, filters) {
_chart.dimension().filter(null);
if (filters && filters.length>0) {
_chart.dimension().filterFunction(function(d) {
if (!(d in _markerList)) {
return false;
}
var locO = _markerList[d].getLatLng();
return locO && filters[0].contains(locO);
});
if (!_innerFilter && _chart.map().getBounds().toString !== filters[0].toString()) {
_chart.map().fitBounds(filters[0]);
}
}
};
var selectFilter = function(e) {
if (!e.target) return;
if (!_chart.modKeyMatches(e, _chart.filterMod())) return;
var filter = e.target.key;
dc.events.trigger(function () {
_chart.filter(filter);
dc.redrawAll(_chart.chartGroup());
});
};
return _chart.anchor(parent, chartGroup);
};