Skip to content

Commit 84a8a52

Browse files
committed
Merge branch 'all-array-attrs-in-event-data' into booz_2
2 parents 96b08d4 + 3a984a2 commit 84a8a52

File tree

20 files changed

+162
-76
lines changed

20 files changed

+162
-76
lines changed

src/components/fx/helpers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
'use strict';
1010

11+
var Lib = require('../../lib');
1112
var constants = require('./constants');
1213

1314
// look for either subplot or xaxis and yaxis attributes
@@ -83,3 +84,29 @@ function quadrature(dx, dy) {
8384
return Math.sqrt(x * x + y * y);
8485
};
8586
}
87+
88+
/** Appends values inside array attributes corresponding to given point number
89+
*
90+
* @param {object} pointData : point data object (gets mutated here)
91+
* @param {object} trace : full trace object
92+
* @param {number} ptNumber : point number
93+
*/
94+
exports.appendArrayPointValue = function(pointData, trace, ptNumber) {
95+
var arrayAttrs = trace._arrayAttrs;
96+
97+
for(var i = 0; i < arrayAttrs.length; i++) {
98+
var astr = arrayAttrs[i];
99+
var key;
100+
101+
if(astr === 'ids') key = 'id';
102+
else if(astr === 'locations') key = 'location';
103+
else key = astr;
104+
105+
if(pointData[key] === undefined) {
106+
var val = Lib.nestedProperty(trace, astr).get();
107+
pointData[key] = Array.isArray(ptNumber) ?
108+
val[ptNumber[0]][ptNumber[1]] :
109+
val[ptNumber];
110+
}
111+
}
112+
};

src/components/fx/hover.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ function _hover(gd, evt, subplot) {
457457
if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;
458458
}
459459

460+
helpers.appendArrayPointValue(out, pt.trace, pt.index);
460461
newhoverdata.push(out);
461462
}
462463

src/components/fx/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
getDistanceFunction: helpers.getDistanceFunction,
3636
getClosest: helpers.getClosest,
3737
inbox: helpers.inbox,
38+
appendArrayPointValue: helpers.appendArrayPointValue,
3839

3940
castHoverOption: castHoverOption,
4041
castHoverinfo: castHoverinfo,

src/plot_api/plot_schema.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ exports.findArrayAttributes = function(trace) {
157157
}
158158

159159
exports.crawl(baseAttributes, callback);
160-
exports.crawl(trace._module.attributes, callback);
160+
if(trace._module && trace._module.attributes) {
161+
exports.crawl(trace._module.attributes, callback);
162+
}
161163

162164
if(trace.transforms) {
163165
var transforms = trace.transforms;
@@ -177,9 +179,8 @@ exports.findArrayAttributes = function(trace) {
177179
// At the moment, we need this block to make sure that
178180
// ohlc and candlestick 'open', 'high', 'low', 'close' can be
179181
// used with filter ang groupby transforms.
180-
if(trace._fullInput) {
182+
if(trace._fullInput && trace._fullInput._module && trace._fullInput._module.attributes) {
181183
exports.crawl(trace._fullInput._module.attributes, callback);
182-
183184
arrayAttributes = Lib.filterUnique(arrayAttributes);
184185
}
185186

src/plots/attributes.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ module.exports = {
6969
role: 'info',
7070
dflt: ''
7171
},
72+
ids: {
73+
valType: 'data_array',
74+
description: [
75+
'Assigns id labels to each datum.',
76+
'These ids for object constancy of data points during animation.'
77+
].join(' ')
78+
},
79+
customdata: {
80+
valType: 'data_array',
81+
description: [
82+
'Assigns extra data each datum.',
83+
'This may be useful when listening to hover, click and selection events.',
84+
'Note that, *scatter* traces also appends customdata items in the markers',
85+
'DOM elements'
86+
].join(' ')
87+
},
7288
hoverinfo: {
7389
valType: 'flaglist',
7490
role: 'info',

src/plots/cartesian/select.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var polygon = require('../../lib/polygon');
1313
var color = require('../../components/color');
14+
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
1415

1516
var axes = require('./axes');
1617
var constants = require('./constants');
@@ -196,3 +197,7 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
196197
}
197198
};
198199
};
200+
201+
function makePointData(selection) {
202+
// appendArrayPointValue()
203+
}

src/plots/gl2d/scene2d.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ proto.updateFx = function(dragmode) {
534534

535535
proto.emitPointAction = function(nextSelection, eventType) {
536536
var uid = nextSelection.trace.uid;
537+
var ptNumber = nextSelection.pointIndex;
537538
var trace;
538539

539540
for(var i = 0; i < this.fullData.length; i++) {
@@ -542,18 +543,20 @@ proto.emitPointAction = function(nextSelection, eventType) {
542543
}
543544
}
544545

545-
this.graphDiv.emit(eventType, {
546-
points: [{
547-
x: nextSelection.traceCoord[0],
548-
y: nextSelection.traceCoord[1],
549-
curveNumber: trace.index,
550-
pointNumber: nextSelection.pointIndex,
551-
data: trace._input,
552-
fullData: this.fullData,
553-
xaxis: this.xaxis,
554-
yaxis: this.yaxis
555-
}]
556-
});
546+
var pointData = {
547+
x: nextSelection.traceCoord[0],
548+
y: nextSelection.traceCoord[1],
549+
curveNumber: trace.index,
550+
pointNumber: ptNumber,
551+
data: trace._input,
552+
fullData: this.fullData,
553+
xaxis: this.xaxis,
554+
yaxis: this.yaxis
555+
};
556+
557+
Fx.appendArrayPointValue(pointData, trace, ptNumber);
558+
559+
this.graphDiv.emit(eventType, {points: [pointData]});
557560
};
558561

559562
proto.draw = function() {

src/plots/gl3d/scene.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,20 @@ function render(scene) {
103103
});
104104
}
105105

106-
var eventData = {
107-
points: [{
108-
x: selection.traceCoordinate[0],
109-
y: selection.traceCoordinate[1],
110-
z: selection.traceCoordinate[2],
111-
data: trace._input,
112-
fullData: trace,
113-
curveNumber: trace.index,
114-
pointNumber: ptNumber
115-
}]
106+
var pointData = {
107+
x: selection.traceCoordinate[0],
108+
y: selection.traceCoordinate[1],
109+
z: selection.traceCoordinate[2],
110+
data: trace._input,
111+
fullData: trace,
112+
curveNumber: trace.index,
113+
pointNumber: ptNumber
116114
};
117115

116+
Fx.appendArrayPointValue(pointData, trace, ptNumber);
117+
118+
var eventData = {points: [pointData]};
119+
118120
if(selection.buttons && selection.distance < 5) {
119121
scene.graphDiv.emit('plotly_click', eventData);
120122
}

src/plots/plots.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ plots.supplyDefaults = function(gd) {
521521

522522
function remapTransformedArrays(cd0, newTrace) {
523523
var oldTrace = cd0.trace;
524-
var arrayAttrs = PlotSchema.findArrayAttributes(oldTrace);
524+
var arrayAttrs = oldTrace._arrayAttrs;
525525
var transformedArrayHash = {};
526526
var i, astr;
527527

@@ -862,6 +862,9 @@ plots.supplyTraceDefaults = function(traceIn, traceOutIndex, layout, traceInInde
862862
}
863863

864864
if(visible) {
865+
coerce('customdata');
866+
coerce('ids');
867+
865868
var _module = plots.getModule(traceOut);
866869
traceOut._module = _module;
867870

@@ -2032,6 +2035,12 @@ plots.doCalcdata = function(gd, traces) {
20322035
}
20332036
}
20342037

2038+
// find array attributes in trace
2039+
for(i = 0; i < fullData.length; i++) {
2040+
trace = fullData[i];
2041+
trace._arrayAttrs = PlotSchema.findArrayAttributes(trace);
2042+
}
2043+
20352044
initCategories(axList);
20362045

20372046
var hasCalcTransform = false;

src/traces/scatter/attributes.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ module.exports = {
5757
'where `y0` is the starting coordinate and `dy` the step.'
5858
].join(' ')
5959
},
60-
customdata: {
61-
valType: 'data_array',
62-
description: 'Assigns extra data to each scatter point DOM element'
63-
},
6460
dy: {
6561
valType: 'number',
6662
dflt: 1,
@@ -70,10 +66,6 @@ module.exports = {
7066
'See `y0` for more info.'
7167
].join(' ')
7268
},
73-
ids: {
74-
valType: 'data_array',
75-
description: 'A list of keys for object constancy of data points during animation'
76-
},
7769
text: {
7870
valType: 'string',
7971
role: 'info',

0 commit comments

Comments
 (0)