Skip to content

Commit 75e6e48

Browse files
add and test aggregation back _index.
This necessitated a change in plots.js to prevent the supply defaults step running twice with the second run acting on fullData rather then userData.
1 parent e555e0a commit 75e6e48

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/plots/plots.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1039,9 +1039,21 @@ plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
10391039
_module = transformsRegistry[type],
10401040
transformOut;
10411041

1042+
/*
1043+
* Supply defaults may run twice. First pass runs all supply defaults steps
1044+
* and adds the _module to any output transforms.
1045+
* If transforms exist another pass is run so that any generated traces also
1046+
* go through supply defaults. This has the effect of rerunning
1047+
* supplyTransforms. If the transform does not have a `transform` function
1048+
* it could not have generated any new traces and the second stage is
1049+
* unnecessary. We detect this case with the following variables.
1050+
*/
1051+
var isSecondStage = transformIn._module && transformIn._module === _module,
1052+
doSecondStage = _module && typeof _module.transform === 'function';
1053+
10421054
if(!_module) Lib.warn('Unrecognized transform type ' + type + '.');
10431055

1044-
if(_module && _module.supplyDefaults) {
1056+
if(_module && _module.supplyDefaults && (!isSecondStage || doSecondStage)) {
10451057
transformOut = _module.supplyDefaults(transformIn, traceOut, layout, traceIn);
10461058
transformOut.type = type;
10471059
transformOut._module = _module;

src/transforms/aggregate.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ exports.supplyDefaults = function(transformIn, traceOut) {
164164
}
165165

166166
for(i = 0; i < aggregationsIn.length; i++) {
167-
aggregationOut = {};
167+
aggregationOut = {_index: i};
168168
var target = coercei('target');
169169
var func = coercei('func');
170170
var enabledi = coercei('enabled');
@@ -177,7 +177,7 @@ exports.supplyDefaults = function(transformIn, traceOut) {
177177
arrayAttrs[target] = 0;
178178
aggregationsOut[i] = aggregationOut;
179179
}
180-
else aggregationsOut[i] = {enabled: false};
180+
else aggregationsOut[i] = {enabled: false, _index: i};
181181
}
182182

183183
// any array attributes we haven't yet covered, fill them with the default aggregation
@@ -186,7 +186,8 @@ exports.supplyDefaults = function(transformIn, traceOut) {
186186
aggregationsOut.push({
187187
target: arrayAttrArray[i],
188188
func: aggAttrs.func.dflt,
189-
enabled: true
189+
enabled: true,
190+
_index: -1
190191
});
191192
}
192193
}

test/jasmine/tests/transform_aggregate_test.js

+36
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,40 @@ describe('aggregate', function() {
245245
expect(traceOut.marker.line.width).toBeCloseToArray([0.5, 0], 5);
246246
expect(traceOut.marker.color).toBeCloseToArray([Math.sqrt(1 / 3), 0], 5);
247247
});
248+
249+
it('links fullData aggregations to userData via _index', function() {
250+
Plotly.newPlot(gd, [{
251+
x: [1, 2, 3, 4, 5],
252+
y: [2, 4, 6, 8, 10],
253+
marker: {
254+
size: [10, 10, 20, 20, 10],
255+
color: ['red', 'green', 'blue', 'yellow', 'white']
256+
},
257+
transforms: [{
258+
type: 'aggregate',
259+
groups: 'marker.size',
260+
aggregations: [
261+
{target: 'x', func: 'sum'},
262+
{target: 'x', func: 'avg'},
263+
{target: 'y', func: 'avg'},
264+
]
265+
}]
266+
}]);
267+
268+
var traceOut = gd._fullData[0];
269+
var fullAggregation = traceOut.transforms[0];
270+
var fullAggregations = fullAggregation.aggregations;
271+
var enabledAggregations = fullAggregations.filter(function(agg) {
272+
return agg.enabled;
273+
});
274+
275+
expect(enabledAggregations[0].target).toEqual('x');
276+
expect(enabledAggregations[0]._index).toEqual(0);
277+
278+
expect(enabledAggregations[1].target).toEqual('y');
279+
expect(enabledAggregations[1]._index).toEqual(2);
280+
281+
expect(enabledAggregations[2].target).toEqual('marker.color');
282+
expect(enabledAggregations[2]._index).toEqual(-1);
283+
});
248284
});

0 commit comments

Comments
 (0)