Skip to content

Include values of all array attributes in hover/click/select event data #1770

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
merged 8 commits into from
Jun 15, 2017
24 changes: 24 additions & 0 deletions src/components/fx/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

'use strict';

var Lib = require('../../lib');
var constants = require('./constants');

// look for either subplot or xaxis and yaxis attributes
Expand Down Expand Up @@ -83,3 +84,26 @@ function quadrature(dx, dy) {
return Math.sqrt(x * x + y * y);
};
}

/** Appends values inside array attributes corresponding to given point number
*
* @param {object} pointData : point data object (gets mutated here
* @param {object} trace : full trace object
* @param {number} ptNumber : point number
*/
exports.appendArrayPointValue = function(pointData, trace, ptNumber) {
var arrayAttrs = trace._arrayAttrs;

for(var i = 0; i < arrayAttrs.length; i++) {
var astr = arrayAttrs[i];
var key;

if(astr === 'ids') key = 'id';
else if(astr === 'location') key = 'location';
else key = astr;

if(pointData[key] === undefined) {
pointData[key] = Lib.nestedProperty(trace, astr).get()[ptNumber];
}
}
};
3 changes: 3 additions & 0 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,19 @@ function _hover(gd, evt, subplot) {
pointNumber: pt.index
};

// could maybe :hocho: _module.eventData
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, trace, pt.index);
newhoverdata.push(out);
}

Expand Down
1 change: 1 addition & 0 deletions src/components/fx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
getDistanceFunction: helpers.getDistanceFunction,
getClosest: helpers.getClosest,
inbox: helpers.inbox,
appendArrayPointValue: helpers.appendArrayPointValue,

castHoverOption: castHoverOption,
castHoverinfo: castHoverinfo,
Expand Down
27 changes: 15 additions & 12 deletions src/plots/gl2d/scene2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ proto.updateTraces = function(fullData, calcData) {

proto.emitPointAction = function(nextSelection, eventType) {
var uid = nextSelection.trace.uid;
var ptNumber = nextSelection.pointIndex;
var trace;

for(var i = 0; i < this.fullData.length; i++) {
Expand All @@ -520,18 +521,20 @@ proto.emitPointAction = function(nextSelection, eventType) {
}
}

this.graphDiv.emit(eventType, {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: trace.index,
pointNumber: nextSelection.pointIndex,
data: trace._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
var pointData = {
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: trace.index,
pointNumber: ptNumber,
data: trace._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
};

Fx.appendArrayPointValue(pointData, trace, ptNumber);

this.graphDiv.emit(eventType, {points: [pointData]});
};

proto.draw = function() {
Expand Down
22 changes: 12 additions & 10 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ function render(scene) {
});
}

var eventData = {
points: [{
x: selection.traceCoordinate[0],
y: selection.traceCoordinate[1],
z: selection.traceCoordinate[2],
data: trace._input,
fullData: trace,
curveNumber: trace.index,
pointNumber: ptNumber
}]
var pointData = {
x: selection.traceCoordinate[0],
y: selection.traceCoordinate[1],
z: selection.traceCoordinate[2],
data: trace._input,
fullData: trace,
curveNumber: trace.index,
pointNumber: ptNumber
};

Fx.appendArrayPointValue(pointData);

var eventData = {points: [pointData]};

if(selection.buttons && selection.distance < 5) {
scene.graphDiv.emit('plotly_click', eventData);
}
Expand Down
3 changes: 3 additions & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,9 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
Lib.pushUnique(modules, _module);
Lib.pushUnique(basePlotModules, fullTrace._module.basePlotModule);

// TODO remove findArrayAttributes calls downstream
fullTrace._arrayAttrs = PlotSchema.findArrayAttributes(fullTrace);
Copy link
Collaborator

Choose a reason for hiding this comment

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

possibly move this to calc since we do that whenever arrays change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call!

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 ee1adf8


cnt++;
}

Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
pointNumber: i,
x: di.x,
y: di.y,
// TODO generalize with hover/click data handler
id: di.id
});
di.dim = 0;
Expand Down