Skip to content

Commit 5f346e4

Browse files
committed
Call clearTraceDefaultColors from groupby
1 parent 0877f48 commit 5f346e4

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

src/plots/plots.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -664,31 +664,38 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
664664
// supplyDefault'd and inherited as *colors* instead of an actual null
665665
// attribute which needs to be supplydefaulted by the individual
666666
// expanded traces.
667-
plots.clearExpandedTraceDefaultColors = function(expandedTraces) {
668-
var colorAttrs, path, trace, i, j;
667+
plots.clearTraceDefaultColors = function(trace) {
668+
var colorAttrs, path, i;
669669

670-
// A better check *might* be to explicitly check for a groupby transform
671-
if(expandedTraces.length <= 1) return;
672-
673-
function locateExpandedTraceAttrs(attr, attrName, attrs, level) {
670+
// This uses weird closure state in order to satisfy the linter rule
671+
// that we can't create functions in a loop.
672+
function locateColorAttrs(attr, attrName, attrs, level) {
674673
path[level] = attrName;
675674
path.length = level + 1;
676675
if(attr.useExpandedTraceDefaultColor) {
677676
colorAttrs.push(path.join('.'));
678677
}
679678
}
680679

681-
for(i = 0; i < expandedTraces.length; i++) {
682-
trace = expandedTraces[i];
683-
colorAttrs = [];
684-
path = [];
680+
path = [];
685681

686-
if(!trace || !trace._module) continue;
682+
// Get the cached colorAttrs:
683+
colorAttrs = trace._module._colorAttrs;
687684

688-
PlotSchema.crawl(trace._module.attributes, locateExpandedTraceAttrs);
685+
// Or else compute and cache the colorAttrs on the module:
686+
if(!colorAttrs) {
687+
trace._module._colorAttrs = colorAttrs = [];
688+
PlotSchema.crawl(
689+
trace._module.attributes,
690+
locateColorAttrs
691+
);
692+
}
689693

690-
for(j = 0; j < colorAttrs.length; j++) {
691-
Lib.nestedProperty(trace, colorAttrs[j]).set(null);
694+
for(i = 0; i < colorAttrs.length; i++) {
695+
var origprop = Lib.nestedProperty(trace, '_input.' + colorAttrs[i]);
696+
697+
if(!origprop.get()) {
698+
Lib.nestedProperty(trace, colorAttrs[i]).set(null);
692699
}
693700
}
694701
};
@@ -728,11 +735,9 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
728735
if(fullTrace.transforms && fullTrace.transforms.length) {
729736
var expandedTraces = applyTransforms(fullTrace, dataOut, layout, fullLayout);
730737

731-
plots.clearExpandedTraceDefaultColors(expandedTraces);
732-
733738
for(var j = 0; j < expandedTraces.length; j++) {
734-
var expandedTrace = expandedTraces[j],
735-
fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, fullLayout, i);
739+
var expandedTrace = expandedTraces[j];
740+
var fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, fullLayout, i);
736741

737742
// mutate uid here using parent uid and expanded index
738743
// to promote consistency between update calls

src/transforms/groupby.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var Lib = require('../lib');
1212
var PlotSchema = require('../plot_api/plot_schema');
13+
var Plots = require('../plots/plots');
1314

1415
exports.moduleType = 'transform';
1516

@@ -172,6 +173,8 @@ function transformOne(trace, state) {
172173

173174
newTrace.name = groupName;
174175

176+
Plots.clearTraceDefaultColors(newTrace);
177+
175178
// there's no need to coerce styleLookup[groupName] here
176179
// as another round of supplyDefaults is done on the transformed traces
177180
newTrace = Lib.extendDeepNoArrays(newTrace, styleLookup[groupName] || {});

test/jasmine/tests/plotschema_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('plot schema', function() {
7676
var valObject = valObjects[attr.valType],
7777
opts = valObject.requiredOpts
7878
.concat(valObject.otherOpts)
79-
.concat(['valType', 'description', 'role', 'editType']);
79+
.concat(['valType', 'description', 'role', 'editType', 'useExpandedTraceDefaultColor']);
8080

8181
Object.keys(attr).forEach(function(key) {
8282
expect(opts.indexOf(key) !== -1).toBe(true, key, attr);

test/jasmine/tests/transform_groupby_test.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Plotly = require('@lib/index');
2+
var Plots = require('@src/plots/plots');
23
var Lib = require('@src/lib');
34

45
var createGraphDiv = require('../assets/create_graph_div');
@@ -236,9 +237,38 @@ describe('groupby', function() {
236237
done();
237238
});
238239
});
240+
});
241+
242+
describe('many-to-many transforms', function() {
243+
it('varies the color for each expanded trace', function() {
244+
var uniqueColors = {};
245+
var dataOut = [];
246+
var dataIn = [{
247+
y: [1, 2, 3],
248+
transforms: [
249+
{type: 'filter', operation: '<', value: 4},
250+
{type: 'groupby', groups: ['a', 'b', 'c']}
251+
]
252+
}, {
253+
y: [4, 5, 6],
254+
transforms: [
255+
{type: 'filter', operation: '<', value: 4},
256+
{type: 'groupby', groups: ['a', 'b', 'b']}
257+
]
258+
}];
239259

260+
Plots.supplyDataDefaults(dataIn, dataOut, {}, {});
261+
262+
for(var i = 0; i < dataOut.length; i++) {
263+
uniqueColors[dataOut[i].marker.color] = true;
264+
}
265+
266+
// Confirm that five total colors exist:
267+
expect(Object.keys(uniqueColors).length).toEqual(5);
268+
});
240269
});
241270

271+
242272
// these tests can be shortened, once the meaning of edge cases gets clarified
243273
describe('symmetry/degeneracy testing of one-to-many transforms on arbitrary arrays where there is no grouping (implicit 1):', function() {
244274
'use strict';

0 commit comments

Comments
 (0)