Skip to content

Commit 5811c3f

Browse files
authored
Merge pull request #852 from plotly/global-transforms
Add global transforms config option
2 parents 2d56127 + 6af72d0 commit 5811c3f

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

src/plot_api/plot_config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ module.exports = {
9494

9595
// Turn all console logging on or off (errors will be thrown)
9696
// This should ONLY be set via Plotly.setPlotConfig
97-
logging: false
97+
logging: false,
98+
99+
// Set global transform to be applied to all traces with no
100+
// specification needed
101+
globalTransforms: []
98102
};
99103

100104
// where and how the background gets set can be overridden by context

src/plots/plots.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ plots.supplyDefaults = function(gd) {
472472
newFullLayout._dataLength = newData.length;
473473

474474
// then do the data
475+
newFullLayout._globalTransforms = (gd._context || {}).globalTransforms;
475476
plots.supplyDataDefaults(newData, newFullData, newFullLayout);
476477

477478
// attach helper method to check whether a plot type is present on graph
@@ -650,6 +651,10 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout) {
650651
var trace = dataIn[i],
651652
fullTrace = plots.supplyTraceDefaults(trace, cnt, layout);
652653

654+
fullTrace.index = i;
655+
fullTrace._input = trace;
656+
fullTrace._expandedIndex = cnt;
657+
653658
if(fullTrace.transforms && fullTrace.transforms.length) {
654659
var expandedTraces = applyTransforms(fullTrace, dataOut, layout);
655660

@@ -674,10 +679,6 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout) {
674679
}
675680
}
676681
else {
677-
fullTrace.index = i;
678-
fullTrace._input = trace;
679-
fullTrace._expandedIndex = cnt;
680-
681682
pushModule(fullTrace);
682683
}
683684
}
@@ -750,13 +751,16 @@ plots.supplyTraceDefaults = function(traceIn, traceIndex, layout) {
750751
};
751752

752753
function supplyTransformDefaults(traceIn, traceOut, layout) {
753-
if(!Array.isArray(traceIn.transforms)) return;
754+
var globalTransforms = layout._globalTransforms || [];
755+
756+
if(!Array.isArray(traceIn.transforms) && globalTransforms.length === 0) return;
754757

755-
var containerIn = traceIn.transforms,
758+
var containerIn = traceIn.transforms || [],
759+
transformList = globalTransforms.concat(containerIn),
756760
containerOut = traceOut.transforms = [];
757761

758-
for(var i = 0; i < containerIn.length; i++) {
759-
var transformIn = containerIn[i],
762+
for(var i = 0; i < transformList.length; i++) {
763+
var transformIn = transformList[i],
760764
type = transformIn.type,
761765
_module = transformsRegistry[type],
762766
transformOut;

test/jasmine/tests/transforms_test.js

+73
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,79 @@ describe('one-to-one transforms:', function() {
6363
expect(traceOut.y).toBe(traceIn.y);
6464
});
6565

66+
it('supplyTraceDefaults should honored global transforms', function() {
67+
var traceIn = {
68+
y: [2, 1, 2],
69+
transforms: [{
70+
type: 'filter',
71+
operation: '>',
72+
value: '0',
73+
filtersrc: 'x'
74+
}]
75+
};
76+
77+
var layout = {
78+
_globalTransforms: [{
79+
type: 'filter'
80+
}]
81+
};
82+
83+
var traceOut = Plots.supplyTraceDefaults(traceIn, 0, layout);
84+
85+
expect(traceOut.transforms[0]).toEqual({
86+
type: 'filter',
87+
operation: '=',
88+
value: 0,
89+
filtersrc: 'x'
90+
}, '- global first');
91+
92+
expect(traceOut.transforms[1]).toEqual({
93+
type: 'filter',
94+
operation: '>',
95+
value: 0,
96+
filtersrc: 'x'
97+
}, '- trace second');
98+
});
99+
100+
it('should pass correctly arguments to transform methods', function() {
101+
var transformIn = { type: 'fake' };
102+
var transformOut = {};
103+
104+
var dataIn = [{
105+
transforms: [transformIn]
106+
}];
107+
108+
var layout = {};
109+
110+
function assertSupplyDefaultsArgs(_transformIn, traceOut, _layout) {
111+
expect(_transformIn).toBe(transformIn);
112+
expect(_layout).toBe(layout);
113+
114+
return transformOut;
115+
}
116+
117+
function assertTransformArgs(dataOut, opts) {
118+
expect(dataOut[0]._input).toBe(dataIn[0]);
119+
expect(opts.transform).toBe(transformOut);
120+
expect(opts.fullTrace._input).toBe(dataIn[0]);
121+
expect(opts.layout).toBe(layout);
122+
123+
return dataOut;
124+
}
125+
126+
var fakeTransformModule = {
127+
moduleType: 'transform',
128+
name: 'fake',
129+
attributes: {},
130+
supplyDefaults: assertSupplyDefaultsArgs,
131+
transform: assertTransformArgs
132+
};
133+
134+
Plotly.register(fakeTransformModule);
135+
Plots.supplyDataDefaults(dataIn, [], layout);
136+
delete Plots.transformsRegistry.fake;
137+
});
138+
66139
it('supplyDataDefaults should apply the transform while', function() {
67140
var dataIn = [{
68141
x: [-2, -2, 1, 2, 3],

0 commit comments

Comments
 (0)