Skip to content

ensure makesData transforms work with Plotly.react #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,7 @@ function diffData(gd, oldFullData, newFullData, immutable) {

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

Expand Down
13 changes: 8 additions & 5 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,17 +1188,20 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
* parameters? If so, we should still keep going with supplyDefaults
* even if the trace is invisible, which may just be because it has no data yet.
*/
function hasMakesDataTransform(traceIn) {
var transformsIn = traceIn.transforms;
if(Array.isArray(transformsIn) && transformsIn.length) {
for(var i = 0; i < transformsIn.length; i++) {
var _module = transformsRegistry[transformsIn[i].type];
function hasMakesDataTransform(trace) {
var transforms = trace.transforms;
if(Array.isArray(transforms) && transforms.length) {
for(var i = 0; i < transforms.length; i++) {
var ti = transforms[i];
var _module = ti._module || transformsRegistry[ti.type];
if(_module && _module.makesData) return true;
}
}
return false;
}

plots.hasMakesDataTransform = hasMakesDataTransform;

plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
// For now we only allow transforms on 1D traces, ie those that specify a _length.
// If we were to implement 2D transforms, we'd need to have each transform
Expand Down
34 changes: 31 additions & 3 deletions test/jasmine/tests/transform_multi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ describe('general transforms:', function() {

describe('user-defined transforms:', function() {
'use strict';
afterEach(destroyGraphDiv);

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

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

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

return [traceOut];
}
};

Plotly.register(lineMakerModule);
Plots.supplyDataDefaults(dataIn, fullData, layout, fullLayout);
delete Plots.transformsRegistry.linemaker;

expect(fullData.length).toBe(1);
var traceOut = fullData[0];
Expand All @@ -336,6 +336,34 @@ describe('user-defined transforms:', function() {
expect(traceOut.mode).toBe('lines+markers');
expect(traceOut.line).toBeDefined();
expect(traceOut.marker).toBeDefined();

// make sure plot is really drawn, and changes in the base trace
// are propagated correctly on an edit (either restyle or react)
var gd = createGraphDiv();
function getLineWidth() {
var line = gd.querySelector('.js-line');
return line && parseFloat(line.style.strokeWidth);
}
Plotly.newPlot(gd, [{transforms: [transformIn], mode: 'lines+markers'}], layout)
.then(function() {
expect(getLineWidth()).toBe(2);

return Plotly.restyle(gd, 'line.width', 7);
})
.then(function() {
expect(getLineWidth()).toBe(7);

var data2 = [{transforms: [transformIn], mode: 'lines+markers', line: {width: 4}}];
return Plotly.react(gd, data2, layout);
})
.then(function() {
expect(getLineWidth()).toBe(4);
})
.catch(failTest)
.then(function() {
delete Plots.transformsRegistry.linemaker;
})
.then(done);
});

});
Expand Down