8000 Introducing transform plugins by etpinard · Pull Request #499 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Introducing transform plugins #499

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 27 commits into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
53a5971
expose Lib on test dashboard window
etpinard May 3, 2016
9909382
lint
etpinard May 3, 2016
28147e2
add check so that we don't try to destroy gd if it DNE
etpinard May 3, 2016
9ac6d0f
make Plotly.register handle transform modules
etpinard May 3, 2016
06f2e6d
add support for transform plugins:
etpinard May 3, 2016
8ab9d0c
make Plotly.restyle support transforms:
etpinard May 3, 2016
2697975
add two example transform modules
etpinard May 3, 2016
56665f2
add transform test suite
etpinard May 3, 2016
aa274ef
update tests (plots.supplyDataDefaults -> supplyTraceDefaults)
etpinard May 3, 2016
a06441a
[tmp commit] add example transforms to ./lib/ Plotly for testing
etpinard May 3, 2016
0b62e45
Merge branch 'master' into transform-plugins
etpinard Jul 4, 2016
1c651e8
toimage_test: make sure that all graph divs are deleted
etpinard Jul 4, 2016
f93b9c1
plots: rm useless skip-over-step
etpinard Jul 4, 2016
ccbcb58
Merge branch 'master' into transform-plugins
etpinard Jul 18, 2016
37e291b
plots: make relinkPrivateKeys less strict
etpinard Jul 19, 2016
383c6d4
move common code in pushModule func
etpinard Jul 19, 2016
be36402
rm useless comment about fits ;)
etpinard Jul 19, 2016 8000
0e1eb5a
do not add transforms container if fullTrace is no transform present
etpinard Jul 19, 2016
6a8e158
try more useless refs to input container in full trace
etpinard Jul 19, 2016
9a341c1
mutate uid of expanded traces w.r.t to expanded index:
etpinard Jul 19, 2016
1752d30
make uid of expanded trace valid alpha-numerical hashes
etpinard Jul 19, 2016
b5ec01e
add support for transform in plot schema and Plotly.validate
etpinard Jul 20, 2016
a6fda02
improve validate test cases
etpinard Jul 20, 2016
da2c24c
validate transform module on registration:
etpinard Jul 20, 2016
8d7dc46
improve transform tests
etpinard Jul 20, 2016
320d62e
Revert "[tmp commit] add example transforms to ./lib/ Plotly for test…
etpinard Jul 20, 2016
3e6e9c1
rm @assets path shortcut,
etpinard Jul 20, 2016
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
Prev Previous commit
Next Next commit
add two example transform modules
  • Loading branch information
etpinard committed May 3, 2016
commit 2697975cb392ec8dc0a72c6440f9e36dd9a7693a
161 changes: 161 additions & 0 deletions test/jasmine/assets/transforms/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
'use strict';

// var Lib = require('@src/lib');
var Lib = require('../../../../src/lib');

/*eslint no-unused-vars: 0*/


// so that Plotly.register knows what to do with it
exports.moduleType = 'transform';

// determines to link between transform type and transform module
exports.name = 'filter';

// ... as trace attributes
exports.attributes = {
operation: {
valType: 'enumerated',
values: ['=', '<', '>'],
dflt: '='
},
value: {
valType: 'number',
dflt: 0
},
filtersrc: {
valType: 'enumerated',
values: ['x', 'y'],
dflt: 'x'
}
};

/**
* Supply transform attributes defaults
*
* @param {object} transformIn
* object linked to trace.transforms[i] with 'type' set to exports.name
* @param {object} fullData
* the plot's full data
* @param {object} layout
* the plot's (not-so-full) layout
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.supplyDefaults = function(transformIn, fullData, layout) {
var transformOut = {};

function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}

coerce('operation');
coerce('value');
coerce('filtersrc');

// or some more complex logic using fullData and layout

return transformOut;
};

/**
* Apply transform !!!
*
* @param {array} data
* array of transformed traces (is [fullTrace] upon first transform)
*
* @param {object} state
* state object which includes:
* - transform {object} full transform attributes
* - fullTrace {object} full trace object which is being transformed
* - fullData {array} full pre-transform(s) data array
* - layout {object} the plot's (not-so-full) layout
*
* @return {object} newData
* array of transformed traces
*/
exports.transform = function(data, state) {

// one-to-one case

var newData = data.map(function(trace) {
return transformOne(trace, state);
});

return newData;
};

function transformOne(trace, state) {
var newTrace = Lib.extendDeep({}, trace);

var opts = state.transform;
var src = opts.filtersrc;
var filterFunc = getFilterFunc(opts);
var len = trace[src].length;
var arrayAttrs = findArrayAttributes(trace);

arrayAttrs.forEach(function(attr) {
Lib.nestedProperty(newTrace, attr).set([]);
});

function fill(attr, i) {
var arr = Lib.nestedProperty(trace, attr).get();
var newArr = Lib.nestedProperty(newTrace, attr).get();

newArr.push(arr[i]);
}

for(var i = 0; i < len; i++) {
var v = trace[src][i];

if(!filterFunc(v)) continue;

for(var j = 0; j < arrayAttrs.length; j++) {
fill(arrayAttrs[j], i);
}
}

return newTrace;
}

function getFilterFunc(opts) {
var value = opts.value;

switch(opts.operation) {
case '=':
return function(v) { return v === value; };
case '<':
return function(v) { return v < value; };
case '>':
return function(v) { return v > value; };
}
}

function findArrayAttributes(obj, root) {
root = root || '';

var list = [];

Object.keys(obj).forEach(function(k) {
var val = obj[k];

if(k.charAt(0) === '_') return;

if(k === 'transforms') {
Copy link

Choose a reason for hiding this comment

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

I think k === 'colorscales' might need a special case too. I'm not using this plugin but adapted this approach for my own purposes, and I found that when coloring by a continuous variable you end up with errors unless colorscales are left alone. (And just by inspecting the docs, perhaps ticktext and tickvals might be special as well.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ideal solution would use our Plotly.PlotSchema.crawl utility to check for data_array attributes in the incoming traces. Here, only data_array attributes would have to be truncated.

val.forEach(function(item, i) {
list = list.concat(
findArrayAttributes(item, root + k + '[' + i + ']' + '.')
);
});
}
else if(Lib.isPlainObject(val)) {
list = list.concat(findArrayAttributes(val, root + k + '.'));
}
else if(Array.isArray(val)) {
list.push(root + k);
}
});

return list;
}
125 changes: 125 additions & 0 deletions test/jasmine/assets/transforms/groupby.js
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
'use strict';

// var Lib = require('@src/lib');
var Lib = require('../../../../src/lib');

/*eslint no-unused-vars: 0*/


// so that Plotly.register knows what to do with it
exports.moduleType = 'transform';

// determines to link between transform type and transform module
exports.name = 'groupby';

// ... as trace attributes
exports.attributes = {
active: {
valType: 'boolean',
dflt: true
},
groups: {
valType: 'data_array',
dflt: []
},
groupColors: {
valType: 'any',
dflt: {}
}
};

Choose a reason for hiding this comment

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

It would also be useful to have a groupSymbols attribute.


/**
* Supply transform attributes defaults
*
* @param {object} transformIn
* object linked to trace.transforms[i] with 'type' set to exports.name
* @param {object} fullData
* the plot's full data
* @param {object} layout
* the plot's (not-so-full) layout
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.supplyDefaults = function(transformIn, fullData, layout) {
var transformOut = {};

function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}

var active = coerce('active');

if(!active) return transformOut;

coerce('groups');
coerce('groupColors');

// or some more complex logic using fullData and layout

return transformOut;
};

/**
* Apply transform !!!
*
* @param {array} data
* array of transformed traces (is [fullTrace] upon first transform)
*
* @param {object} state
* state object which includes:
* - transform {object} full transform attributes
* - fullTrace {object} full trace object which is being transformed
* - fullData {array} full pre-transform(s) data array
* - layout {object} the plot's (not-so-full) layout
*
* @return {object} newData
* array of transformed traces
*/
exports.transform = function(data, state) {

// one-to-many case

var newData = [];

data.forEach(function(trace) {
newData = newData.concat(transformOne(trace, state));
});

return newData;
};

function transformOne(trace, state) {
var opts = state.transform;
var groups = opts.groups;

var groupNames = groups.filter(function(g, i, self) {
return self.indexOf(g) === i;
});

var newData = new Array(groupNames.length);
var len = Math.min(trace.x.length, trace.y.length, groups.length);

for(var i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i];

// TODO is this the best pattern ???
// maybe we could abstract this out
var newTrace = newData[i] = Lib.extendDeep({}, trace);

newTrace.x = [];
newTrace.y = [];

for(var j = 0; j < len; j++) {
if(groups[j] !== groupName) continue;

newTrace.x.push(trace.x[j]);
newTrace.y.push(trace.y[j]);
}

newTrace.name = groupName;
newTrace.marker.color = opts.groupColors[groupName];

Choose a reason for hiding this comment

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

We should probably set the color for each relevant trace mode, i.e.

for (var i = 0; i < modes.length; i++) {
  newTrace[modes[i]].color = opts.groupColors[groupName];
}

}

return newData;
}
0