11
11
12
12
var d3 = require ( 'd3' ) ;
13
13
14
- var Axes = require ( '../../plots/cartesian/axes' ) ;
15
- var Fx = require ( '../../plots/cartesian/graph_interact' ) ;
16
14
var Color = require ( '../../components/color' ) ;
17
15
var Drawing = require ( '../../components/drawing' ) ;
18
16
var Colorscale = require ( '../../components/colorscale' ) ;
@@ -22,42 +20,8 @@ var locationToFeature = require('../../lib/geo_location_utils').locationToFeatur
22
20
var arrayToCalcItem = require ( '../../lib/array_to_calc_item' ) ;
23
21
24
22
var constants = require ( '../../plots/geo/constants' ) ;
25
- var attributes = require ( './attributes' ) ;
26
23
27
- var plotChoropleth = module . exports = { } ;
28
-
29
-
30
- plotChoropleth . calcGeoJSON = function ( trace , topojson ) {
31
- var cdi = [ ] ,
32
- locations = trace . locations ,
33
- len = locations . length ,
34
- features = getTopojsonFeatures ( trace , topojson ) ,
35
- markerLine = ( trace . marker || { } ) . line || { } ;
36
-
37
- var feature ;
38
-
39
- for ( var i = 0 ; i < len ; i ++ ) {
40
- feature = locationToFeature ( trace . locationmode , locations [ i ] , features ) ;
41
-
42
- if ( ! feature ) continue ; // filter the blank features here
43
-
44
- // 'data_array' attributes
45
- feature . z = trace . z [ i ] ;
46
- if ( trace . text !== undefined ) feature . tx = trace . text [ i ] ;
47
-
48
- // 'arrayOk' attributes
49
- arrayToCalcItem ( markerLine . color , feature , 'mlc' , i ) ;
50
- arrayToCalcItem ( markerLine . width , feature , 'mlw' , i ) ;
51
-
52
- cdi . push ( feature ) ;
53
- }
54
-
55
- if ( cdi . length > 0 ) cdi [ 0 ] . trace = trace ;
56
-
57
- return cdi ;
58
- } ;
59
-
60
- plotChoropleth . plot = function ( geo , calcData , geoLayout ) {
24
+ module . exports = function plot ( geo , calcData , geoLayout ) {
61
25
62
26
function keyFunc ( d ) { return d [ 0 ] . trace . uid ; }
63
27
@@ -79,54 +43,20 @@ plotChoropleth.plot = function(geo, calcData, geoLayout) {
79
43
80
44
gChoroplethTraces . each ( function ( calcTrace ) {
81
45
var trace = calcTrace [ 0 ] . trace ,
82
- cdi = plotChoropleth . calcGeoJSON ( trace , geo . topojson ) ,
83
- cleanHoverLabelsFunc = makeCleanHoverLabelsFunc ( geo , trace ) ,
84
- eventDataFunc = makeEventDataFunc ( trace ) ;
85
-
86
- // keep ref to event data in this scope for plotly_unhover
87
- var eventData = null ;
88
-
89
- function handleMouseOver ( pt , ptIndex ) {
90
- if ( ! geo . showHover ) return ;
91
-
92
- var xy = geo . projection ( pt . properties . ct ) ;
93
- cleanHoverLabelsFunc ( pt ) ;
94
-
95
- Fx . loneHover ( {
96
- x : xy [ 0 ] ,
97
- y : xy [ 1 ] ,
98
- name : pt . nameLabel ,
99
- text : pt . textLabel
100
- } , {
101
- container : geo . hoverContainer . node ( )
102
- } ) ;
103
-
104
- eventData = eventDataFunc ( pt , ptIndex ) ;
46
+ cdi = calcGeoJSON ( trace , geo . topojson ) ;
105
47
106
- geo . graphDiv . emit ( 'plotly_hover' , eventData ) ;
107
- }
108
-
109
- function handleClick ( pt , ptIndex ) {
110
- geo . graphDiv . emit ( 'plotly_click' , eventDataFunc ( pt , ptIndex ) ) ;
111
- }
112
-
113
- var paths = d3 . select ( this ) . selectAll ( 'path.choroplethlocation' )
114
- . data ( cdi ) ;
48
+ var paths = d3 . select ( this )
49
+ . selectAll ( 'path.choroplethlocation' )
50
+ . data ( cdi ) ;
115
51
116
52
paths . enter ( ) . append ( 'path' )
117
53
. classed ( 'choroplethlocation' , true )
118
- . on ( 'mouseover' , handleMouseOver )
119
- . on ( 'click' , handleClick )
120
- . on ( 'mouseout' , function ( ) {
121
- Fx . loneUnhover ( geo . hoverContainer ) ;
122
-
123
- geo . graphDiv . emit ( 'plotly_unhover' , eventData ) ;
124
- } )
125
- . on ( 'mousedown' , function ( ) {
126
- // to simulate the 'zoomon' event
127
- Fx . loneUnhover ( geo . hoverContainer ) ;
54
+ . on ( 'mouseover' , function ( pt ) {
55
+ geo . choroplethHoverPt = pt ;
128
56
} )
129
- . on ( 'mouseup' , handleMouseOver ) ; // ~ 'zoomend'
57
+ . on ( 'mouseout' , function ( ) {
58
+ geo . choroplethHoverPt = null ;
59
+ } ) ;
130
60
131
61
paths . exit ( ) . remove ( ) ;
132
62
} ) ;
@@ -141,87 +71,62 @@ plotChoropleth.plot = function(geo, calcData, geoLayout) {
141
71
geo . styleLayer ( gBaseLayerOverChoropleth , layerName , geoLayout ) ;
142
72
}
143
73
144
- plotChoropleth . style ( geo ) ;
74
+ style ( geo ) ;
145
75
} ;
146
76
147
- plotChoropleth . style = function ( geo ) {
148
- geo . framework . selectAll ( 'g.trace.choropleth' )
149
- . each ( function ( calcTrace ) {
150
- var trace = calcTrace [ 0 ] . trace ,
151
- s = d3 . select ( this ) ,
152
- marker = trace . marker || { } ,
153
- markerLine = marker . line || { } ;
154
-
155
- var sclFunc = Colorscale . makeColorScaleFunc (
156
- Colorscale . extractScale (
157
- trace . colorscale ,
158
- trace . zmin ,
159
- trace . zmax
160
- )
161
- ) ;
162
-
163
- s . selectAll ( 'path.choroplethlocation' )
164
- . each ( function ( pt ) {
165
- d3 . select ( this )
166
- . attr ( 'fill' , function ( pt ) { return sclFunc ( pt . z ) ; } )
167
- . call ( Color . stroke , pt . mlc || markerLine . color )
168
- . call ( Drawing . dashLine , '' , pt . mlw || markerLine . width || 0 ) ;
169
- } ) ;
170
- } ) ;
171
- } ;
77
+ function calcGeoJSON ( trace , topojson ) {
78
+ var cdi = [ ] ,
79
+ locations = trace . locations ,
80
+ len = locations . length ,
81
+ features = getTopojsonFeatures ( trace , topojson ) ,
82
+ markerLine = ( trace . marker || { } ) . line || { } ;
172
83
173
- function makeCleanHoverLabelsFunc ( geo , trace ) {
174
- var hoverinfo = trace . hoverinfo ;
84
+ var feature ;
175
85
176
- if ( hoverinfo === 'none' || hoverinfo === 'skip' ) {
177
- return function cleanHoverLabelsFunc ( pt ) {
178
- delete pt . nameLabel ;
179
- delete pt . textLabel ;
180
- } ;
181
- }
86
+ for ( var i = 0 ; i < len ; i ++ ) {
87
+ feature = locationToFeature ( trace . locationmode , locations [ i ] , features ) ;
182
88
183
- var hoverinfoParts = ( hoverinfo === 'all' ) ?
184
- attributes . hoverinfo . flags :
185
- hoverinfo . split ( '+' ) ;
89
+ if ( ! feature ) continue ; // filter the blank features here
186
90
187
- var hasName = ( hoverinfoParts . indexOf ( 'name' ) !== - 1 ) ,
188
- hasLocation = ( hoverinfoParts . indexOf ( 'location' ) !== - 1 ) ,
189
- hasZ = ( hoverinfoParts . indexOf ( 'z' ) !== - 1 ) ,
190
- hasText = ( hoverinfoParts . indexOf ( 'text' ) !== - 1 ) ,
191
- hasIdAsNameLabel = ! hasName && hasLocation ;
91
+ // 'data_array' attributes
92
+ feature . z = trace . z [ i ] ;
93
+ if ( trace . text !== undefined ) feature . tx = trace . text [ i ] ;
192
94
193
- function formatter ( val ) {
194
- var axis = geo . mockAxis ;
195
- return Axes . tickText ( axis , axis . c2l ( val ) , 'hover' ) . text ;
196
- }
95
+ // 'arrayOk' attributes
96
+ arrayToCalcItem ( markerLine . color , feature , 'mlc' , i ) ;
97
+ arrayToCalcItem ( markerLine . width , feature , 'mlw' , i ) ;
197
98
198
- return function cleanHoverLabelsFunc ( pt ) {
199
- // put location id in name label container
200
- // if name isn't part of hoverinfo
201
- var thisText = [ ] ;
99
+ // for event data
100
+ feature . index = i ;
202
101
203
- if ( hasIdAsNameLabel ) pt . nameLabel = pt . id ;
204
- else {
205
- if ( hasName ) pt . nameLabel = trace . name ;
206
- if ( hasLocation ) thisText . push ( pt . id ) ;
207
- }
102
+ cdi . push ( feature ) ;
103
+ }
208
104
209
- if ( hasZ ) thisText . push ( formatter ( pt . z ) ) ;
210
- if ( hasText ) thisText . push ( pt . tx ) ;
105
+ if ( cdi . length > 0 ) cdi [ 0 ] . trace = trace ;
211
106
212
- pt . textLabel = thisText . join ( '<br>' ) ;
213
- } ;
107
+ return cdi ;
214
108
}
215
109
216
- function makeEventDataFunc ( trace ) {
217
- return function ( pt , ptIndex ) {
218
- return { points : [ {
219
- data : trace . _input ,
220
- fullData : trace ,
221
- curveNumber : trace . index ,
222
- pointNumber : ptIndex ,
223
- location : pt . id ,
224
- z : pt . z
225
- } ] } ;
226
- } ;
110
+ function style ( geo ) {
111
+ geo . framework . selectAll ( 'g.trace.choropleth' ) . each ( function ( calcTrace ) {
112
+ var trace = calcTrace [ 0 ] . trace ,
113
+ s = d3 . select ( this ) ,
114
+ marker = trace . marker || { } ,
115
+ markerLine = marker . line || { } ;
116
+
117
+ var sclFunc = Colorscale . makeColorScaleFunc (
118
+ Colorscale . extractScale (
119
+ trace . colorscale ,
120
+ trace . zmin ,
121
+ trace . zmax
122
+ )
123
+ ) ;
124
+
125
+ s . selectAll ( 'path.choroplethlocation' ) . each ( function ( pt ) {
126
+ d3 . select ( this )
127
+ . attr ( 'fill' , function ( pt ) { return sclFunc ( pt . z ) ; } )
128
+ . call ( Color . stroke , pt . mlc || markerLine . color )
129
+ . call ( Drawing . dashLine , '' , pt . mlw || markerLine . width || 0 ) ;
130
+ } ) ;
131
+ } ) ;
227
132
}
0 commit comments