Skip to content

Commit 1ddbd93

Browse files
committed
inverse mapping
1 parent 9bfbabf commit 1ddbd93

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/transforms/aggregate.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,28 @@ exports.calcTransform = function(gd, trace, opts) {
215215
var groupArray = Lib.getTargetArray(trace, {target: groups});
216216
if(!groupArray) return;
217217

218-
var i, vi, groupIndex;
218+
var i, vi, groupIndex, newGrouping;
219219

220220
var groupIndices = {};
221+
var groupToPoints = {};
222+
var indexToPoints = {};
221223
var groupings = [];
222224
for(i = 0; i < groupArray.length; i++) {
223225
vi = groupArray[i];
224226
groupIndex = groupIndices[vi];
225227
if(groupIndex === undefined) {
226228
groupIndices[vi] = groupings.length;
227-
groupings.push([i]);
229+
newGrouping = [i];
230+
groupings.push(newGrouping);
231+
groupToPoints[vi] = newGrouping;
232+
indexToPoints[groupIndices[vi]] = newGrouping;
228233
}
229234
else groupings[groupIndex].push(i);
230235
}
231236

237+
opts.groupToPoints = groupToPoints;
238+
opts.indexToPoints = indexToPoints;
239+
232240
var aggregations = opts.aggregations;
233241

234242
for(i = 0; i < aggregations.length; i++) {

test/jasmine/tests/transform_aggregate_test.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ describe('aggregate', function() {
5959
expect(traceOut.marker.opacity).toEqual([0.6, 'boo']);
6060
expect(traceOut.marker.line.color).toEqual(['the end', 3.3]);
6161
expect(traceOut.marker.line.width).toEqual([4, 1]);
62+
63+
var transform = traceOut.transforms[0];
64+
var inverseMapping = transform.indexToPoints;
65+
expect(inverseMapping).toEqual({0: [0, 2, 3, 4], 1: [1]});
6266
});
6367

6468
it('handles all funcs except sum for date data', function() {
@@ -163,6 +167,10 @@ describe('aggregate', function() {
163167
expect(traceOut.y).toEqual(['b', undefined]);
164168
// category average: can result in fractional categories -> rounds (0.5 rounds to 1)
165169
expect(traceOut.text).toEqual(['b', 'b']);
170+
171+
var transform = traceOut.transforms[0];
172+
var inverseMapping = transform.indexToPoints;
173+
expect(inverseMapping).toEqual({0: [0, 1], 1: [2, 3]});
166174
});
167175

168176
it('can aggregate on an existing data array', function() {
@@ -185,10 +193,12 @@ describe('aggregate', function() {
185193
expect(traceOut.x).toEqual([8, 7]);
186194
expect(traceOut.y).toBeCloseToArray([16 / 3, 7], 5);
187195
expect(traceOut.marker.size).toEqual([10, 20]);
196+
197+
var transform = traceOut.transforms[0];
198+
var inverseMapping = transform.indexToPoints;
199+
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
188200
});
189201

190-
// Regression test - throws before fix:
191-
// https://github.com/plotly/plotly.js/issues/2024
192202
it('can handle case where aggregation array is missing', function() {
193203
Plotly.newPlot(gd, [{
194204
x: [1, 2, 3, 4, 5],
@@ -205,6 +215,10 @@ describe('aggregate', function() {
205215
expect(traceOut.x).toEqual([1, 3]);
206216
expect(traceOut.y).toEqual([2, 6]);
207217
expect(traceOut.marker.size).toEqual([10, 20]);
218+
219+
var transform = traceOut.transforms[0];
220+
var inverseMapping = transform.indexToPoints;
221+
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
208222
});
209223

210224
it('handles median, mode, rms, & stddev for numeric data', function() {
@@ -257,7 +271,7 @@ describe('aggregate', function() {
257271
aggregations: [
258272
{target: 'x', func: 'sum'},
259273
{target: 'x', func: 'avg'},
260-
{target: 'y', func: 'avg'},
274+
{target: 'y', func: 'avg'}
261275
]
262276
}]
263277
}]);

0 commit comments

Comments
 (0)