Skip to content

Commit f99c366

Browse files
committed
fire hover and click events on geo maps
1 parent e8ebcb2 commit f99c366

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/plots/geo/geo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var topojsonFeature = require('topojson').feature;
3333
function Geo(options, fullLayout) {
3434

3535
this.id = options.id;
36+
this.graphDiv = options.graphDiv;
3637
this.container = options.container;
3738
this.topojsonURL = options.topojsonURL;
3839

src/plots/geo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ exports.plot = function plotGeo(gd) {
5252
if(geo === undefined) {
5353
geo = new Geo({
5454
id: geoId,
55+
graphDiv: gd,
5556
container: fullLayout._geocontainer.node(),
5657
topojsonURL: gd._context.topojsonURL
5758
},

src/traces/choropleth/plot.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
7979
if(trace.visible !== true) return;
8080

8181
var cdi = plotChoropleth.calcGeoJSON(trace, geo.topojson),
82-
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace);
82+
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace),
83+
eventDataFunc = makeEventDataFunc(trace);
8384

8485
function handleMouseOver(pt, ptIndex) {
8586
if(!geo.showHover) return;
@@ -95,6 +96,12 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
9596
}, {
9697
container: geo.hoverContainer.node()
9798
});
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));
98105
}
99106

100107
d3.select(this)
@@ -103,6 +110,7 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
103110
.enter().append('path')
104111
.attr('class', 'choroplethlocation')
105112
.on('mouseover', handleMouseOver)
113+
.on('click', handleClick)
106114
.on('mouseout', function() {
107115
Plotly.Fx.loneUnhover(geo.hoverContainer);
108116
})
@@ -187,6 +195,18 @@ function makeCleanHoverLabelsFunc(geo, trace) {
187195
if(hasText) thisText.push(pt.tx);
188196

189197
pt.textLabel = thisText.join('<br>');
198+
};
199+
}
190200

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+
}]};
191211
};
192212
}

src/traces/scattergeo/plot.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ plotScatterGeo.plot = function(geo, scattergeoData) {
148148
}
149149

150150
var cdi = plotScatterGeo.calcGeoJSON(trace, geo.topojson),
151-
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace);
151+
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace),
152+
eventDataFunc = makeEventDataFunc(trace);
152153

153154
var hoverinfo = trace.hoverinfo,
154155
hasNameLabel = (
@@ -171,6 +172,12 @@ plotScatterGeo.plot = function(geo, scattergeoData) {
171172
}, {
172173
container: geo.hoverContainer.node()
173174
});
175+
176+
geo.graphDiv.emit('plotly_hover', eventDataFunc(pt, ptIndex));
177+
}
178+
179+
function handleClick(pt, ptIndex) {
180+
geo.graphDiv.emit('plotly_click', eventDataFunc(pt, ptIndex));
174181
}
175182

176183
if(showMarkers) {
@@ -179,6 +186,7 @@ plotScatterGeo.plot = function(geo, scattergeoData) {
179186
.enter().append('path')
180187
.attr('class', 'point')
181188
.on('mouseover', handleMouseOver)
189+
.on('click', handleClick)
182190
.on('mouseout', function() {
183191
Fx.loneUnhover(geo.hoverContainer);
184192
})
@@ -263,6 +271,21 @@ function makeCleanHoverLabelsFunc(geo, trace) {
263271
if(hasText) thisText.push(pt.tx || trace.text);
264272

265273
pt.textLabel = thisText.join('<br>');
274+
};
275+
}
266276

277+
function makeEventDataFunc(trace) {
278+
var hasLocation = Array.isArray(trace.locations);
279+
280+
return function(pt, ptIndex) {
281+
return {points: [{
282+
data: trace._input,
283+
fullData: trace,
284+
curveNumber: trace.index,
285+
pointNumber: ptIndex,
286+
lon: pt.lon,
287+
lat: pt.lat,
288+
location: hasLocation ? pt.location : null
289+
}]};
267290
};
268291
}

0 commit comments

Comments
 (0)