-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
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 9909382
lint
etpinard 28147e2
add check so that we don't try to destroy gd if it DNE
etpinard 9ac6d0f
make Plotly.register handle transform modules
etpinard 06f2e6d
add support for transform plugins:
etpinard 8ab9d0c
make Plotly.restyle support transforms:
etpinard 2697975
add two example transform modules
etpinard 56665f2
add transform test suite
etpinard aa274ef
update tests (plots.supplyDataDefaults -> supplyTraceDefaults)
etpinard a06441a
[tmp commit] add example transforms to ./lib/ Plotly for testing
etpinard 0b62e45
Merge branch 'master' into transform-plugins
etpinard 1c651e8
toimage_test: make sure that all graph divs are deleted
etpinard f93b9c1
plots: rm useless skip-over-step
etpinard ccbcb58
Merge branch 'master' into transform-plugins
etpinard 37e291b
plots: make relinkPrivateKeys less strict
etpinard 383c6d4
move common code in pushModule func
etpinard be36402
rm useless comment about fits ;)
etpinard 0e1eb5a
do not add transforms container if fullTrace is no transform present
etpinard 6a8e158
try more useless refs to input container in full trace
etpinard 9a341c1
mutate uid of expanded traces w.r.t to expanded index:
etpinard 1752d30
make uid of expanded trace valid alpha-numerical hashes
etpinard b5ec01e
add support for transform in plot schema and Plotly.validate
etpinard a6fda02
improve validate test cases
etpinard da2c24c
validate transform module on registration:
etpinard 8d7dc46
improve transform tests
etpinard 320d62e
Revert "[tmp commit] add example transforms to ./lib/ Plotly for test…
etpinard 3e6e9c1
rm @assets path shortcut,
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add two example transform modules
- Loading branch information
commit 2697975cb392ec8dc0a72c6440f9e36dd9a7693a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { | ||
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also be useful to have a |
||
|
||
/** | ||
* 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'); | ||
6D40 td> | 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, perhapsticktext
andtickvals
might be special as well.)There was a problem hiding this comment.
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 fordata_array
attributes in the incoming traces. Here, onlydata_array
attributes would have to be truncated.