forked from chelm/esri-d3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3Layer.js
139 lines (111 loc) · 3.66 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
dojo.provide("modules.d3Layer");
dojo.require("esri.layers.graphics");
dojo.declare("modules.d3Layer", esri.layers.GraphicsLayer, {
constructor: function(url, options) {
var self = this;
this.inherited(arguments);
this.url = url;
//this.id = options.id || ( Math.round( Math.random() * 100000 ).toString( 16 ) ) + ( new Date() ).getTime().toString(16);
this.type = options.type || 'path';
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( self._project );
// load features
this._load();
},
_load: function(){
var self = this;
d3.json( this.url, function( geojson ){
self.geojson = geojson;
self.bounds = d3.geo.bounds( self.geojson );
self.loaded = true;
// TODO the onLoad event fires too soon, have to wait until the DOM is created
setTimeout(function(){
self.onLoad( self );
}, 1000);
});
},
_bind: function(map){
this._connects = [];
this._connects.push( dojo.connect( this._map, "onZoomEnd", this, this._reset ) );
},
_project: function(x){
var p = new esri.geometry.Point( x[0], x[1] );
var point = map.toScreen( esri.geometry.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('class', this.id)
.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('class', this.id)
.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);
});
this._bind();
},
style: function( s ){
this._paths().style(s.key, s.value);
},
attr: function( a ){
if (a.key == "class"){
this._paths().attr('class', function(d) {
var val = d3.select(this).attr('class') + " " + a.value;
return val;
});
} else {
this._paths().attr(a.key, a.value);
}
},
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(){
return this._element().selectAll( this.type+"."+this.id );
},
hover: function() {},
exit: function() {},
select: function() {}
});