Skip to content

Commit 922bad6

Browse files
authored
Merge pull request #3274 from plotly/2925-default-colorbars
initial implementation of `layout.colorscale`
2 parents 2f6c6e7 + dca4350 commit 922bad6

File tree

31 files changed

+482
-42
lines changed

31 files changed

+482
-42
lines changed

src/components/colorscale/calc.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

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

14-
var scales = require('./scales');
1514
var flipScale = require('./flip_scale');
1615

1716

18-
module.exports = function calc(trace, vals, containerStr, cLetter) {
17+
module.exports = function calc(gd, trace, opts) {
18+
var fullLayout = gd._fullLayout;
19+
var vals = opts.vals;
20+
var containerStr = opts.containerStr;
21+
var cLetter = opts.cLetter;
1922
var container = trace;
2023
var inputContainer = trace._input;
2124
var fullInputContainer = trace._fullInput;
@@ -84,9 +87,9 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
8487
doUpdate(autoAttr, (auto !== false || (min === undefined && max === undefined)));
8588

8689
if(container.autocolorscale) {
87-
if(min * max < 0) scl = scales.RdBu;
88-
else if(min >= 0) scl = scales.Reds;
89-
else scl = scales.Blues;
90+
if(min * max < 0) scl = fullLayout.colorscale.diverging;
91+
else if(min >= 0) scl = fullLayout.colorscale.sequential;
92+
else scl = gd._fullLayout.colorscale.sequentialminus;
9093

9194
// reversescale is handled at the containerOut level
9295
doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl);

src/components/colorscale/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99

1010
'use strict';
1111

12+
exports.moduleType = 'component';
13+
14+
exports.name = 'colorscale';
15+
1216
exports.scales = require('./scales');
1317

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

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

22+
exports.layoutAttributes = require('./layout_attributes');
23+
24+
exports.supplyLayoutDefaults = require('./layout_defaults');
25+
1826
exports.handleDefaults = require('./defaults');
1927

2028
exports.calc = require('./calc');
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var scales = require('./scales');
12+
13+
var msg = 'Note that `autocolorscale` must be true for this attribute to work.';
14+
module.exports = {
15+
editType: 'calc',
16+
sequential: {
17+
valType: 'colorscale',
18+
dflt: scales.Reds,
19+
role: 'style',
20+
editType: 'calc',
21+
description: [
22+
'Sets the default sequential colorscale for positive values.',
23+
msg
24+
].join(' ')
25+
},
26+
sequentialminus: {
27+
valType: 'colorscale',
28+
dflt: scales.Blues,
29+
role: 'style',
30+
editType: 'calc',
31+
description: [
32+
'Sets the default sequential colorscale for negative values.',
33+
msg
34+
].join(' ')
35+
},
36+
diverging: {
37+
valType: 'colorscale',
38+
dflt: scales.RdBu,
39+
role: 'style',
40+
editType: 'calc',
41+
description: [
42+
'Sets the default diverging colorscale.',
43+
msg
44+
].join(' ')
45+
}
46+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('../../lib');
13+
var colorscaleAttrs = require('./layout_attributes');
14+
var Template = require('../../plot_api/plot_template');
15+
16+
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
17+
var colorscaleIn = layoutIn.colorscale;
18+
var colorscaleOut = Template.newContainer(layoutOut, 'colorscale');
19+
function coerce(attr, dflt) {
20+
return Lib.coerce(colorscaleIn, colorscaleOut, colorscaleAttrs, attr, dflt);
21+
}
22+
23+
coerce('sequential');
24+
coerce('sequentialminus');
25+
coerce('diverging');
26+
};

src/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ register([
5353
require('./components/rangeslider'),
5454
require('./components/rangeselector'),
5555
require('./components/grid'),
56-
require('./components/errorbars')
56+
require('./components/errorbars'),
57+
require('./components/colorscale')
5758
]);
5859

5960
// locales en and en-US are required for default behavior

src/plots/layout_attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var fontAttrs = require('./font_attributes');
1212
var colorAttrs = require('../components/color/attributes');
13+
var colorscaleAttrs = require('../components/colorscale/layout_attributes');
1314

1415
var globalFont = fontAttrs({
1516
editType: 'calc',
@@ -188,6 +189,7 @@ module.exports = {
188189
editType: 'calc',
189190
description: 'Sets the default trace colors.'
190191
},
192+
colorscale: colorscaleAttrs,
191193
datarevision: {
192194
valType: 'any',
193195
role: 'info',

src/traces/bar/calc.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ module.exports = function calc(gd, trace) {
4242

4343
// auto-z and autocolorscale if applicable
4444
if(hasColorscale(trace, 'marker')) {
45-
colorscaleCalc(trace, trace.marker.color, 'marker', 'c');
45+
colorscaleCalc(gd, trace, {
46+
vals: trace.marker.color,
47+
containerStr: 'marker',
48+
cLetter: 'c'
49+
});
4650
}
4751
if(hasColorscale(trace, 'marker.line')) {
48-
colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c');
52+
colorscaleCalc(gd, trace, {
53+
vals: trace.marker.line.color,
54+
containerStr: 'marker.line',
55+
cLetter: 'c'
56+
});
4957
}
5058

5159
arraysToCalcdata(cd, trace);

src/traces/barpolar/calc.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ function calc(gd, trace) {
5353
}
5454

5555
if(hasColorscale(trace, 'marker')) {
56-
colorscaleCalc(trace, trace.marker.color, 'marker', 'c');
56+
colorscaleCalc(gd, trace, {
57+
vals: trace.marker.color,
58+
containerStr: 'marker',
59+
cLetter: 'c'
60+
});
5761
}
5862
if(hasColorscale(trace, 'marker.line')) {
59-
colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c');
63+
colorscaleCalc(gd, trace, {
64+
vals: trace.marker.line.color,
65+
containerStr: 'marker.line',
66+
cLetter: 'c'
67+
});
6068
}
6169

6270
arraysToCalcdata(cd, trace);

src/traces/choropleth/calc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ module.exports = function calc(gd, trace) {
3030
}
3131

3232
arraysToCalcdata(calcTrace, trace);
33-
colorscaleCalc(trace, trace.z, '', 'z');
33+
colorscaleCalc(gd, trace, {
34+
vals: trace.z,
35+
containerStr: '',
36+
cLetter: 'z'
37+
});
3438
calcSelection(calcTrace, trace);
3539

3640
return calcTrace;

src/traces/cone/calc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ module.exports = function calc(gd, trace) {
3434
trace._len = len;
3535
trace._normMax = normMax;
3636

37-
colorscaleCalc(trace, [normMin, normMax], '', 'c');
37+
colorscaleCalc(gd, trace, {
38+
vals: [normMin, normMax],
39+
containerStr: '',
40+
cLetter: 'c'
41+
});
3842
};

0 commit comments

Comments
 (0)