11
11
12
12
var mapboxgl = require ( 'mapbox-gl' ) ;
13
13
14
+ var Fx = require ( '../cartesian/graph_interact' ) ;
14
15
var constants = require ( './constants' ) ;
15
- var xmlnsNamespaces = require ( '../../constants/xmlns_namespaces' ) ;
16
16
17
17
18
18
function Mapbox ( opts ) {
@@ -31,7 +31,8 @@ function Mapbox(opts) {
31
31
32
32
// create framework on instantiation for a smoother first plot call
33
33
this . div = null ;
34
- this . hoverLayer = null ;
34
+ this . xaxis = null ;
35
+ this . yaxis = null ;
35
36
this . createFramework ( fullLayout ) ;
36
37
37
38
this . map = null ;
@@ -70,7 +71,8 @@ proto.plot = function(fullData, fullLayout, promises) {
70
71
71
72
proto . createMap = function ( fullData , fullLayout , resolve ) {
72
73
var self = this ,
73
- opts = this . opts ;
74
+ gd = self . gd ,
75
+ opts = self . opts ;
74
76
75
77
var map = self . map = new mapboxgl . Map ( {
76
78
container : self . div ,
@@ -104,10 +106,28 @@ proto.createMap = function(fullData, fullLayout, resolve) {
104
106
opts . _input . zoom = opts . zoom = map . getZoom ( ) ;
105
107
} ) ;
106
108
109
+ map . on ( 'mousemove' , function ( evt ) {
110
+ var bb = self . div . getBoundingClientRect ( ) ;
111
+
112
+ // some hackery to get Fx.hover to work
113
+
114
+ evt . clientX = evt . point . x + bb . left ;
115
+ evt . clientY = evt . point . y + bb . top ;
107
116
108
- // TODO hover labels
109
- map . on ( 'mousemove' , function ( ) { } ) ;
117
+ evt . target . getBoundingClientRect = function ( ) { return bb ; } ;
110
118
119
+ self . xaxis . p2c = function ( ) { return evt . lngLat . lng ; } ;
120
+ self . yaxis . p2c = function ( ) { return evt . lngLat . lat ; } ;
121
+
122
+ Fx . hover ( gd , evt , self . id ) ;
123
+ } ) ;
124
+
125
+ function unhover ( ) {
126
+ Fx . loneUnhover ( fullLayout . _toppaper ) ;
127
+ }
128
+
129
+ map . on ( 'dragstart' , unhover ) ;
130
+ map . on ( 'zoomstart' , unhover ) ;
111
131
} ;
112
132
113
133
proto . updateMap = function ( fullData , fullLayout , resolve ) {
@@ -185,27 +205,28 @@ proto.updateLayout = function(fullLayout) {
185
205
} ;
186
206
187
207
proto . createFramework = function ( fullLayout ) {
188
- var div = this . div = document . createElement ( 'div' ) ;
208
+ var self = this ;
189
209
190
- div . id = this . uid ;
210
+ var div = self . div = document . createElement ( 'div' ) ;
211
+
212
+ div . id = self . uid ;
191
213
div . style . position = 'absolute' ;
192
214
193
- var hoverLayer = this . hoverLayer = document . createElementNS (
194
- xmlnsNamespaces . svg , 'svg'
195
- ) ;
215
+ self . container . appendChild ( div ) ;
196
216
197
- var hoverStyle = hoverLayer . style ;
217
+ // create mock x/y axes for hover routine
198
218
199
- hoverStyle . position = 'absolute' ;
200
- hoverStyle . top = hoverStyle . left = '0px' ;
201
- hoverStyle . width = hoverStyle . height = '100%' ;
202
- hoverStyle [ 'z-index' ] = 20 ;
203
- hoverStyle [ 'pointer-events' ] = 'none' ;
219
+ self . xaxis = {
220
+ _id : 'x' ,
221
+ c2p : function ( v ) { return self . project ( v ) . x ; }
222
+ } ;
204
223
205
- this . container . appendChild ( div ) ;
206
- this . container . appendChild ( hoverLayer ) ;
224
+ self . yaxis = {
225
+ _id : 'y' ,
226
+ c2p : function ( v ) { return self . project ( v ) . y ; }
227
+ } ;
207
228
208
- this . updateFramework ( fullLayout ) ;
229
+ self . updateFramework ( fullLayout ) ;
209
230
} ;
210
231
211
232
proto . updateFramework = function ( fullLayout ) {
@@ -214,18 +235,23 @@ proto.updateFramework = function(fullLayout) {
214
235
215
236
var style = this . div . style ;
216
237
217
- // Is this correct? It seems to get the map zoom level wrong?
238
+ // TODO Is this correct? It seems to get the map zoom level wrong?
218
239
219
240
style . width = size . w * ( domain . x [ 1 ] - domain . x [ 0 ] ) + 'px' ;
220
241
style . height = size . h * ( domain . y [ 1 ] - domain . y [ 0 ] ) + 'px' ;
221
242
style . left = size . l + domain . x [ 0 ] * size . w + 'px' ;
222
243
style . top = size . t + ( 1 - domain . y [ 1 ] ) * size . h + 'px' ;
244
+
245
+ this . xaxis . _offset = size . l + domain . x [ 0 ] * size . w ;
246
+ this . xaxis . _length = size . w * ( domain . x [ 1 ] - domain . x [ 0 ] ) ;
247
+
248
+ this . yaxis . _offset = size . t + ( 1 - domain . y [ 1 ] ) * size . h ;
249
+ this . yaxis . _length = size . h * ( domain . y [ 1 ] - domain . y [ 0 ] ) ;
223
250
} ;
224
251
225
252
proto . destroy = function ( ) {
226
253
this . map . remove ( ) ;
227
254
this . container . removeChild ( this . div ) ;
228
- this . container . removeChild ( this . hoverLayer ) ;
229
255
} ;
230
256
231
257
proto . toImage = function ( ) {
@@ -255,6 +281,11 @@ proto.createGeoJSONSource = function() {
255
281
return new mapboxgl . GeoJSONSource ( { data : blank } ) ;
256
282
} ;
257
283
284
+ // convenience method to project a [lon, lat] array to pixel coords
285
+ proto . project = function ( v ) {
286
+ return this . map . project ( new mapboxgl . LngLat ( v [ 0 ] , v [ 1 ] ) ) ;
287
+ } ;
288
+
258
289
function convertStyleUrl ( style ) {
259
290
return constants . styleUrlPrefix + style + '-' + constants . styleUrlSuffix ;
260
291
}
0 commit comments