Skip to content

Persistent point selection with transforms compatibility #2163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
46 changes: 46 additions & 0 deletions src/components/fx/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@ exports.quadrature = function quadrature(dx, dy) {
};
};

/** Fill event data point object for hover and selection.
* Invokes _module.eventData if present.
*
* N.B. note that point 'index' corresponds to input data array index
* whereas 'number' is its post-transform version.
*
* If the hovered/selected pt corresponds to an multiple input points
* (e.g. for histogram and transformed traces), 'pointNumbers` and 'pointIndices'
* are include in the event data.
*
* @param {object} pt
* @param {object} trace
* @param {object} cd
* @return {object}
*/
exports.makeEventData = function makeEventData(pt, trace, cd) {
// hover uses 'index', select uses 'pointNumber'
var pointNumber = 'index' in pt ? pt.index : pt.pointNumber;

var out = {
data: trace._input,
fullData: trace,
curveNumber: trace.index,
pointNumber: pointNumber
};


if(trace._module.eventData) {
out = trace._module.eventData(out, pt, trace, cd, pointNumber);
} else {
if('xVal' in pt) out.x = pt.xVal;
else if('x' in pt) out.x = pt.x;

if('yVal' in pt) out.y = pt.yVal;
else if('y' in pt) out.y = pt.y;

if(pt.xa) out.xaxis = pt.xa;
if(pt.ya) out.yaxis = pt.ya;
if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;
}

exports.appendArrayPointValue(out, trace, pointNumber);

return out;
};

/** Appends values inside array attributes corresponding to given point number
*
* @param {object} pointData : point data object (gets mutated here)
Expand Down
21 changes: 1 addition & 20 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
// 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
};

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;
}

helpers.appendArrayPointValue(out, pt.trace, pt.index);
newhoverdata.push(out);
newhoverdata.push(helpers.makeEventData(pt, pt.trace, pt.cd));
}

gd._hoverdata = newhoverdata;
Expand Down
10 changes: 3 additions & 7 deletions src/plots/cartesian/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var polybool = require('polybooljs');
var polygon = require('../../lib/polygon');
var throttle = require('../../lib/throttle');
var color = require('../../components/color');
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
var makeEventData = require('../../components/fx/helpers').makeEventData;

var axes = require('./axes');
var constants = require('./constants');
Expand Down Expand Up @@ -355,15 +355,11 @@ function mergePolygons(list, poly, subtract) {

function fillSelectionItem(selection, searchInfo) {
if(Array.isArray(selection)) {
var cd = searchInfo.cd;
var trace = searchInfo.cd[0].trace;

for(var i = 0; i < selection.length; i++) {
var sel = selection[i];

sel.curveNumber = trace.index;
sel.data = trace._input;
sel.fullData = trace;
appendArrayPointValue(sel, trace, sel.pointNumber);
selection[i] = makeEventData(selection[i], trace, cd);
}
}

Expand Down
24 changes: 13 additions & 11 deletions src/traces/histogram/event_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';


module.exports = function eventData(out, pt) {
module.exports = function eventData(out, pt, trace, cd, pointNumber) {
// standard cartesian event data
out.x = pt.xVal;
out.y = pt.yVal;
out.xaxis = pt.xa;
out.yaxis = pt.ya;
out.x = 'xVal' in pt ? pt.xVal : pt.x;
out.y = 'yVal' in pt ? pt.yVal : pt.y;

if(pt.xa) out.xaxis = pt.xa;
if(pt.ya) out.yaxis = pt.ya;

// specific to histogram - CDFs do not have pts (yet?)
if(!(trace.cumulative || {}).enabled) {
var pts = Array.isArray(pointNumber) ?
cd[0].pts[pointNumber[0]][pointNumber[1]] :
cd[pointNumber].pts;

// specific to histogram
// CDFs do not have pts (yet?)
if(pt.pts) {
out.pointNumbers = pt.pts;
out.pointNumbers = pts;
out.binNumber = out.pointNumber;
delete out.pointNumber;
}
Expand Down
1 change: 0 additions & 1 deletion src/traces/histogram/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var posLetter = trace.orientation === 'h' ? 'y' : 'x';

pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], di.p0, di.p1);
pointData.pts = di.pts;
}

return pts;
Expand Down
1 change: 0 additions & 1 deletion src/traces/histogram2d/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, hoverLay

pointData.xLabel = hoverLabelText(pointData.xa, xRange[0], xRange[1]);
pointData.yLabel = hoverLabelText(pointData.ya, yRange[0], yRange[1]);
pointData.pts = cd0.pts[ny][nx];

return pts;
};
18 changes: 18 additions & 0 deletions src/traces/scattercarpet/event_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2012-2017, 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';

module.exports = function eventData(out, pt, trace, cd, pointNumber) {
var cdi = cd[pointNumber];

out.a = cdi.a;
out.b = cdi.b;

return out;
};
3 changes: 2 additions & 1 deletion src/traces/scattercarpet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ ScatterCarpet.calc = require('./calc');
ScatterCarpet.plot = require('./plot');
ScatterCarpet.style = require('./style');
ScatterCarpet.hoverPoints = require('./hover');
ScatterCarpet.selectPoints = require('./select');
ScatterCarpet.selectPoints = require('../scatter/select');
ScatterCarpet.eventData = require('./event_data');

ScatterCarpet.moduleType = 'trace';
ScatterCarpet.name = 'scattercarpet';
Expand Down
32 changes: 0 additions & 32 deletions src/traces/scattercarpet/select.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/traces/scatterternary/event_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2012-2017, 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';

module.exports = function eventData(out, pt, trace, cd, pointNumber) {
if(pt.xa) out.xaxis = pt.xa;
if(pt.ya) out.yaxis = pt.ya;

if(cd[pointNumber]) {
var cdi = cd[pointNumber];

// N.B. These are the scale coordinates !!!
//
// On master, hover events get the non-scaled coordinates
// whereas selection events get the scaled version.
// Note also that the hover labels show the scaled version.
//
// What about the 'raw' input coordinates?
// Should we include them in parallel here or replace a/b/c with them?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the scaled (normalized) coords are the most useful here, and folks can get the raw data back out easily enough using the indices, lets not add them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in c8d715b

out.a = cdi.a;
out.b = cdi.b;
out.c = cdi.c;
} else {
// for fill-hover only
out.a = pt.a;
out.b = pt.b;
out.c = pt.c;
}

return out;
};
3 changes: 2 additions & 1 deletion src/traces/scatterternary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ ScatterTernary.calc = require('./calc');
ScatterTernary.plot = require('./plot');
ScatterTernary.style = require('./style');
ScatterTernary.hoverPoints = require('./hover');
ScatterTernary.selectPoints = require('./select');
ScatterTernary.selectPoints = require('../scatter/select');
ScatterTernary.eventData = require('./event_data');

ScatterTernary.moduleType = 'trace';
ScatterTernary.name = 'scatterternary';
Expand Down
33 changes: 0 additions & 33 deletions src/traces/scatterternary/select.js

This file was deleted.