Skip to content

Commit

Permalink
Merge pull request #1004 from plotly/geo-line-picking
Browse files Browse the repository at this point in the history
Multiple scattergeo improvement
  • Loading branch information
etpinard committed Oct 6, 2016
2 parents 987f1aa + 510cb60 commit f058e8b
Show file tree
Hide file tree
Showing 30 changed files with 865 additions and 388 deletions.
2 changes: 1 addition & 1 deletion src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ function handleGeo(gd, ev) {
geoIds = Plots.getSubplotIds(fullLayout, 'geo');

for(var i = 0; i < geoIds.length; i++) {
var geo = fullLayout[geoIds[i]]._geo;
var geo = fullLayout[geoIds[i]]._subplot;

if(attr === 'zoom') {
var scale = geo.projection.scale();
Expand Down
130 changes: 130 additions & 0 deletions src/lib/geojson_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

/**
* Convert calcTrace to GeoJSON 'MultiLineString' coordinate arrays
*
* @param {object} calcTrace
* gd.calcdata item.
* Note that calcTrace[i].lonlat is assumed to be defined
*
* @return {array}
* return line coords array (or array of arrays)
*
*/
exports.calcTraceToLineCoords = function(calcTrace) {
var trace = calcTrace[0].trace,
connectgaps = trace.connectgaps;

var coords = [],
lineString = [];

for(var i = 0; i < calcTrace.length; i++) {
var calcPt = calcTrace[i];

lineString.push(calcPt.lonlat);

if(!connectgaps && calcPt.gapAfter && lineString.length > 0) {
coords.push(lineString);
lineString = [];
}
}

coords.push(lineString);

return coords;
};


/**
* Make line ('LineString' or 'MultiLineString') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*
*/
exports.makeLine = function(coords, trace) {
var out = {};

if(coords.length === 1) {
out = {
type: 'LineString',
coordinates: coords[0]
};
}
else {
out = {
type: 'MultiLineString',
coordinates: coords
};
}

if(trace) out.trace = trace;

return out;
};

/**
* Make polygon ('Polygon' or 'MultiPolygon') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*/
exports.makePolygon = function(coords, trace) {
var out = {};

if(coords.length === 1) {
out = {
type: 'Polygon',
coordinates: coords
};
}
else {
var _coords = new Array(coords.length);

for(var i = 0; i < coords.length; i++) {
_coords[i] = [coords[i]];
}

out = {
type: 'MultiPolygon',
coordinates: _coords
};
}

if(trace) out.trace = trace;

return out;
};

/**
* Make blank GeoJSON
*
* @return {object}
* Blank GeoJSON object
*
*/
exports.makeBlank = function() {
return {
type: 'Point',
coordinates: []
};
};
2 changes: 1 addition & 1 deletion src/plot_api/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ exports.doModeBar = function(gd) {

subplotIds = Plots.getSubplotIds(fullLayout, 'geo');
for(i = 0; i < subplotIds.length; i++) {
var geo = fullLayout[subplotIds[i]]._geo;
var geo = fullLayout[subplotIds[i]]._subplot;
geo.updateFx(fullLayout.hovermode);
}

Expand Down
39 changes: 25 additions & 14 deletions src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ function hover(gd, evt, subplot) {
curvenum,
cd,
trace,
subplotId,
subploti,
mode,
xval,
Expand Down Expand Up @@ -468,7 +469,8 @@ function hover(gd, evt, subplot) {
if(!cd || !cd[0] || !cd[0].trace || cd[0].trace.visible !== true) continue;

trace = cd[0].trace;
subploti = subplots.indexOf(getSubplot(trace));
subplotId = getSubplot(trace);
subploti = subplots.indexOf(subplotId);

// within one trace mode can sometimes be overridden
mode = hovermode;
Expand All @@ -495,6 +497,11 @@ function hover(gd, evt, subplot) {
text: undefined
};

// add ref to subplot object (non-cartesian case)
if(fullLayout[subplotId]) {
pointData.subplot = fullLayout[subplotId]._subplot;
}

closedataPreviousLength = hoverData.length;

// for a highlighting array, figure out what
Expand Down Expand Up @@ -545,7 +552,6 @@ function hover(gd, evt, subplot) {
hoverData.splice(0, closedataPreviousLength);
distance = hoverData[0].distance;
}

}

// nothing left: remove all labels and quit
Expand Down Expand Up @@ -583,31 +589,35 @@ function hover(gd, evt, subplot) {
// other people and send it to the event
for(itemnum = 0; itemnum < hoverData.length; itemnum++) {
var pt = hoverData[itemnum];

var out = {
data: pt.trace._input,
fullData: pt.trace,
curveNumber: pt.trace.index,
pointNumber: pt.index,
x: pt.xVal,
y: pt.yVal,
xaxis: pt.xa,
yaxis: pt.ya
pointNumber: pt.index
};
if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;

if(pt.trace._module.eventData) out = pt.trace._module.eventData(out, pt);
else {
out.x = pt.xVal;
out.y = pt.yVal;
out.xaxis = pt.xa;
out.yaxis = pt.ya;

if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;
}

newhoverdata.push(out);
}

gd._hoverdata = newhoverdata;

if(!hoverChanged(gd, evt, oldhoverdata)) return;

/* Emit the custom hover handler. Bind this like:
* gd.on('hover.plotly', function(extras) {
* // do something with extras.data
* });
*/
if(oldhoverdata) {
gd.emit('plotly_unhover', { points: oldhoverdata });
}

gd.emit('plotly_hover', {
points: gd._hoverdata,
xaxes: xaArray,
Expand All @@ -620,7 +630,7 @@ function hover(gd, evt, subplot) {
// look for either .subplot (currently just ternary)
// or xaxis and yaxis attributes
function getSubplot(trace) {
return trace.subplot || (trace.xaxis + trace.yaxis);
return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo;
}

fx.getDistanceFunction = function(mode, dx, dy, dxy) {
Expand Down Expand Up @@ -724,6 +734,7 @@ function cleanPoint(d, hovermode) {
if(infomode.indexOf('text') === -1) d.text = undefined;
if(infomode.indexOf('name') === -1) d.name = undefined;
}

return d;
}

Expand Down
Loading

0 comments on commit f058e8b

Please sign in to comment.