Skip to content

Commit 64dc72a

Browse files
committed
transforms: make findArrayAttributes a lib function
- instead of calling it for every transform trace - call findArrayAttributes in transform function - make findArrayAttributes 'aware' of array attribute in trace so that it lists only the array attributes present in given trace
1 parent 0158f45 commit 64dc72a

File tree

4 files changed

+56
-43
lines changed

4 files changed

+56
-43
lines changed

src/lib/coerce.js

+50
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,53 @@ exports.crawl = function(attrs, callback, specifiedLevel) {
409409
if(isPlainObject(attr)) exports.crawl(attr, callback, level + 1);
410410
});
411411
};
412+
413+
/**
414+
* Find all data array attributes in a given trace object - including
415+
* `arrayOk` attributes.
416+
*
417+
* @param {object} trace
418+
* full trace object that contains a reference to `_module.attributes`
419+
*
420+
* @return {array} arrayAttributes
421+
* list of array attributes for the given trace
422+
*/
423+
exports.findArrayAttributes = function(trace) {
424+
var arrayAttributes = [],
425+
stack = [];
426+
427+
/**
428+
* A closure that gathers attribute paths into its enclosed arraySplitAttributes
429+
* Attribute paths are collected iff their leaf node is a splittable attribute
430+
*
431+
* @callback callback
432+
* @param {object} attr an attribute
433+
* @param {String} attrName name string
434+
* @param {object[]} attrs all the attributes
435+
* @param {Number} level the recursion level, 0 at the root
436+
*
437+
* @closureVariable {String[][]} arrayAttributes the set of gathered attributes
438+
* Example of filled closure variable (expected to be initialized to []):
439+
* [["marker","size"],["marker","line","width"],["marker","line","color"]]
440+
*/
441+
function callback(attr, attrName, attrs, level) {
442+
stack = stack.slice(0, level).concat([attrName]);
443+
444+
var splittableAttr = attr.valType === 'data_array' || attr.arrayOk === true;
445+
if(!splittableAttr) return;
446+
447+
var astr = toAttrString(stack);
448+
var val = nestedProperty(trace, astr).get();
449+
if(!Array.isArray(val)) return;
450+
451+
arrayAttributes.push(astr);
452+
}
453+
454+
function toAttrString(stack) {
455+
return stack.join('.');
456+
}
457+
458+
exports.crawl(trace._module.attributes, callback);
459+
460+
return arrayAttributes;
461+
};

src/lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ lib.coerceFont = coerceModule.coerceFont;
2525
lib.validate = coerceModule.validate;
2626
lib.isValObject = coerceModule.isValObject;
2727
lib.crawl = coerceModule.crawl;
28+
lib.findArrayAttributes = coerceModule.findArrayAttributes;
2829
lib.IS_SUBPLOT_OBJ = coerceModule.IS_SUBPLOT_OBJ;
2930
lib.IS_LINKED_TO_ARRAY = coerceModule.IS_LINKED_TO_ARRAY;
3031
lib.DEPRECATED = coerceModule.DEPRECATED;

src/plots/plots.js

-36
Original file line numberDiff line numberDiff line change
@@ -786,41 +786,6 @@ function applyTransforms(fullTrace, fullData, layout) {
786786
var container = fullTrace.transforms,
787787
dataOut = [fullTrace];
788788

789-
var attributeSets = dataOut.map(function(trace) {
790-
791-
var arraySplitAttributes = [];
792-
793-
var stack = [];
794-
795-
/**
796-
* A closure that gathers attribute paths into its enclosed arraySplitAttributes
797-
* Attribute paths are collected iff their leaf node is a splittable attribute
798-
* @callback callback
799-
* @param {object} attr an attribute
800-
* @param {String} attrName name string
801-
* @param {object[]} attrs all the attributes
802-
* @param {Number} level the recursion level, 0 at the root
803-
* @closureVariable {String[][]} arraySplitAttributes the set of gathered attributes
804-
* Example of filled closure variable (expected to be initialized to []):
805-
* [["marker","size"],["marker","line","width"],["marker","line","color"]]
806-
*/
807-
function callback(attr, attrName, attrs, level) {
808-
809-
stack = stack.slice(0, level).concat([attrName]);
810-
811-
var splittableAttr = attr.valType === 'data_array' || attr.arrayOk === true;
812-
if(splittableAttr) {
813-
arraySplitAttributes.push(stack.slice());
814-
}
815-
}
816-
817-
Lib.crawl(trace._module.attributes, callback);
818-
819-
return arraySplitAttributes.map(function(path) {
820-
return path.join('.');
821-
});
822-
});
823-
824789
for(var i = 0; i < container.length; i++) {
825790
var transform = container[i],
826791
type = transform.type,
@@ -831,7 +796,6 @@ function applyTransforms(fullTrace, fullData, layout) {
831796
transform: transform,
832797
fullTrace: fullTrace,
833798
fullData: fullData,
834-
attributeSets: attributeSets,
835799
layout: layout,
836800
transformIndex: i
837801
});

src/transforms/groupby.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ function pasteArray(newTrace, trace, j, a) {
103103
);
104104
}
105105

106-
function transformOne(trace, state, attributeSet) {
107-
106+
function transformOne(trace, state) {
108107
var opts = state.transform;
109108
var groups = trace.transforms[state.transformIndex].groups;
110109

@@ -119,10 +118,9 @@ function transformOne(trace, state, attributeSet) {
119118
var newData = new Array(groupNames.length);
120119
var len = groups.length;
121120

122-
var style = opts.style || {};
121+
var arrayAttrs = Lib.findArrayAttributes(trace);
123122

124-
var arrayAttributes = attributeSet
125-
.filter(function(array) {return Array.isArray(Lib.nestedProperty(trace, array).get());});
123+
var style = opts.style || {};
126124

127125
for(var i = 0; i < groupNames.length; i++) {
128126
var groupName = groupNames[i];
@@ -131,12 +129,12 @@ function transformOne(trace, state, attributeSet) {
131129
// maybe we could abstract this out
132130
var newTrace = newData[i] = Lib.extendDeep({}, trace);
133131

134-
arrayAttributes.forEach(initializeArray.bind(null, newTrace));
132+
arrayAttrs.forEach(initializeArray.bind(null, newTrace));
135133

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

139-
arrayAttributes.forEach(pasteArray.bind(0, newTrace, trace, j));
137+
arrayAttrs.forEach(pasteArray.bind(0, newTrace, trace, j));
140138
}
141139

142140
newTrace.name = groupName;

0 commit comments

Comments
 (0)