Skip to content

Add global transforms config option #852

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 4 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ module.exports = {

// Turn all console logging on or off (errors will be thrown)
// This should ONLY be set via Plotly.setPlotConfig
logging: false
logging: false,

// Set global transform to be applied to all traces with no
// specification needed
globalTransforms: []
};

// where and how the background gets set can be overridden by context
Expand Down
20 changes: 12 additions & 8 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ plots.supplyDefaults = function(gd) {
newFullLayout._dataLength = newData.length;

// then do the data
newFullLayout._globalTransforms = gd._context.globalTransforms;
plots.supplyDataDefaults(newData, newFullData, newFullLayout);

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

fullTrace.index = i;
fullTrace._input = trace;
fullTrace._expandedIndex = cnt;

if(fullTrace.transforms && fullTrace.transforms.length) {
var expandedTraces = applyTransforms(fullTrace, dataOut, layout);

Expand All @@ -674,10 +679,6 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout) {
}
}
else {
fullTrace.index = i;
fullTrace._input = trace;
fullTrace._expandedIndex = cnt;

pushModule(fullTrace);
}
}
Expand Down Expand Up @@ -750,13 +751,16 @@ plots.supplyTraceDefaults = function(traceIn, traceIndex, layout) {
};

function supplyTransformDefaults(traceIn, traceOut, layout) {
if(!Array.isArray(traceIn.transforms)) return;
var globalTransforms = layout._globalTransforms || [];

if(!Array.isArray(traceIn.transforms) && globalTransforms.length === 0) return;

var containerIn = traceIn.transforms,
var containerIn = traceIn.transforms || [],
transformList = globalTransforms.concat(containerIn),
containerOut = traceOut.transforms = [];

for(var i = 0; i < containerIn.length; i++) {
var transformIn = containerIn[i],
for(var i = 0; i < transformList.length; i++) {
var transformIn = transformList[i],
type = transformIn.type,
_module = transformsRegistry[type],
transformOut;
Expand Down
34 changes: 34 additions & 0 deletions test/jasmine/tests/transforms_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ describe('one-to-one transforms:', function() {
expect(traceOut.y).toBe(traceIn.y);
});

it('supplyTraceDefaults should honored global transforms', function() {
var traceIn = {
y: [2, 1, 2],
transforms: [{
type: 'filter',
operation: '>',
value: '0',
filtersrc: 'x'
}]
};

var layout = {
_globalTransforms: [{
type: 'filter'
}]
};

var traceOut = Plots.supplyTraceDefaults(traceIn, 0, layout);

expect(traceOut.transforms[0]).toEqual({
type: 'filter',
operation: '=',
value: 0,
filtersrc: 'x'
}, '- global first');

expect(traceOut.transforms[1]).toEqual({
type: 'filter',
operation: '>',
value: 0,
filtersrc: 'x'
}, '- trace second');
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!
What do you think about one additional test that ensures transforms have access to fullTrace._input user data? Just in case someone other than yourself decides to refactor one day and accidentally breaks a bunch of userspace transforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 6af72d0

it('supplyDataDefaults should apply the transform while', function() {
var dataIn = [{
x: [-2, -2, 1, 2, 3],
Expand Down