Skip to content
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

initial implementation of layout.colorscale #3274

Merged
merged 15 commits into from
Nov 23, 2018
Merged
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
13 changes: 8 additions & 5 deletions src/components/colorscale/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

var Lib = require('../../lib');

var scales = require('./scales');
var flipScale = require('./flip_scale');


module.exports = function calc(trace, vals, containerStr, cLetter) {
module.exports = function calc(gd, trace, opts) {
var fullLayout = gd._fullLayout;
var vals = opts.vals;
var containerStr = opts.containerStr;
var cLetter = opts.cLetter;
var container = trace;
var inputContainer = trace._input;
var fullInputContainer = trace._fullInput;
Expand Down Expand Up @@ -84,9 +87,9 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
doUpdate(autoAttr, (auto !== false || (min === undefined && max === undefined)));

if(container.autocolorscale) {
if(min * max < 0) scl = scales.RdBu;
else if(min >= 0) scl = scales.Reds;
else scl = scales.Blues;
if(min * max < 0) scl = fullLayout.colorscale.diverging;
else if(min >= 0) scl = fullLayout.colorscale.sequential;
else scl = gd._fullLayout.colorscale.sequentialminus;

// reversescale is handled at the containerOut level
doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl);
Expand Down
8 changes: 8 additions & 0 deletions src/components/colorscale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@

'use strict';

exports.moduleType = 'component';

exports.name = 'colorscale';

exports.scales = require('./scales');

exports.defaultScale = require('./default_scale');

exports.attributes = require('./attributes');

exports.layoutAttributes = require('./layout_attributes');

exports.supplyLayoutDefaults = require('./layout_defaults');

exports.handleDefaults = require('./defaults');

exports.calc = require('./calc');
Expand Down
46 changes: 46 additions & 0 deletions src/components/colorscale/layout_attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var scales = require('./scales');

var msg = 'Note that `autocolorscale` must be true for this attribute to work.';
module.exports = {
editType: 'calc',
sequential: {
valType: 'colorscale',
dflt: scales.Reds,
role: 'style',
editType: 'calc',
description: [
'Sets the default sequential colorscale for positive values.',
msg
].join(' ')
},
sequentialminus: {
valType: 'colorscale',
dflt: scales.Blues,
role: 'style',
editType: 'calc',
description: [
'Sets the default sequential colorscale for negative values.',
msg
].join(' ')
},
diverging: {
valType: 'colorscale',
dflt: scales.RdBu,
role: 'style',
editType: 'calc',
description: [
'Sets the default diverging colorscale.',
msg
].join(' ')
}
};
26 changes: 26 additions & 0 deletions src/components/colorscale/layout_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');
var colorscaleAttrs = require('./layout_attributes');
var Template = require('../../plot_api/plot_template');

module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
var colorscaleIn = layoutIn.colorscale;
var colorscaleOut = Template.newContainer(layoutOut, 'colorscale');
function coerce(attr, dflt) {
return Lib.coerce(colorscaleIn, colorscaleOut, colorscaleAttrs, attr, dflt);
}

coerce('sequential');
coerce('sequentialminus');
coerce('diverging');
};
3 changes: 2 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ register([
require('./components/rangeslider'),
require('./components/rangeselector'),
require('./components/grid'),
require('./components/errorbars')
require('./components/errorbars'),
require('./components/colorscale')
]);

// locales en and en-US are required for default behavior
Expand Down
2 changes: 2 additions & 0 deletions src/plots/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var fontAttrs = require('./font_attributes');
var colorAttrs = require('../components/color/attributes');
var colorscaleAttrs = require('../components/colorscale/layout_attributes');

var globalFont = fontAttrs({
editType: 'calc',
Expand Down Expand Up @@ -188,6 +189,7 @@ module.exports = {
editType: 'calc',
description: 'Sets the default trace colors.'
},
colorscale: colorscaleAttrs,
datarevision: {
valType: 'any',
role: 'info',
Expand Down
12 changes: 10 additions & 2 deletions src/traces/bar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ module.exports = function calc(gd, trace) {

// auto-z and autocolorscale if applicable
if(hasColorscale(trace, 'marker')) {
colorscaleCalc(trace, trace.marker.color, 'marker', 'c');
colorscaleCalc(gd, trace, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking nice 💎

vals: trace.marker.color,
containerStr: 'marker',
cLetter: 'c'
});
}
if(hasColorscale(trace, 'marker.line')) {
colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c');
colorscaleCalc(gd, trace, {
vals: trace.marker.line.color,
containerStr: 'marker.line',
cLetter: 'c'
});
}

arraysToCalcdata(cd, trace);
Expand Down
12 changes: 10 additions & 2 deletions src/traces/barpolar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ function calc(gd, trace) {
}

if(hasColorscale(trace, 'marker')) {
colorscaleCalc(trace, trace.marker.color, 'marker', 'c');
colorscaleCalc(gd, trace, {
vals: trace.marker.color,
containerStr: 'marker',
cLetter: 'c'
});
}
if(hasColorscale(trace, 'marker.line')) {
colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c');
colorscaleCalc(gd, trace, {
vals: trace.marker.line.color,
containerStr: 'marker.line',
cLetter: 'c'
});
}

arraysToCalcdata(cd, trace);
Expand Down
6 changes: 5 additions & 1 deletion src/traces/choropleth/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ module.exports = function calc(gd, trace) {
}

arraysToCalcdata(calcTrace, trace);
colorscaleCalc(trace, trace.z, '', 'z');
colorscaleCalc(gd, trace, {
vals: trace.z,
containerStr: '',
cLetter: 'z'
});
calcSelection(calcTrace, trace);

return calcTrace;
Expand Down
6 changes: 5 additions & 1 deletion src/traces/cone/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ module.exports = function calc(gd, trace) {
trace._len = len;
trace._normMax = normMax;

colorscaleCalc(trace, [normMin, normMax], '', 'c');
colorscaleCalc(gd, trace, {
vals: [normMin, normMax],
containerStr: '',
cLetter: 'c'
});
};
6 changes: 5 additions & 1 deletion src/traces/contourcarpet/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ function heatmappishCalc(gd, trace) {

if(trace.contours.type === 'levels' && trace.contours.coloring !== 'none') {
// auto-z and autocolorscale if applicable
colorscaleCalc(trace, z, '', 'z');
colorscaleCalc(gd, trace, {
vals: z,
containerStr: '',
cLetter: 'z'
});
}

return [cd0];
Expand Down
6 changes: 5 additions & 1 deletion src/traces/heatmap/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ module.exports = function calc(gd, trace) {

// auto-z and autocolorscale if applicable
if(!isContour || trace.contours.type !== 'constraint') {
colorscaleCalc(trace, z, '', 'z');
colorscaleCalc(gd, trace, {
vals: z,
containerStr: '',
cLetter: 'z'
});
}

if(isContour && trace.contours && trace.contours.coloring === 'heatmap') {
Expand Down
6 changes: 5 additions & 1 deletion src/traces/mesh3d/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var colorscaleCalc = require('../../components/colorscale/calc');

module.exports = function calc(gd, trace) {
if(trace.intensity) {
colorscaleCalc(trace, trace.intensity, '', 'c');
colorscaleCalc(gd, trace, {
vals: trace.intensity,
containerStr: '',
cLetter: 'c'
});
}
};
6 changes: 5 additions & 1 deletion src/traces/parcats/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ module.exports = function calc(gd, trace) {
// Process colorscale
if(line) {
if(hasColorscale(trace, 'line')) {
colorscaleCalc(trace, trace.line.color, 'line', 'c');
colorscaleCalc(gd, trace, {
vals: trace.line.color,
containerStr: 'line',
cLetter: 'c'
});
}
markerColorscale = Drawing.tryColorscale(line);
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/traces/parcoords/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ module.exports = function calc(gd, trace) {
var cscale = cs ? trace.line.colorscale : [[0, trace.line.color], [1, trace.line.color]];

if(hasColorscale(trace, 'line')) {
calcColorscale(trace, color, 'line', 'c');
calcColorscale(gd, trace, {
vals: color,
containerStr: 'line',
cLetter: 'c'
});
}

return wrap({
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatter/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function calc(gd, trace) {
}

arraysToCalcdata(cd, trace);
calcColorscale(trace);
calcColorscale(gd, trace);
calcSelection(cd, trace);

if(stackGroupOpts) {
Expand Down
20 changes: 16 additions & 4 deletions src/traces/scatter/colorscale_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@ var calcColorscale = require('../../components/colorscale/calc');
var subTypes = require('./subtypes');


module.exports = function calcMarkerColorscale(trace) {
module.exports = function calcMarkerColorscale(gd, trace) {
if(subTypes.hasLines(trace) && hasColorscale(trace, 'line')) {
calcColorscale(trace, trace.line.color, 'line', 'c');
calcColorscale(gd, trace, {
vals: trace.line.color,
containerStr: 'line',
cLetter: 'c'
});
}

if(subTypes.hasMarkers(trace)) {
if(hasColorscale(trace, 'marker')) {
calcColorscale(trace, trace.marker.color, 'marker', 'c');
calcColorscale(gd, trace, {
vals: trace.marker.color,
containerStr: 'marker',
cLetter: 'c'
});
}
if(hasColorscale(trace, 'marker.line')) {
calcColorscale(trace, trace.marker.line.color, 'marker.line', 'c');
calcColorscale(gd, trace, {
vals: trace.marker.line.color,
containerStr: 'marker.line',
cLetter: 'c'
});
}
}
};
2 changes: 1 addition & 1 deletion src/traces/scatter3d/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function calc(gd, trace) {
var cd = [{x: false, y: false, trace: trace, t: {}}];

arraysToCalcdata(cd, trace);
calcColorscales(trace);
calcColorscales(gd, trace);

return cd;
};
2 changes: 1 addition & 1 deletion src/traces/scattercarpet/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = function calc(gd, trace) {
cd[0].trace = trace;

calcMarkerSize(trace, serieslen);
calcColorscale(trace);
calcColorscale(gd, trace);
arraysToCalcdata(cd, trace);
calcSelection(cd, trace);

Expand Down
2 changes: 1 addition & 1 deletion src/traces/scattergeo/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function calc(gd, trace) {
}

arraysToCalcdata(calcTrace, trace);
calcMarkerColorscale(trace);
calcMarkerColorscale(gd, trace);
calcSelection(calcTrace, trace);

if(len) {
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scattergl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function calc(gd, trace) {
}

// create scene options and scene
calcColorscales(trace);
calcColorscales(gd, trace);
var opts = sceneOptions(gd, subplot, trace, positions, x, y);
var scene = sceneUpdate(gd, subplot);

Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatterpolar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function calc(gd, trace) {
var ppad = calcMarkerSize(trace, len);
trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {ppad: ppad});

calcColorscale(trace);
calcColorscale(gd, trace);
arraysToCalcdata(cd, trace);
calcSelection(cd, trace);

Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatterpolargl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function calc(gd, trace) {
stash.r = rArray;
stash.theta = thetaArray;

calcColorscales(trace);
calcColorscales(gd, trace);

// only compute 'style' options in calc, as position options
// depend on the radial range and must be set in plot
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatterternary/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = function calc(gd, trace) {
}

calcMarkerSize(trace, serieslen);
calcColorscale(trace);
calcColorscale(gd, trace);
arraysToCalcdata(cd, trace);
calcSelection(cd, trace);

Expand Down
Loading