Skip to content

adding traceColorDefaults config #1

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ 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, traceColors) {
// 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 +229,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 = traceColors,
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, gd._context.traceColorDefaults);

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._context.traceColorDefaults);
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._context.traceColorDefaults);

// add the traces to gd.data (no redrawing yet!)
for(i = 0; i < traces.length; i++) {
Expand Down
7 changes: 6 additions & 1 deletion src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* so we get the right experience without any config argument.
*/

var Color = require('../components/color');

module.exports = {

// no interactivity, for export or image generation
Expand Down Expand Up @@ -126,5 +128,8 @@ module.exports = {

// Set global transform to be applied to all traces with no
// specification needed
globalTransforms: []
globalTransforms: [],

// Set the default trace color scheme
traceColorDefaults: Color.defaults
};
5 changes: 4 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ plots.supplyDefaults = function(gd) {

// then do the data
newFullLayout._globalTransforms = (gd._context || {}).globalTransforms;
// pass along default trace colors
newFullLayout._traceColors = (gd._context || {}).traceColorDefaults;
plots.supplyDataDefaults(newData, newFullData, newLayout, newFullLayout);

// attach helper method to check whether a plot type is present on graph
Expand Down Expand Up @@ -950,8 +952,9 @@ plots.supplyFrameDefaults = function(frameIn) {
};

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

function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, plots.attributes, attr, dflt);
Expand Down
9 changes: 5 additions & 4 deletions src/traces/pie/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = function calc(gd, trace) {
var allThisTraceLabels = {};
var vTotal = 0;
var hiddenLabels = fullLayout.hiddenlabels || [];
var traceColors = gd._context.traceColorDefaults;

var i, v, label, hidden, pt;

Expand Down Expand Up @@ -107,7 +108,7 @@ 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, traceColors);
fullLayout._piedefaultcolorcount++;
}
}
Expand Down Expand Up @@ -148,10 +149,10 @@ module.exports = function calc(gd, trace) {
*/
var pieDefaultColors;

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

var i;
Expand All @@ -160,7 +161,7 @@ function nextDefaultColor(index) {
pieDefaultColors.push(tinycolor(mainDefaults[i]).lighten(20).toHexString());
}

for(i = 0; i < Color.defaults.length; i++) {
for(i = 0; i < traceColors.length; i++) {
pieDefaultColors.push(tinycolor(mainDefaults[i]).darken(20).toHexString());
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/image/mocks/trace-color-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"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]}
],
"layout": {
"title": "Trace Color Defaults"
},
"config": {
"traceColorDefaults": [
"#DE5845",
"#E83898",
"#A83DD1",
"#5A38E8",
"#3C71DE"
]
}
}
28 changes: 28 additions & 0 deletions test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ describe('Test Plots', function() {

testSanitizeMarginsHasBeenCalledOnlyOnce(gd);
});

it('should include the trace color override', function() {
var gd = {
_context: {
traceColorDefaults: ['red', 'yellow', 'blue']
}
};

Plots.supplyDefaults(gd);
expect(gd._fullLayout._traceColors).toBe(gd._context.traceColorDefaults);
});
});

describe('Plots.supplyLayoutGlobalDefaults should', function() {
Expand Down Expand Up @@ -244,6 +255,23 @@ describe('Test Plots', function() {
expect(traceOut.hoverinfo).toEqual('name');
});
});

it('should pass defined trace colors to supplyDefaults', function() {
var originalGetModule = Plots.getModule;
var supplyDefaultsSpy;

spyOn(Plots, 'getModule').and.callFake(function(traceOut) {
var mod = originalGetModule(traceOut);
spyOn(mod, 'supplyDefaults').and.callThrough();
supplyDefaultsSpy = mod.supplyDefaults;
return mod;
});

layout._traceColors = ['red', 'yellow', 'blue'];
traceIn = {};
traceOut = supplyTraceDefaults(traceIn, 0, layout);
expect(supplyDefaultsSpy).toHaveBeenCalledWith(traceIn, traceOut, 'red', layout);
});
});

describe('Plots.supplyTransformDefaults', function() {
Expand Down