Skip to content

Commit 06f2e6d

Browse files
committed
add support for transform plugins:
- split per-trace defaults step from plots.supplyDataDefaults - call transform module supplyDefaults at end of trace-defaults routine, so that transform default logic can rely on trace defaults - add an applyTransforms step which generates 'expanded' data which themselves go through a trace-default step. - transform functions are expected to supply an array of traces
1 parent 9ac6d0f commit 06f2e6d

File tree

1 file changed

+97
-22
lines changed

1 file changed

+97
-22
lines changed

src/plots/plots.js

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var plots = module.exports = {};
2121
var modules = plots.modules = {},
2222
allTypes = plots.allTypes = [],
2323
allCategories = plots.allCategories = {},
24-
subplotsRegistry = plots.subplotsRegistry = {};
24+
subplotsRegistry = plots.subplotsRegistry = {},
25+
transformsRegistry = plots.transformsRegistry = {};
2526

2627
plots.attributes = require('./attributes');
2728
plots.attributes.type.values = allTypes;
@@ -449,8 +450,7 @@ plots.supplyDefaults = function(gd) {
449450
newData = gd.data || [],
450451
modules = gd._modules = [];
451452

452-
var i, trace, fullTrace, _module, axList, ax;
453-
453+
var i, trace, _module, axList, ax;
454454

455455
// first fill in what we can of layout without looking at data
456456
// because fullData needs a few things from layout
@@ -460,24 +460,7 @@ plots.supplyDefaults = function(gd) {
460460
newFullLayout._dataLength = newData.length;
461461

462462
// then do the data
463-
for(i = 0; i < newData.length; i++) {
464-
trace = newData[i];
465-
466-
fullTrace = plots.supplyDataDefaults(trace, i, newFullLayout);
467-
newFullData.push(fullTrace);
468-
469-
// detect plot type
470-
if(plots.traceIs(fullTrace, 'cartesian')) newFullLayout._hasCartesian = true;
471-
else if(plots.traceIs(fullTrace, 'gl3d')) newFullLayout._hasGL3D = true;
472-
else if(plots.traceIs(fullTrace, 'geo')) newFullLayout._hasGeo = true;
473-
else if(plots.traceIs(fullTrace, 'pie')) newFullLayout._hasPie = true;
474-
else if(plots.traceIs(fullTrace, 'gl2d')) newFullLayout._hasGL2D = true;
475-
else if(plots.traceIs(fullTrace, 'ternary')) newFullLayout._hasTernary = true;
476-
else if('r' in fullTrace) newFullLayout._hasPolar = true;
477-
478-
_module = fullTrace._module;
479-
if(_module && modules.indexOf(_module)===-1) modules.push(_module);
480-
}
463+
plots.supplyDataDefaults(newData, newFullData, newFullLayout, modules);
481464

482465
// special cases that introduce interactions between traces
483466
for(i = 0; i < modules.length; i++) {
@@ -609,7 +592,61 @@ function relinkPrivateKeys(toLayout, fromLayout) {
609592
}
610593
}
611594

612-
plots.supplyDataDefaults = function(traceIn, i, layout) {
595+
plots.supplyDataDefaults = function(dataIn, dataOut, layout, modules) {
596+
var cnt = 0;
597+
598+
// push to array if item isn't already in array
599+
function fill(arr, item) {
600+
if(item && arr.indexOf(item) === -1) return arr.push(item);
601+
}
602+
603+
// detect plot type
604+
function detect(trace) {
605+
if(plots.traceIs(trace, 'cartesian')) layout._hasCartesian = true;
606+
else if(plots.traceIs(trace, 'gl3d')) layout._hasGL3D = true;
607+
else if(plots.traceIs(trace, 'geo')) layout._hasGeo = true;
608+
else if(plots.traceIs(trace, 'pie')) layout._hasPie = true;
609+
else if(plots.traceIs(trace, 'gl2d')) layout._hasGL2D = true;
610+
else if(plots.traceIs(trace, 'ternary')) layout._hasTernary = true;
611+
else if('r' in trace) layout._hasPolar = true;
612+
}
613+
614+
for(var i = 0; i < dataIn.length; i++) {
615+
var trace = dataIn[i];
616+
617+
var fullTrace = plots.supplyTraceDefaults(trace, cnt, layout);
618+
619+
// keep track of pre-transform _input
620+
var traceIn = fullTrace._input;
621+
622+
if(fullTrace.transforms && fullTrace.transforms.length) {
623+
var expandedTraces = applyTransforms(fullTrace, dataOut, layout);
624+
625+
for(var j = 0; j < expandedTraces.length; j++) {
626+
var expandedTrace = expandedTraces[j];
627+
var fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, layout);
628+
629+
// copy refs
630+
fullExpandedTrace._input = traceIn;
631+
fullExpandedTrace._fullTransforms = fullTrace.transforms;
632+
fullExpandedTrace._index = i;
633+
634+
dataOut.push(fullExpandedTrace);
635+
fill(modules, fullExpandedTrace._module);
636+
detect(fullExpandedTrace);
637+
cnt++;
638+
}
639+
}
640+
else {
641+
dataOut.push(fullTrace);
642+
fill(modules, fullTrace._module);
643+
detect(trace);
644+
cnt++;
645+
}
646+
}
647+
};
648+
649+
plots.supplyTraceDefaults = function(traceIn, i, layout) {
613650
var traceOut = {},
614651
defaultColor = Color.defaults[i % Color.defaults.length];
615652

@@ -666,6 +703,8 @@ plots.supplyDataDefaults = function(traceIn, i, layout) {
666703
coerce('showlegend');
667704
coerce('legendgroup');
668705
}
706+
707+
supplyTransformDefaults(traceIn, traceOut, layout);
669708
}
670709

671710
// 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) {
678717
return traceOut;
679718
};
680719

720+
function supplyTransformDefaults(traceIn, traceOut, layout) {
721+
var containerIn = traceIn.transforms || [],
722+
containerOut = traceOut.transforms = [];
723+
724+
for(var i = 0; i < containerIn.length; i++) {
725+
var transformIn = containerIn[i],
726+
type = transformIn.type,
727+
_module = transformsRegistry[type];
728+
729+
var transformOut = _module.supplyDefaults(transformIn, traceOut, layout);
730+
transformOut.type = type;
731+
732+
containerOut.push(transformOut);
733+
}
734+
}
735+
736+
function applyTransforms(fullTrace, fullData, layout) {
737+
var container = fullTrace.transforms,
738+
dataOut = [fullTrace];
739+
740+
for(var i = 0; i < container.length; i++) {
741+
var transform = container[i],
742+
type = transform.type,
743+
_module = transformsRegistry[type];
744+
745+
dataOut = _module.transform(dataOut, {
746+
transform: transform,
747+
fullTrace: fullTrace,
748+
fullData: fullData,
749+
layout: layout
750+
});
751+
}
752+
753+
return dataOut;
754+
}
755+
681756
plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
682757
function coerce(attr, dflt) {
683758
return Lib.coerce(layoutIn, layoutOut, plots.layoutAttributes, attr, dflt);

0 commit comments

Comments
 (0)