@@ -59,46 +59,49 @@ plotChoropleth.calcGeoJSON = function(trace, topojson) {
5959
6060plotChoropleth . plot = function ( geo , choroplethData , geoLayout ) {
6161 var framework = geo . framework ,
62- topojson = geo . topojson ,
6362 gChoropleth = framework . select ( 'g.choroplethlayer' ) ,
6463 gBaseLayer = framework . select ( 'g.baselayer' ) ,
6564 gBaseLayerOverChoropleth = framework . select ( 'g.baselayeroverchoropleth' ) ,
6665 baseLayersOverChoropleth = constants . baseLayersOverChoropleth ,
6766 layerName ;
6867
69- // TODO move to more d3-idiomatic pattern (that's work on replot)
70- // N.B. html('') does not work in IE11
71- gChoropleth . selectAll ( '*' ) . remove ( ) ;
72- gBaseLayerOverChoropleth . selectAll ( '*' ) . remove ( ) ;
73-
7468 var gChoroplethTraces = gChoropleth
75- . selectAll ( 'g.trace.scatter ' )
69+ . selectAll ( 'g.trace.choropleth ' )
7670 . data ( choroplethData ) ;
7771
7872 gChoroplethTraces . enter ( ) . append ( 'g' )
79- . attr ( 'class' , 'trace choropleth' ) ;
73+ . attr ( 'class' , 'trace choropleth' ) ;
74+
75+ gChoroplethTraces . exit ( ) . remove ( ) ;
8076
8177 gChoroplethTraces
8278 . each ( function ( trace ) {
8379 if ( trace . visible !== true ) return ;
8480
85- var cdi = plotChoropleth . calcGeoJSON ( trace , topojson ) ,
86- cleanHoverLabelsFunc = makeCleanHoverLabelsFunc ( geo , trace ) ;
81+ var cdi = plotChoropleth . calcGeoJSON ( trace , geo . topojson ) ,
82+ cleanHoverLabelsFunc = makeCleanHoverLabelsFunc ( geo , trace ) ,
83+ eventDataFunc = makeEventDataFunc ( trace ) ;
8784
88- function handleMouseOver ( d ) {
85+ function handleMouseOver ( pt , ptIndex ) {
8986 if ( ! geo . showHover ) return ;
9087
91- var xy = geo . projection ( d . properties . ct ) ;
92- cleanHoverLabelsFunc ( d ) ;
88+ var xy = geo . projection ( pt . properties . ct ) ;
89+ cleanHoverLabelsFunc ( pt ) ;
9390
9491 Plotly . Fx . loneHover ( {
9592 x : xy [ 0 ] ,
9693 y : xy [ 1 ] ,
97- name : d . nameLabel ,
98- text : d . textLabel
94+ name : pt . nameLabel ,
95+ text : pt . textLabel
9996 } , {
10097 container : geo . hoverContainer . node ( )
10198 } ) ;
99+
100+ geo . graphDiv . emit ( 'plotly_hover' , eventDataFunc ( pt , ptIndex ) ) ;
101+ }
102+
103+ function handleClick ( pt , ptIndex ) {
104+ geo . graphDiv . emit ( 'plotly_click' , eventDataFunc ( pt , ptIndex ) ) ;
102105 }
103106
104107 d3 . select ( this )
@@ -107,6 +110,7 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
107110 . enter ( ) . append ( 'path' )
108111 . attr ( 'class' , 'choroplethlocation' )
109112 . on ( 'mouseover' , handleMouseOver )
113+ . on ( 'click' , handleClick )
110114 . on ( 'mouseout' , function ( ) {
111115 Plotly . Fx . loneUnhover ( geo . hoverContainer ) ;
112116 } )
@@ -118,6 +122,8 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
118122 } ) ;
119123
120124 // some baselayers are drawn over choropleth
125+ gBaseLayerOverChoropleth . selectAll ( '*' ) . remove ( ) ;
126+
121127 for ( var i = 0 ; i < baseLayersOverChoropleth . length ; i ++ ) {
122128 layerName = baseLayersOverChoropleth [ i ] ;
123129 gBaseLayer . select ( 'g.' + layerName ) . remove ( ) ;
@@ -140,11 +146,11 @@ plotChoropleth.style = function(geo) {
140146 sclFunc = makeScaleFunction ( scl , zmin , zmax ) ;
141147
142148 s . selectAll ( 'path.choroplethlocation' )
143- . each ( function ( d ) {
149+ . each ( function ( pt ) {
144150 d3 . select ( this )
145- . attr ( 'fill' , function ( d ) { return sclFunc ( d . z ) ; } )
146- . call ( Color . stroke , d . mlc || markerLine . color )
147- . call ( Drawing . dashLine , '' , d . mlw || markerLine . width ) ;
151+ . attr ( 'fill' , function ( pt ) { return sclFunc ( pt . z ) ; } )
152+ . call ( Color . stroke , pt . mlc || markerLine . color )
153+ . call ( Drawing . dashLine , '' , pt . mlw || markerLine . width ) ;
148154 } ) ;
149155 } ) ;
150156} ;
@@ -153,9 +159,9 @@ function makeCleanHoverLabelsFunc(geo, trace) {
153159 var hoverinfo = trace . hoverinfo ;
154160
155161 if ( hoverinfo === 'none' ) {
156- return function cleanHoverLabelsFunc ( d ) {
157- delete d . nameLabel ;
158- delete d . textLabel ;
162+ return function cleanHoverLabelsFunc ( pt ) {
163+ delete pt . nameLabel ;
164+ delete pt . textLabel ;
159165 } ;
160166 }
161167
@@ -174,20 +180,33 @@ function makeCleanHoverLabelsFunc(geo, trace) {
174180 return Plotly . Axes . tickText ( axis , axis . c2l ( val ) , 'hover' ) . text ;
175181 }
176182
177- return function cleanHoverLabelsFunc ( d ) {
183+ return function cleanHoverLabelsFunc ( pt ) {
178184 // put location id in name label container
179185 // if name isn't part of hoverinfo
180186 var thisText = [ ] ;
181187
182- if ( hasIdAsNameLabel ) d . nameLabel = d . id ;
188+ if ( hasIdAsNameLabel ) pt . nameLabel = pt . id ;
183189 else {
184- if ( hasName ) d . nameLabel = trace . name ;
185- if ( hasLocation ) thisText . push ( d . id ) ;
190+ if ( hasName ) pt . nameLabel = trace . name ;
191+ if ( hasLocation ) thisText . push ( pt . id ) ;
186192 }
187193
188- if ( hasZ ) thisText . push ( formatter ( d . z ) ) ;
189- if ( hasText ) thisText . push ( d . tx ) ;
194+ if ( hasZ ) thisText . push ( formatter ( pt . z ) ) ;
195+ if ( hasText ) thisText . push ( pt . tx ) ;
196+
197+ pt . textLabel = thisText . join ( '<br>' ) ;
198+ } ;
199+ }
190200
191- d . textLabel = thisText . join ( '<br>' ) ;
201+ function makeEventDataFunc ( trace ) {
202+ return function ( pt , ptIndex ) {
203+ return { points : [ {
204+ data : trace . _input ,
205+ fullData : trace ,
206+ curveNumber : trace . index ,
207+ pointNumber : ptIndex ,
208+ location : pt . id ,
209+ z : pt . z
210+ } ] } ;
192211 } ;
193212}
0 commit comments