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

20 files changed

+162
-76
lines changed

src/components/fx/helpers.js

+27
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

+1
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

+1
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

+4-3
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

+16
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

+5
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

+15-12
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

+12-10
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

+10-1
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

-8
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',

src/traces/scatter/defaults.js

-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3636
return;
3737
}
3838

39-
coerce('customdata');
4039
coerce('text');
4140
coerce('hovertext');
4241
coerce('mode', defaultMode);
43-
coerce('ids');
4442

4543
if(subTypes.hasLines(traceOut)) {
4644
handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce);

src/traces/scatter/select.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
4747
pointNumber: i,
4848
x: di.x,
4949
y: di.y,
50+
// TODO generalize with hover/click data handler
5051
id: di.id
5152
});
5253
di.dim = 0;

src/transforms/filter.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
var Lib = require('../lib');
1212
var Registry = require('../registry');
13-
var PlotSchema = require('../plot_api/plot_schema');
1413
var Axes = require('../plots/cartesian/axes');
1514

1615
var COMPARISON_OPS = ['=', '!=', '<', '>=', '>', '<='];
@@ -148,6 +147,7 @@ exports.calcTransform = function(gd, trace, opts) {
148147
var target = opts.target;
149148
var len = targetArray.length;
150149
var targetCalendar = opts.targetcalendar;
150+
var arrayAttrs = trace._arrayAttrs;
151151

152152
// even if you provide targetcalendar, if target is a string and there
153153
// is a calendar attribute matching target it will get used instead.
@@ -158,7 +158,6 @@ exports.calcTransform = function(gd, trace, opts) {
158158

159159
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray);
160160
var filterFunc = getFilterFunc(opts, d2c, targetCalendar);
161-
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
162161
var originalArrays = {};
163162

164163
function forAllAttrs(fn, index) {

src/transforms/groupby.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,14 @@ function transformOne(trace, state) {
122122
return trace;
123123
}
124124

125-
var groupNames = Lib.filterUnique(groups),
126-
newData = new Array(groupNames.length),
127-
len = groups.length;
128-
125+
var groupNames = Lib.filterUnique(groups);
126+
var newData = new Array(groupNames.length);
127+
var len = groups.length;
129128
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
130-
131129
var style = opts.style || {};
132130

133131
for(var i = 0; i < groupNames.length; i++) {
134132
var groupName = groupNames[i];
135-
136133
var newTrace = newData[i] = Lib.extendDeepNoArrays({}, trace);
137134

138135
arrayAttrs.forEach(initializeArray.bind(null, newTrace));

src/transforms/sort.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
'use strict';
1010

1111
var Lib = require('../lib');
12-
var PlotSchema = require('../plot_api/plot_schema');
1312
var Axes = require('../plots/cartesian/axes');
1413

1514
exports.moduleType = 'transform';
@@ -78,7 +77,7 @@ exports.calcTransform = function(gd, trace, opts) {
7877

7978
var target = opts.target;
8079
var len = targetArray.length;
81-
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
80+
var arrayAttrs = trace._arrayAttrs;
8281
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray);
8382
var indices = getIndices(opts, targetArray, d2c);
8483

0 commit comments

Comments
 (0)