Skip to content

Commit 039d9b6

Browse files
committed
aggregation to also use points from the preceding transform step
1 parent fb9b049 commit 039d9b6

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/transforms/aggregate.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,25 @@ exports.calcTransform = function(gd, trace, opts) {
220220
var groupIndices = {};
221221
var indexToPoints = {};
222222
var groupings = [];
223+
224+
var prevTransforms = trace.transforms.filter(function(tr) {return tr.indexToPoints;});
225+
var originalPointsAccessor = prevTransforms.length ?
226+
function(i) {return prevTransforms[prevTransforms.length - 1].indexToPoints[i];} :
227+
function(i) {return [i];};
228+
223229
for(i = 0; i < groupArray.length; i++) {
224230
vi = groupArray[i];
225231
groupIndex = groupIndices[vi];
226232
if(groupIndex === undefined) {
227233
groupIndices[vi] = groupings.length;
228234
newGrouping = [i];
229235
groupings.push(newGrouping);
230-
indexToPoints[groupIndices[vi]] = newGrouping;
236+
indexToPoints[groupIndices[vi]] = originalPointsAccessor(i);
237+
}
238+
else {
239+
groupings[groupIndex].push(i);
240+
indexToPoints[groupIndices[vi]] = indexToPoints[groupIndices[vi]].concat(originalPointsAccessor(i));
231241
}
232-
else groupings[groupIndex].push(i);
233242
}
234243

235244
opts.indexToPoints = indexToPoints;

test/jasmine/tests/transform_multi_test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ describe('multiple transforms:', function() {
278278
}];
279279

280280
var mockData2 = [{
281-
x: [1, 2, 3, 4, 5],
282-
y: [2, 3, 1, 7, 9],
281+
x: [1, 2, 3, 4, 5],
282+
y: [2, 3, 1, 7, 9],
283283
marker: {size: [10, 20, 20, 20, 10]},
284284
transforms: [
285285
{
@@ -292,8 +292,8 @@ describe('multiple transforms:', function() {
292292
type: 'aggregate',
293293
groups: 'marker.size',
294294
aggregations: [
295-
{target: 'x', func: 'sum'}, //20: 6, 10: 5
296-
{target: 'y', func: 'avg'} //20: 5, 10: 9
295+
{target: 'x', func: 'sum'}, // 20: 6, 10: 5
296+
{target: 'y', func: 'avg'} // 20: 5, 10: 9
297297
]
298298
},
299299
{
@@ -307,13 +307,13 @@ describe('multiple transforms:', function() {
307307

308308
afterEach(destroyGraphDiv);
309309

310-
fit('Plotly.plot should plot the transform traces - filter|aggregate|filter', function(done) {
310+
it('Plotly.plot should plot the transform traces - filter|aggregate|filter', function(done) {
311311
var data = Lib.extendDeep([], mockData2);
312312

313313
Plotly.plot(gd, data).then(function() {
314314
expect(gd.data.length).toEqual(1);
315315

316-
// this would be the result if we didn't have a second filter
316+
// this would be the result if we didn't have a second filter - kept for test case overview
317317
// expect(gd._fullData[0].x).toEqual([6, 5]);
318318
// expect(gd._fullData[0].y).toEqual([5, 9]);
319319
// expect(gd._fullData[0].marker.size).toEqual([20, 10]);
@@ -322,6 +322,10 @@ describe('multiple transforms:', function() {
322322
expect(gd._fullData[0].y).toEqual([9]);
323323
expect(gd._fullData[0].marker.size).toEqual([10]);
324324

325+
expect(gd._fullData[0].transforms[0].indexToPoints).toEqual({0: [1], 1: [3], 2: [4]});
326+
expect(gd._fullData[0].transforms[1].indexToPoints).toEqual({0: [1, 3], 1: [4]});
327+
expect(gd._fullData[0].transforms[2].indexToPoints).toEqual({0: [4]});
328+
325329
done();
326330
});
327331
});

0 commit comments

Comments
 (0)