Skip to content

Commit

Permalink
add and test aggregation back _index.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bpostlethwaite committed Sep 22, 2017
1 parent e555e0a commit 13dd5c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,19 @@ plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
_module = transformsRegistry[type],
transformOut;

/*
* Supply defaults runs twice. First pass runs all supply defaults steps.
* If transforms exists another pass is run so any new traces generated
* in the transforms steps have their transform run. However if the
* transform does not have a 'transform' function (instead a calcFunction)
* than this second stage is unnecessary.
*/
var isSecondStage = transformIn._module && transformIn._module === _module,
doSecondStage = _module && typeof _module.transform === 'function';

if(!_module) Lib.warn('Unrecognized transform type ' + type + '.');

if(_module && _module.supplyDefaults) {
if(_module && _module.supplyDefaults && (!isSecondStage || doSecondStage)) {
transformOut = _module.supplyDefaults(transformIn, traceOut, layout, traceIn);
transformOut.type = type;
transformOut._module = _module;
Expand Down
7 changes: 4 additions & 3 deletions src/transforms/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ exports.supplyDefaults = function(transformIn, traceOut) {
}

for(i = 0; i < aggregationsIn.length; i++) {
aggregationOut = {};
aggregationOut = {_index: i};
var target = coercei('target');
var func = coercei('func');
var enabledi = coercei('enabled');
Expand All @@ -177,7 +177,7 @@ exports.supplyDefaults = function(transformIn, traceOut) {
arrayAttrs[target] = 0;
aggregationsOut[i] = aggregationOut;
}
else aggregationsOut[i] = {enabled: false};
else aggregationsOut[i] = {enabled: false, _index: i};
}

// any array attributes we haven't yet covered, fill them with the default aggregation
Expand All @@ -186,7 +186,8 @@ exports.supplyDefaults = function(transformIn, traceOut) {
aggregationsOut.push({
target: arrayAttrArray[i],
func: aggAttrs.func.dflt,
enabled: true
enabled: true,
_index: -1
});
}
}
Expand Down
36 changes: 36 additions & 0 deletions test/jasmine/tests/transform_aggregate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,40 @@ describe('aggregate', function() {
expect(traceOut.marker.line.width).toBeCloseToArray([0.5, 0], 5);
expect(traceOut.marker.color).toBeCloseToArray([Math.sqrt(1 / 3), 0], 5);
});

it('links fullData aggregations to userData via _index', function() {
Plotly.newPlot(gd, [{
x: [1, 2, 3, 4, 5],
y: [2, 4, 6, 8, 10],
marker: {
size: [10, 10, 20, 20, 10],
color: ['red', 'green', 'blue', 'yellow', 'white']
},
transforms: [{
type: 'aggregate',
groups: 'marker.size',
aggregations: [
{target: 'x', func: 'sum'},
{target: 'x', func: 'avg'},
{target: 'y', func: 'avg'},
]
}]
}]);

var traceOut = gd._fullData[0];
var fullAggregation = traceOut.transforms[0];
var fullAggregations = fullAggregation.aggregations;
var enabledAggregations = fullAggregations.filter(function(agg) {
return agg.enabled;
});

expect(enabledAggregations[0].target).toEqual('x');
expect(enabledAggregations[0]._index).toEqual(0);

expect(enabledAggregations[1].target).toEqual('y');
expect(enabledAggregations[1]._index).toEqual(2);

expect(enabledAggregations[2].target).toEqual('marker.color');
expect(enabledAggregations[2]._index).toEqual(-1);
});
});

0 comments on commit 13dd5c6

Please sign in to comment.