Skip to content

Commit 3255d0f

Browse files
alexcjohnsonetpinard
authored andcommitted
Support transforms that make their own data without trace input data
1 parent ece927f commit 3255d0f

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/plots/plots.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1123,11 +1123,29 @@ plots.supplyTraceDefaults = function(traceIn, colorIndex, layout, traceInIndex)
11231123
return traceOut;
11241124
};
11251125

1126+
/**
1127+
* hasMakesDataTransform: does this trace have a transform that makes its own
1128+
* data, either by grabbing it from somewhere else or by creating it from input
1129+
* parameters? If so, we should still keep going with supplyDefaults
1130+
* even if the trace is invisible, which may just be because it has no data yet.
1131+
*/
1132+
function hasMakesDataTransform(traceIn) {
1133+
var transformsIn = traceIn.transforms;
1134+
if(Array.isArray(transformsIn) && transformsIn.length) {
1135+
for(var i = 0; i < transformsIn.length; i++) {
1136+
var _module = transformsRegistry[transformsIn[i].type];
1137+
if(_module && _module.makesData) return true;
1138+
}
1139+
}
1140+
return false;
1141+
}
1142+
11261143
plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
11271144
// For now we only allow transforms on 1D traces, ie those that specify a _length.
11281145
// If we were to implement 2D transforms, we'd need to have each transform
11291146
// describe its own applicability and disable itself when it doesn't apply.
1130-
if(!traceOut._length) return;
1147+
// Also allow transforms that make their own data, but not in globalTransforms
1148+
if(!(traceOut._length || hasMakesDataTransform(traceIn))) return;
11311149

11321150
var globalTransforms = layout._globalTransforms || [];
11331151
var transformModules = layout._transformModules || [];

test/jasmine/tests/transform_multi_test.js

+55
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,61 @@ describe('user-defined transforms:', function() {
280280
expect(calledSupplyLayoutDefaults).toBe(1);
281281
});
282282

283+
it('handles `makesData` transforms when the incoming trace has no data', function() {
284+
var transformIn = {type: 'linemaker', x0: 3, y0: 2, x1: 5, y1: 10, n: 3};
285+
var dataIn = [{transforms: [transformIn], mode: 'lines+markers'}];
286+
var fullData = [];
287+
var layout = {};
288+
var fullLayout = Lib.extendDeep({}, mockFullLayout);
289+
290+
var lineMakerModule = {
291+
moduleType: 'transform',
292+
name: 'linemaker',
293+
makesData: true,
294+
attributes: {},
295+
supplyDefaults: function(transformIn) {
296+
return Lib.extendFlat({}, transformIn);
297+
},
298+
transform: function(data, state) {
299+
var transform = state.transform;
300+
var trace = data[0];
301+
var n = transform.n;
302+
var x = new Array(n);
303+
var y = new Array(n);
304+
305+
// our exciting transform - make a line!
306+
for(var i = 0; i < n; i++) {
307+
x[i] = transform.x0 + (i / (n - 1)) * (transform.x1 - transform.x0);
308+
y[i] = transform.y0 + (i / (n - 1)) * (transform.y1 - transform.y0);
309+
}
310+
311+
// we didn't coerce mode before, because there was no data
312+
expect(trace.mode).toBeUndefined();
313+
expect(trace.line).toBeUndefined();
314+
expect(trace.marker).toBeUndefined();
315+
316+
// just put the input trace back in here, it'll get coerced again after the transform
317+
var traceOut = Lib.extendFlat(trace._input, {x: x, y: y});
318+
319+
return [traceOut];
320+
}
321+
};
322+
323+
Plotly.register(lineMakerModule);
324+
Plots.supplyDataDefaults(dataIn, fullData, layout, fullLayout);
325+
delete Plots.transformsRegistry.linemaker;
326+
327+
expect(fullData.length).toBe(1);
328+
var traceOut = fullData[0];
329+
expect(traceOut.x).toEqual([3, 4, 5]);
330+
expect(traceOut.y).toEqual([2, 6, 10]);
331+
332+
// make sure we redid supplyDefaults after the data arrays were added
333+
expect(traceOut.mode).toBe('lines+markers');
334+
expect(traceOut.line).toBeDefined();
335+
expect(traceOut.marker).toBeDefined();
336+
});
337+
283338
});
284339

285340
describe('multiple transforms:', function() {

0 commit comments

Comments
 (0)