-
-
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
Changes from 10 commits
53a5971
9909382
28147e2
9ac6d0f
06f2e6d
8ab9d0c
2697975
56665f2
aa274ef
a06441a
0b62e45
1c651e8
f93b9c1
ccbcb58
37e291b
383c6d4
be36402
0e1eb5a
6a8e158
9a341c1
1752d30
b5ec01e
a6fda02
da2c24c
8d7dc46
320d62e
3e6e9c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ var plots = module.exports = {}; | |
var modules = plots.modules = {}, | ||
allTypes = plots.allTypes = [], | ||
allCategories = plots.allCategories = {}, | ||
subplotsRegistry = plots.subplotsRegistry = {}; | ||
subplotsRegistry = plots.subplotsRegistry = {}, | ||
transformsRegistry = plots.transformsRegistry = {}; | ||
|
||
plots.attributes = require('./attributes'); | ||
plots.attributes.type.values = allTypes; | ||
|
@@ -449,8 +450,7 @@ plots.supplyDefaults = function(gd) { | |
newData = gd.data || [], | ||
modules = gd._modules = []; | ||
|
||
var i, trace, fullTrace, _module, axList, ax; | ||
|
||
var i, trace, _module, axList, ax; | ||
|
||
// first fill in what we can of layout without looking at data | ||
// because fullData needs a few things from layout | ||
|
@@ -460,24 +460,7 @@ plots.supplyDefaults = function(gd) { | |
newFullLayout._dataLength = newData.length; | ||
|
||
// then do the data | ||
for(i = 0; i < newData.length; i++) { | ||
trace = newData[i]; | ||
|
||
fullTrace = plots.supplyDataDefaults(trace, i, newFullLayout); | ||
newFullData.push(fullTrace); | ||
|
||
// detect plot type | ||
if(plots.traceIs(fullTrace, 'cartesian')) newFullLayout._hasCartesian = true; | ||
else if(plots.traceIs(fullTrace, 'gl3d')) newFullLayout._hasGL3D = true; | ||
else if(plots.traceIs(fullTrace, 'geo')) newFullLayout._hasGeo = true; | ||
else if(plots.traceIs(fullTrace, 'pie')) newFullLayout._hasPie = true; | ||
else if(plots.traceIs(fullTrace, 'gl2d')) newFullLayout._hasGL2D = true; | ||
else if(plots.traceIs(fullTrace, 'ternary')) newFullLayout._hasTernary = true; | ||
else if('r' in fullTrace) newFullLayout._hasPolar = true; | ||
|
||
_module = fullTrace._module; | ||
if(_module && modules.indexOf(_module)===-1) modules.push(_module); | ||
} | ||
plots.supplyDataDefaults(newData, newFullData, newFullLayout, modules); | ||
|
||
// special cases that introduce interactions between traces | ||
for(i = 0; i < modules.length; i++) { | ||
|
@@ -609,7 +592,61 @@ function relinkPrivateKeys(toLayout, fromLayout) { | |
} | ||
} | ||
|
||
plots.supplyDataDefaults = function(traceIn, i, layout) { | ||
plots.supplyDataDefaults = function(dataIn, dataOut, layout, modules) { | ||
var cnt = 0; | ||
|
||
// push to array if item isn't already in array | ||
function fill(arr, item) { | ||
if(item && arr.indexOf(item) === -1) return arr.push(item); | ||
} | ||
|
||
// detect plot type | ||
function detect(trace) { | ||
if(plots.traceIs(trace, 'cartesian')) layout._hasCartesian = true; | ||
else if(plots.traceIs(trace, 'gl3d')) layout._hasGL3D = true; | ||
else if(plots.traceIs(trace, 'geo')) layout._hasGeo = true; | ||
else if(plots.traceIs(trace, 'pie')) layout._hasPie = true; | ||
else if(plots.traceIs(trace, 'gl2d')) layout._hasGL2D = true; | ||
else if(plots.traceIs(trace, 'ternary')) layout._hasTernary = true; | ||
else if('r' in trace) layout._hasPolar = true; | ||
} | ||
|
||
for(var i = 0; i < dataIn.length; i++) { | ||
var trace = dataIn[i]; | ||
|
||
var fullTrace = plots.supplyTraceDefaults(trace, cnt, layout); | ||
|
||
// keep track of pre-transform _input | ||
var traceIn = fullTrace._input; | ||
|
||
if(fullTrace.transforms && fullTrace.transforms.length) { | ||
var expandedTraces = applyTransforms(fullTrace, dataOut, layout); | ||
|
||
for(var j = 0; j < expandedTraces.length; j++) { | ||
var expandedTrace = expandedTraces[j]; | ||
var fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, layout); | ||
|
||
// copy refs | ||
fullExpandedTrace._input = traceIn; | ||
fullExpandedTrace._fullTransforms = fullTrace.transforms; | ||
fullExpandedTrace._index = i; | ||
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. not sure what we need 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 need it to set the 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.
Good call. I like the sound of 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. If ok, I would not rename |
||
|
||
dataOut.push(fullExpandedTrace); | ||
fill(modules, fullExpandedTrace._module); | ||
detect(fullExpandedTrace); | ||
cnt++; | ||
} | ||
} | ||
else { | ||
dataOut.push(fullTrace); | ||
fill(modules, fullTrace._module); | ||
detect(trace); | ||
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.
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. in fact, this untransformed case needs function addFinalTrace(finalTrace) {
finalTrace._input = trace;
finalTrace._inputIndex = i;
finalTrace._expandedIndex = cnt;
dataOut.push(finalTrace);
fill(modules, finalTrace);
detect(finalTrace);
cnt++;
} since all of that needs to happen identically whichever way you added a trace to 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. Good catch ⚾ . Thanks! |
||
cnt++; | ||
} | ||
} | ||
}; | ||
|
||
plots.supplyTraceDefaults = function(traceIn, i, layout) { | ||
var traceOut = {}, | ||
defaultColor = Color.defaults[i % Color.defaults.length]; | ||
|
||
|
@@ -666,6 +703,8 @@ plots.supplyDataDefaults = function(traceIn, i, layout) { | |
coerce('showlegend'); | ||
coerce('legendgroup'); | ||
} | ||
|
||
supplyTransformDefaults(traceIn, traceOut, layout); | ||
} | ||
|
||
// NOTE: I didn't include fit info at all... for now I think it can stay | ||
|
@@ -678,6 +717,42 @@ plots.supplyDataDefaults = function(traceIn, i, layout) { | |
return traceOut; | ||
}; | ||
|
||
function supplyTransformDefaults(traceIn, traceOut, layout) { | ||
var containerIn = traceIn.transforms || [], | ||
containerOut = traceOut.transforms = []; | ||
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. I think this line here ensures that every fullTrace will now have a transforms array? Most of the time it will be empty. Some tests that have nothing to do with transforms are failing due to an empty transforms array appearing. I think this is the culprit. 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. No problem. I can remove it. 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. just verified. if(!Array.isArray(traceIn.transforms)) {
return;
} at the top of this function remedies the issue. Though perhaps this breaks other things. Not sure. |
||
|
||
for(var i = 0; i < containerIn.length; i++) { | ||
var transformIn = containerIn[i], | ||
type = transformIn.type, | ||
_module = transformsRegistry[type]; | ||
|
||
var transformOut = _module.supplyDefaults(transformIn, traceOut, layout); | ||
transformOut.type = type; | ||
|
||
containerOut.push(transformOut); | ||
} | ||
} | ||
|
||
function applyTransforms(fullTrace, fullData, layout) { | ||
var container = fullTrace.transforms, | ||
dataOut = [fullTrace]; | ||
|
||
for(var i = 0; i < container.length; i++) { | ||
var transform = container[i], | ||
type = transform.type, | ||
_module = transformsRegistry[type]; | ||
|
||
dataOut = _module.transform(dataOut, { | ||
transform: transform, | ||
fullTrace: fullTrace, | ||
fullData: fullData, | ||
layout: layout | ||
}); | ||
} | ||
|
||
return dataOut; | ||
} | ||
|
||
plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) { | ||
function coerce(attr, dflt) { | ||
return Lib.coerce(layoutIn, layoutOut, plots.layoutAttributes, attr, dflt); | ||
|
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.
isn't this just
trace
?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.
Correct. Thanks for checking.