Skip to content

Commit e92aaa6

Browse files
committed
add globalTransforms config argument
- transform options can be specified using the config arg or globally using Plotly.setPlotConfig() - global transforms are applied to all traces and are supplied before all other trace transforms.
1 parent f543871 commit e92aaa6

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
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

+8-4
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
@@ -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 || [];
754755

755-
var containerIn = traceIn.transforms,
756+
if(!Array.isArray(traceIn.transforms) && globalTransforms.length === 0) return;
757+
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

+34
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@ 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+
66100
it('supplyDataDefaults should apply the transform while', function() {
67101
var dataIn = [{
68102
x: [-2, -2, 1, 2, 3],

0 commit comments

Comments
 (0)