Skip to content

Commit 4398bd7

Browse files
committed
Clear expanded trace colors between supplydefault passes
1 parent 476c8b7 commit 4398bd7

File tree

7 files changed

+93
-19
lines changed

7 files changed

+93
-19
lines changed

src/components/colorscale/color_attributes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = function makeColorScaleAttributes(context) {
2323
' or an array of numbers that are mapped to the colorscale',
2424
' relative to the max and min values of the array or relative to',
2525
' `cmin` and `cmax` if set.'
26-
].join('')
26+
].join(''),
27+
useExpandedTraceDefaultColor: true
2728
},
2829
colorscale: extendDeep({}, colorScaleAttributes.colorscale, {
2930
description: [

src/plots/plots.js

+35
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,39 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
659659
}
660660
};
661661

662+
// This function clears defaults between the first and second pass of
663+
// supplyDefaults. It exists because otherwise null attributes are
664+
// supplyDefault'd and inherited as *colors* instead of an actual null
665+
// attribute which needs to be supplydefaulted by the individual
666+
// expanded traces.
667+
plots.clearExpandedTraceDefaultColors = function(expandedTraces) {
668+
var colorAttrs, path, trace, i, j;
669+
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) {
674+
path[level] = attrName;
675+
path.length = level + 1;
676+
if(attr.useExpandedTraceDefaultColor) {
677+
colorAttrs.push(path.join('.'));
678+
}
679+
}
680+
681+
for(i = 0; i < expandedTraces.length; i++) {
682+
trace = expandedTraces[i];
683+
colorAttrs = [];
684+
path = [];
685+
686+
PlotSchema.crawl(trace._module.attributes, locateExpandedTraceAttrs);
687+
688+
for(j = 0; j < colorAttrs.length; j++) {
689+
Lib.nestedProperty(trace, colorAttrs[j]).set(null);
690+
}
691+
}
692+
};
693+
694+
662695
plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
663696
var i, fullTrace, trace;
664697
var modules = fullLayout._modules = [],
@@ -693,6 +726,8 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
693726
if(fullTrace.transforms && fullTrace.transforms.length) {
694727
var expandedTraces = applyTransforms(fullTrace, dataOut, layout, fullLayout);
695728

729+
plots.clearExpandedTraceDefaultColors(expandedTraces);
730+
696731
for(var j = 0; j < expandedTraces.length; j++) {
697732
var expandedTrace = expandedTraces[j],
698733
fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, fullLayout, i);

src/traces/scatter/attributes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ module.exports = {
124124
color: {
125125
valType: 'color',
126126
role: 'style',
127-
description: 'Sets the line color.'
127+
description: 'Sets the line color.',
128+
useExpandedTraceDefaultColor: true
128129
},
129130
width: {
130131
valType: 'number',
@@ -203,6 +204,7 @@ module.exports = {
203204
fillcolor: {
204205
valType: 'color',
205206
role: 'style',
207+
useExpandedTraceDefaultColor: true,
206208
description: [
207209
'Sets the fill color.',
208210
'Defaults to a half-transparent variant of the line color,',
24.4 KB
Loading
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"data": [{
3+
"x": [2, 3, 4, 5, 6, 7],
4+
"y": [3, 2, 4, 3, 5, 4],
5+
"transforms": [{
6+
"type": "groupby",
7+
"groups": ["c", "b", "d", "c", "e", "d"]
8+
}]
9+
}, {
10+
"x": [2, 3, 4, 5, 6, 7],
11+
"y": [9, 8, 10, 9, 11, 10],
12+
"marker": {
13+
"symbol": "square"
14+
},
15+
"transforms": [{
16+
"type": "groupby",
17+
"groups": ["c", "b", "d", "c", "e", "d"]
18+
}]
19+
}],
20+
"layout": {
21+
"title": "Groupby expanded trace coloring"
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var Plots = require('@src/plots/plots');
2+
3+
describe('groupby', function() {
4+
it('varies the color for each expanded trace', function() {
5+
var uniqueColors = {};
6+
var dataOut = [];
7+
var dataIn = [{
8+
y: [1, 2, 3],
9+
transforms: [
10+
{type: 'filter', operation: '<', value: 4},
11+
{type: 'groupby', groups: ['a', 'b', 'c']}
12+
]
13+
}, {
14+
y: [4, 5, 6],
15+
transforms: [
16+
{type: 'filter', operation: '<', value: 4},
17+
{type: 'groupby', groups: ['a', 'b', 'b']}
18+
]
19+
}];
20+
21+
Plots.supplyDataDefaults(dataIn, dataOut, {}, {});
22+
23+
for(var i = 0; i < dataOut.length; i++) {
24+
uniqueColors[dataOut[i].marker.color] = true;
25+
}
26+
27+
// Confirm that five total colors exist:
28+
expect(Object.keys(uniqueColors).length).toEqual(5);
29+
});
30+
});

test/jasmine/tests/transform_groupby_test.js

-17
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,6 @@ describe('groupby', function() {
6363
});
6464
});
6565

66-
it('varies the color for each expanded trace', function () {
67-
var uniqueColors = {};
68-
var dataOut = [];
69-
var dataIn = [{
70-
y: [1, 2, 3],
71-
transforms: [{type: 'groupby', groups: ['a', 'b', 'c']}]
72-
}];
73-
74-
Plots.supplyDataDefaults(dataIn, dataOut, {}, {});
75-
76-
for (var i = 0; i < dataOut.length; i++) {
77-
uniqueColors[dataOut[i].marker.color] = true;
78-
}
79-
80-
expect(Object.keys(uniqueColors).length).toEqual(3);
81-
});
82-
8366
it('Accepts deprecated object notation for styles', function(done) {
8467
var oldStyleMockData = [{
8568
mode: 'markers',

0 commit comments

Comments
 (0)