Skip to content

Unique colors for expanded traces #1830

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

Merged
merged 8 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/colorscale/color_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = function makeColorScaleAttributes(context) {
' or an array of numbers that are mapped to the colorscale',
' relative to the max and min values of the array or relative to',
' `cmin` and `cmax` if set.'
].join('')
].join(''),
useExpandedTraceDefaultColor: true
},
colorscale: extendDeep({}, colorScaleAttributes.colorscale, {
description: [
Expand Down
37 changes: 37 additions & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,41 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
}
};

// This function clears defaults between the first and second pass of
// supplyDefaults. It exists because otherwise null attributes are
// supplyDefault'd and inherited as *colors* instead of an actual null
// attribute which needs to be supplydefaulted by the individual
// expanded traces.
plots.clearExpandedTraceDefaultColors = function(expandedTraces) {
var colorAttrs, path, trace, i, j;

// A better check *might* be to explicitly check for a groupby transform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better for sure, but I'm ok with this for now, as this will never get called for transform-less traces.

Maybe we start thinking about adding transform module categories (similar to our trace module categories) to help us out.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a hypothetical for now, since groupby is currently the only one that can yield more than one trace... but my rationale for suggesting this was that it's precisely this condition that breaks default colors. We'll see what happens if/when we add more such transforms, if this is insufficient then a category system is a great idea!

if(expandedTraces.length <= 1) return;

function locateExpandedTraceAttrs(attr, attrName, attrs, level) {
path[level] = attrName;
path.length = level + 1;
if(attr.useExpandedTraceDefaultColor) {
colorAttrs.push(path.join('.'));
}
}

for(i = 0; i < expandedTraces.length; i++) {
trace = expandedTraces[i];
colorAttrs = [];
path = [];

if(!trace || !trace._module) continue;

PlotSchema.crawl(trace._module.attributes, locateExpandedTraceAttrs);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be worthwhile to cache colorAttrs in _module so we don't need to crawl the schema with every supplyDefaults?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Will do.


for(j = 0; j < colorAttrs.length; j++) {
Lib.nestedProperty(trace, colorAttrs[j]).set(null);
}
}
};


plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
var i, fullTrace, trace;
var modules = fullLayout._modules = [],
Expand Down Expand Up @@ -693,6 +728,8 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
if(fullTrace.transforms && fullTrace.transforms.length) {
var expandedTraces = applyTransforms(fullTrace, dataOut, layout, fullLayout);

plots.clearExpandedTraceDefaultColors(expandedTraces);

for(var j = 0; j < expandedTraces.length; j++) {
var expandedTrace = expandedTraces[j],
fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, fullLayout, i);
Expand Down
4 changes: 3 additions & 1 deletion src/traces/scatter/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ module.exports = {
color: {
valType: 'color',
role: 'style',
description: 'Sets the line color.'
description: 'Sets the line color.',
useExpandedTraceDefaultColor: true
},
width: {
valType: 'number',
Expand Down Expand Up @@ -203,6 +204,7 @@ module.exports = {
fillcolor: {
valType: 'color',
role: 'style',
useExpandedTraceDefaultColor: true,
description: [
'Sets the fill color.',
'Defaults to a half-transparent variant of the line color,',
Expand Down
Binary file added test/image/baselines/groupby_coloring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/image/mocks/groupby_coloring.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [{
"x": [2, 3, 4, 5, 6, 7],
"y": [3, 2, 4, 3, 5, 4],
"transforms": [{
"type": "groupby",
"groups": ["c", "b", "d", "c", "e", "d"]
}]
}, {
"x": [2, 3, 4, 5, 6, 7],
"y": [9, 8, 10, 9, 11, 10],
"marker": {
"symbol": "square"
},
"transforms": [{
"type": "groupby",
"groups": ["c", "b", "d", "c", "e", "d"]
}]
}],
"layout": {
"title": "Groupby expanded trace coloring"
}
}
30 changes: 30 additions & 0 deletions test/jasmine/tests/transform_expanded_trace_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var Plots = require('@src/plots/plots');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a new file here.

Can you move this to either transform_groupby_test.js or the poorly-named transform_multi_test.js?


describe('groupby', function() {
it('varies the color for each expanded trace', function() {
var uniqueColors = {};
var dataOut = [];
var dataIn = [{
y: [1, 2, 3],
transforms: [
{type: 'filter', operation: '<', value: 4},
{type: 'groupby', groups: ['a', 'b', 'c']}
]
}, {
y: [4, 5, 6],
transforms: [
{type: 'filter', operation: '<', value: 4},
{type: 'groupby', groups: ['a', 'b', 'b']}
]
}];

Plots.supplyDataDefaults(dataIn, dataOut, {}, {});

for(var i = 0; i < dataOut.length; i++) {
uniqueColors[dataOut[i].marker.color] = true;
}

// Confirm that five total colors exist:
expect(Object.keys(uniqueColors).length).toEqual(5);
});
});