Skip to content

Commit e555e0a

Browse files
missing aggregations array no longer throws - fix #2024
1 parent cb5a465 commit e555e0a

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/transforms/aggregate.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -155,31 +155,29 @@ exports.supplyDefaults = function(transformIn, traceOut) {
155155
arrayAttrs[groups] = 0;
156156
}
157157

158-
var aggregationsIn = transformIn.aggregations;
158+
var aggregationsIn = transformIn.aggregations || [];
159159
var aggregationsOut = transformOut.aggregations = new Array(aggregationsIn.length);
160160
var aggregationOut;
161161

162162
function coercei(attr, dflt) {
163163
return Lib.coerce(aggregationsIn[i], aggregationOut, aggAttrs, attr, dflt);
164164
}
165165

166-
if(aggregationsIn) {
167-
for(i = 0; i < aggregationsIn.length; i++) {
168-
aggregationOut = {};
169-
var target = coercei('target');
170-
var func = coercei('func');
171-
var enabledi = coercei('enabled');
172-
173-
// add this aggregation to the output only if it's the first instance
174-
// of a valid target attribute - or an unused target attribute with "count"
175-
if(enabledi && target && (arrayAttrs[target] || (func === 'count' && arrayAttrs[target] === undefined))) {
176-
if(func === 'stddev') coercei('funcmode');
177-
178-
arrayAttrs[target] = 0;
179-
aggregationsOut[i] = aggregationOut;
180-
}
181-
else aggregationsOut[i] = {enabled: false};
166+
for(i = 0; i < aggregationsIn.length; i++) {
167+
aggregationOut = {};
168+
var target = coercei('target');
169+
var func = coercei('func');
170+
var enabledi = coercei('enabled');
171+
172+
// add this aggregation to the output only if it's the first instance
173+
// of a valid target attribute - or an unused target attribute with "count"
174+
if(enabledi && target && (arrayAttrs[target] || (func === 'count' && arrayAttrs[target] === undefined))) {
175+
if(func === 'stddev') coercei('funcmode');
176+
177+
arrayAttrs[target] = 0;
178+
aggregationsOut[i] = aggregationOut;
182179
}
180+
else aggregationsOut[i] = {enabled: false};
183181
}
184182

185183
// any array attributes we haven't yet covered, fill them with the default aggregation

test/jasmine/tests/transform_aggregate_test.js

+20
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ describe('aggregate', function() {
190190
expect(traceOut.marker.size).toEqual([10, 20]);
191191
});
192192

193+
// Regression test - throws before fix:
194+
// https://github.com/plotly/plotly.js/issues/2024
195+
it('can handle case where aggregation array is missing', function() {
196+
Plotly.newPlot(gd, [{
197+
x: [1, 2, 3, 4, 5],
198+
y: [2, 4, 6, 8, 10],
199+
marker: {size: [10, 10, 20, 20, 10]},
200+
transforms: [{
201+
type: 'aggregate',
202+
groups: 'marker.size'
203+
}]
204+
}]);
205+
206+
var traceOut = gd._fullData[0];
207+
208+
expect(traceOut.x).toEqual([1, 3]);
209+
expect(traceOut.y).toEqual([2, 6]);
210+
expect(traceOut.marker.size).toEqual([10, 20]);
211+
});
212+
193213
it('handles median, mode, rms, & stddev for numeric data', function() {
194214
// again, nothing is going to barf with non-numeric data, but sometimes it
195215
// won't make much sense.

0 commit comments

Comments
 (0)