Skip to content

Commit 4199bb5

Browse files
committed
ensure makesData transforms work with Plotly.react
1 parent 854bbc9 commit 4199bb5

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/plot_api/plot_api.js

+1
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,7 @@ function diffData(gd, oldFullData, newFullData, immutable) {
23622362

23632363
for(i = 0; i < oldFullData.length; i++) {
23642364
trace = newFullData[i]._fullInput;
2365+
if(Plots.hasMakesDataTransform(trace)) trace = newFullData[i];
23652366
if(seenUIDs[trace.uid]) continue;
23662367
seenUIDs[trace.uid] = 1;
23672368

src/plots/plots.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1188,17 +1188,20 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
11881188
* parameters? If so, we should still keep going with supplyDefaults
11891189
* even if the trace is invisible, which may just be because it has no data yet.
11901190
*/
1191-
function hasMakesDataTransform(traceIn) {
1192-
var transformsIn = traceIn.transforms;
1193-
if(Array.isArray(transformsIn) && transformsIn.length) {
1194-
for(var i = 0; i < transformsIn.length; i++) {
1195-
var _module = transformsRegistry[transformsIn[i].type];
1191+
function hasMakesDataTransform(trace) {
1192+
var transforms = trace.transforms;
1193+
if(Array.isArray(transforms) && transforms.length) {
1194+
for(var i = 0; i < transforms.length; i++) {
1195+
var ti = transforms[i];
1196+
var _module = ti._module || transformsRegistry[ti.type];
11961197
if(_module && _module.makesData) return true;
11971198
}
11981199
}
11991200
return false;
12001201
}
12011202

1203+
plots.hasMakesDataTransform = hasMakesDataTransform;
1204+
12021205
plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
12031206
// For now we only allow transforms on 1D traces, ie those that specify a _length.
12041207
// If we were to implement 2D transforms, we'd need to have each transform

test/jasmine/tests/transform_multi_test.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ describe('general transforms:', function() {
207207

208208
describe('user-defined transforms:', function() {
209209
'use strict';
210+
afterEach(destroyGraphDiv);
210211

211212
it('should pass correctly arguments to transform methods', function() {
212213
var transformIn = { type: 'fake' };
@@ -283,7 +284,7 @@ describe('user-defined transforms:', function() {
283284
expect(calledSupplyLayoutDefaults).toBe(1);
284285
});
285286

286-
it('handles `makesData` transforms when the incoming trace has no data', function() {
287+
it('handles `makesData` transforms when the incoming trace has no data', function(done) {
287288
var transformIn = {type: 'linemaker', x0: 3, y0: 2, x1: 5, y1: 10, n: 3};
288289
var dataIn = [{transforms: [transformIn], mode: 'lines+markers'}];
289290
var fullData = [];
@@ -317,15 +318,14 @@ describe('user-defined transforms:', function() {
317318
expect(trace.marker).toBeUndefined();
318319

319320
// just put the input trace back in here, it'll get coerced again after the transform
320-
var traceOut = Lib.extendFlat(trace._input, {x: x, y: y});
321+
var traceOut = Lib.extendFlat({}, trace._input, {x: x, y: y});
321322

322323
return [traceOut];
323324
}
324325
};
325326

326327
Plotly.register(lineMakerModule);
327328
Plots.supplyDataDefaults(dataIn, fullData, layout, fullLayout);
328-
delete Plots.transformsRegistry.linemaker;
329329

330330
expect(fullData.length).toBe(1);
331331
var traceOut = fullData[0];
@@ -336,6 +336,34 @@ describe('user-defined transforms:', function() {
336336
expect(traceOut.mode).toBe('lines+markers');
337337
expect(traceOut.line).toBeDefined();
338338
expect(traceOut.marker).toBeDefined();
339+
340+
// make sure plot is really drawn, and changes in the base trace
341+
// are propagated correctly on an edit (either restyle or react)
342+
var gd = createGraphDiv();
343+
function getLineWidth() {
344+
var line = gd.querySelector('.js-line');
345+
return line && parseFloat(line.style.strokeWidth);
346+
}
347+
Plotly.newPlot(gd, [{transforms: [transformIn], mode: 'lines+markers'}], layout)
348+
.then(function() {
349+
expect(getLineWidth()).toBe(2);
350+
351+
return Plotly.restyle(gd, 'line.width', 7);
352+
})
353+
.then(function() {
354+
expect(getLineWidth()).toBe(7);
355+
356+
var data2 = [{transforms: [transformIn], mode: 'lines+markers', line: {width: 4}}];
357+
return Plotly.react(gd, data2, layout);
358+
})
359+
.then(function() {
360+
expect(getLineWidth()).toBe(4);
361+
})
362+
.catch(failTest)
363+
.then(function() {
364+
delete Plots.transformsRegistry.linemaker;
365+
})
366+
.then(done);
339367
});
340368

341369
});

0 commit comments

Comments
 (0)