-
Notifications
You must be signed in to change notification settings - Fork 29
/
d3Layer.js
173 lines (145 loc) · 4.68 KB
/
d3Layer.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
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/_base/array",
"dojo/on",
"esri/geometry/Point",
"esri/geometry/webMercatorUtils",
"esri/layers/GraphicsLayer",
"http://d3js.org/d3.v3.min.js"
],
function(
declare,
lang,
array,
on,
Point,
webMercatorUtils,
GraphicsLayer,
d3) {
var d3Layer = declare("d3Layer", [GraphicsLayer], {
constructor: function(url, options) {
var self = this;
this.url = url;
this.type = options.type || 'path';
this.selector = this.type;
if (options.projection) this._project = options.projection;
this._styles = options.styles || [];
this._attrs = options.attrs || [];
this._events = options.events || [];
this._path = options.path || d3.geo.path();
this.path = this._path.projection(lang.hitch(this, self._project));
},
_load: function() {
var self = this;
d3.json(self.url, function(geojson) {
self.geojson = geojson;
self.bounds = d3.geo.bounds(self.geojson);
self.loaded = true;
self._render();
self.onLoad(self);
});
},
//called once the layer's been added to the map
_setMap: function(map, surface) {
this._load();
this._zoomEnd = map.on("zoom-end", lang.hitch(this, function() {
this._reset();
}));
return this.inherited(arguments);
},
_unsetMap: function() {
this.inherited(arguments);
this._zoomEnd.remove();
},
_project: function(x) {
var p = new Point(x[0], x[1]);
var point = this._map.toScreen(webMercatorUtils.geographicToWebMercator(p))
return [point.x, point.y];
},
_render: function() {
var self = this;
var p = this._paths();
if (this.type == 'circle') {
p.data(this.geojson.features)
.enter().append(this.type)
.attr("cx", function(d, i) {
return self._project(d.geometry.coordinates)[0];
})
.attr("cy", function(d, i) {
return self._project(d.geometry.coordinates)[1];
})
.attr('r', 10)
.on('click', function(d) {
self.select(d, this)
})
.on('mouseover', function(d) {
self.hover(d, this);
})
.on('mouseout', function(d) {
self.exit(d, this);
});
} else {
p.data(this.geojson.features)
.enter().append(this.type)
.attr('d', this.path);
}
this._styles.forEach(function(s, i) {
self.style(s);
});
this._attrs.forEach(function(s, i) {
self.attr(s);
});
this._events.forEach(function(s, i) {
self.event(s);
});
// assign a class to each feature element that is the ID of the layer
// this makes it possible to select all primary features, and have secondary ones
this._paths().attr('class', function(d, el) {
return d3.select(this).attr('class') + " " + self.id;
});
// selector needs to respect the layer id classname we just gave each element
this.selector += "." + this.id;
},
style: function(s) {
this._paths().style(s.key, s.value);
},
attr: function(a) {
/* at, 3.9+, this method fires with 'data-suspended' as the sole argument before the graphics have drawn for the first time
it seems like it would be sufficient to check layer.suspended, but it is returning false
https://developers.arcgis.com/javascript/jsapi/layer-amd.html#suspended
*/
if (a != "data-suspended" || this.suspended) {
this._paths().attr(a.key, a.value);
}
return this.inherited(arguments);
},
event: function(e) {
this._paths().on(e.type, e.fn);
},
_reset: function() {
var self = this;
if (this.type == 'circle') {
this._paths()
.attr("cx", function(d, i) {
return self._project(d.geometry.coordinates)[0];
})
.attr("cy", function(d, i) {
return self._project(d.geometry.coordinates)[1];
})
} else {
this._paths().attr('d', this.path)
}
},
_element: function() {
return d3.select("g#" + this.id + "_layer");
},
_paths: function(selector) {
return this._element().selectAll(selector || this.selector);
},
hover: function() {},
exit: function() {},
select: function() {}
});
return d3Layer;
});