Skip to content

adding layout.colorway #2

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ function cleanAxRef(container, attr) {

// Make a few changes to the data right away
// before it gets used for anything
exports.cleanData = function(data, existingData) {
exports.cleanData = function(data, existingData, colorway) {
Copy link

Choose a reason for hiding this comment

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

cleanData is intended for legacy code only, so maybe we don't have to propagate colorway here.

var defaultColors = colorway || Color.defaults;
// Enforce unique IDs
var suids = [], // seen uids --- so we can weed out incoming repeats
uids = data.concat(Array.isArray(existingData) ? existingData : [])
Expand Down Expand Up @@ -229,7 +230,7 @@ exports.cleanData = function(data, existingData) {

// error_y.opacity is obsolete - merge into color
if(trace.error_y && 'opacity' in trace.error_y) {
var dc = Color.defaults,
var dc = defaultColors,
yeColor = trace.error_y.color ||
(Registry.traceIs(trace, 'bar') ? Color.defaultLine : dc[tracei % dc.length]);
trace.error_y.color = Color.addOpacity(
Expand Down
6 changes: 3 additions & 3 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Plotly.plot = function(gd, data, layout, config) {
// if there is already data on the graph, append the new data
// if you only want to redraw, pass a non-array for data
if(Array.isArray(data)) {
helpers.cleanData(data, gd.data);
helpers.cleanData(data, gd.data, layout.colorway);

if(graphWasEmpty) gd.data = data;
else gd.data.push.apply(gd.data, data);
Expand Down Expand Up @@ -587,7 +587,7 @@ Plotly.redraw = function(gd) {
throw new Error('This element is not a Plotly plot: ' + gd);
}

helpers.cleanData(gd.data, gd.data);
helpers.cleanData(gd.data, gd.data, gd.layout.colorway);
helpers.cleanLayout(gd.layout);

gd.calcdata = undefined;
Expand Down Expand Up @@ -1045,7 +1045,7 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
return Lib.extendFlat({}, trace);
});

helpers.cleanData(traces, gd.data);
helpers.cleanData(traces, gd.data, gd.layout.colorway);

// add the traces to gd.data (no redrawing yet!)
for(i = 0; i < traces.length; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/plots/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,6 @@ module.exports = {
role: 'info',
editType: 'legend',
description: 'Determines whether or not a legend is drawn.'
}
},
colorway: colorAttrs.defaults,
Copy link

Choose a reason for hiding this comment

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

This should be:

colorway: {
  valType: 'any', // for now ;)
  dflt: colorAttrs.defaults,
  role: 'style',
  editType: 'plot' // (maybe 'calc')
  description: 'describe this thing!'
}

};
6 changes: 5 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ plots.supplyDefaults = function(gd) {

newFullLayout._initialAutoSizeIsDone = true;

// pass along trace colors
newFullLayout._colorway = newLayout.colorway;
Copy link

Choose a reason for hiding this comment

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

That works, but we can do better. Ideally, we should use Lib.coerce to validate and map colorway from input layout to full layout. layout.colorway is a bit of a special case: we don't have a valType that corresponds to the desired behavior. That said, we should at the very least make sure that colorway is an array and (manually) validate every color in the colorway.


// keep track of how many traces are inputted
newFullLayout._dataLength = newData.length;

Expand Down Expand Up @@ -950,8 +953,9 @@ plots.supplyFrameDefaults = function(frameIn) {
};

plots.supplyTraceDefaults = function(traceIn, traceOutIndex, layout, traceInIndex) {
var colorway = layout._colorway || Color.defaults;
var traceOut = {},
defaultColor = Color.defaults[traceOutIndex % Color.defaults.length];
defaultColor = colorway[traceOutIndex % colorway.length];

function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, plots.attributes, attr, dflt);
Expand Down
8 changes: 5 additions & 3 deletions src/traces/pie/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ module.exports = function calc(gd, trace) {
pt.color = colorMap[pt.label];
}
else {
colorMap[pt.label] = pt.color = nextDefaultColor(fullLayout._piedefaultcolorcount);
colorMap[pt.label] = pt.color = nextDefaultColor(
fullLayout._piedefaultcolorcount,
fullLayout._colorway);
fullLayout._piedefaultcolorcount++;
}
}
Expand Down Expand Up @@ -148,10 +150,10 @@ module.exports = function calc(gd, trace) {
*/
var pieDefaultColors;

function nextDefaultColor(index) {
function nextDefaultColor(index, colorway) {
if(!pieDefaultColors) {
// generate this default set on demand (but then it gets saved in the module)
var mainDefaults = Color.defaults;
var mainDefaults = colorway || Color.defaults;
pieDefaultColors = mainDefaults.slice();

var i;
Expand Down
32 changes: 32 additions & 0 deletions test/image/mocks/layout-colorway.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"data": [
{"y": [8, 7, 7, 6, 5, 4, 4, 3, 2, 2, 3]},
{"y": [7, 7, 6, 5, 4, 4, 3, 2, 2, 3, 8]},
{"y": [7, 6, 5, 4, 4, 3, 2, 2, 3, 8, 7]},
{"y": [6, 5, 4, 4, 3, 2, 2, 3, 8, 7, 7]},
{"y": [5, 4, 4, 3, 2, 2, 3, 8, 7, 7, 6]},
{
"labels": ["a","b","c","c","c","a","d","e"],
"type": "pie",
"domain": {"x": [0, 0.4]},
"xaxis": "x2",
"yaxis": "y2"
}
],
"layout": {
"title": "Custom Trace Color Defaults",
"colorway": [
"#DE5845",
"#E83898",
"#A83DD1",
"#5A38E8",
"#3C71DE"
],
"xaxis": {
"domain": [0.4, 1]
},
"yaxis": {
"anchor": "y2"
}
}
}