Skip to content

Commit 5767a71

Browse files
authored
Merge pull request #6024 from plotly/h-colorbar
Introduce horizontal colorbars
2 parents 1206902 + a0901bd commit 5767a71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3103
-565
lines changed

draftlogs/6024_add.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Introduce horizontal colorbars [[#6024](https://github.com/plotly/plotly.js/pull/6024)]

src/components/colorbar/attributes.js

+29-20
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ var overrideAll = require('../../plot_api/edit_types').overrideAll;
77

88

99
module.exports = overrideAll({
10-
// TODO: only right is supported currently
11-
// orient: {
12-
// valType: 'enumerated',
13-
// values: ['left', 'right', 'top', 'bottom'],
14-
// dflt: 'right',
15-
// description: [
16-
// 'Determines which side are the labels on',
17-
// '(so left and right make vertical bars, etc.)'
18-
// ].join(' ')
19-
// },
10+
orientation: {
11+
valType: 'enumerated',
12+
values: ['h', 'v'],
13+
dflt: 'v',
14+
description: 'Sets the orientation of the colorbar.'
15+
},
2016
thicknessmode: {
2117
valType: 'enumerated',
2218
values: ['fraction', 'pixels'],
@@ -61,21 +57,23 @@ module.exports = overrideAll({
6157
},
6258
x: {
6359
valType: 'number',
64-
dflt: 1.02,
6560
min: -2,
6661
max: 3,
6762
description: [
68-
'Sets the x position of the color bar (in plot fraction).'
63+
'Sets the x position of the color bar (in plot fraction).',
64+
'Defaults to 1.02 when `orientation` is *v* and',
65+
'0.5 when `orientation` is *h*.'
6966
].join(' ')
7067
},
7168
xanchor: {
7269
valType: 'enumerated',
7370
values: ['left', 'center', 'right'],
74-
dflt: 'left',
7571
description: [
7672
'Sets this color bar\'s horizontal position anchor.',
7773
'This anchor binds the `x` position to the *left*, *center*',
78-
'or *right* of the color bar.'
74+
'or *right* of the color bar.',
75+
'Defaults to *left* when `orientation` is *v* and',
76+
'*center* when `orientation` is *h*.'
7977
].join(' ')
8078
},
8179
xpad: {
@@ -86,21 +84,23 @@ module.exports = overrideAll({
8684
},
8785
y: {
8886
valType: 'number',
89-
dflt: 0.5,
9087
min: -2,
9188
max: 3,
9289
description: [
93-
'Sets the y position of the color bar (in plot fraction).'
90+
'Sets the y position of the color bar (in plot fraction).',
91+
'Defaults to 0.5 when `orientation` is *v* and',
92+
'1.02 when `orientation` is *h*.'
9493
].join(' ')
9594
},
9695
yanchor: {
9796
valType: 'enumerated',
9897
values: ['top', 'middle', 'bottom'],
99-
dflt: 'middle',
10098
description: [
10199
'Sets this color bar\'s vertical position anchor',
102100
'This anchor binds the `y` position to the *top*, *middle*',
103-
'or *bottom* of the color bar.'
101+
'or *bottom* of the color bar.',
102+
'Defaults to *middle* when `orientation` is *v* and',
103+
'*bottom* when `orientation` is *h*.'
104104
].join(' ')
105105
},
106106
ypad: {
@@ -143,18 +143,26 @@ module.exports = overrideAll({
143143
'In other cases the default is *hide past div*.'
144144
].join(' ')
145145
}),
146+
147+
// ticklabelposition: not used directly, as values depend on orientation
148+
// left/right options are for x axes, and top/bottom options are for y axes
146149
ticklabelposition: {
147150
valType: 'enumerated',
148151
values: [
149152
'outside', 'inside',
150153
'outside top', 'inside top',
154+
'outside left', 'inside left',
155+
'outside right', 'inside right',
151156
'outside bottom', 'inside bottom'
152157
],
153158
dflt: 'outside',
154159
description: [
155-
'Determines where tick labels are drawn.'
160+
'Determines where tick labels are drawn relative to the ticks.',
161+
'Left and right options are used when `orientation` is *h*,',
162+
'top and bottom when `orientation` is *v*.'
156163
].join(' ')
157164
},
165+
158166
ticklen: axesAttrs.ticklen,
159167
tickwidth: axesAttrs.tickwidth,
160168
tickcolor: axesAttrs.tickcolor,
@@ -193,10 +201,11 @@ module.exports = overrideAll({
193201
side: {
194202
valType: 'enumerated',
195203
values: ['right', 'top', 'bottom'],
196-
dflt: 'top',
197204
description: [
198205
'Determines the location of color bar\'s title',
199206
'with respect to the color bar.',
207+
'Defaults to *top* when `orientation` if *v* and ',
208+
'defaults to *right* when `orientation` if *h*.',
200209
'Note that the title\'s location used to be set',
201210
'by the now deprecated `titleside` attribute.'
202211
].join(' ')

src/components/colorbar/defaults.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,30 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
1818
return Lib.coerce(colorbarIn, colorbarOut, attributes, attr, dflt);
1919
}
2020

21+
var margin = layout.margin || {t: 0, b: 0, l: 0, r: 0};
22+
var w = layout.width - margin.l - margin.r;
23+
var h = layout.height - margin.t - margin.b;
24+
25+
var orientation = coerce('orientation');
26+
var isVertical = orientation === 'v';
27+
2128
var thicknessmode = coerce('thicknessmode');
2229
coerce('thickness', (thicknessmode === 'fraction') ?
23-
30 / (layout.width - layout.margin.l - layout.margin.r) :
30+
30 / (isVertical ? w : h) :
2431
30
2532
);
2633

2734
var lenmode = coerce('lenmode');
2835
coerce('len', (lenmode === 'fraction') ?
2936
1 :
30-
layout.height - layout.margin.t - layout.margin.b
37+
isVertical ? h : w
3138
);
3239

33-
coerce('x');
34-
coerce('xanchor');
40+
coerce('x', isVertical ? 1.02 : 0.5);
41+
coerce('xanchor', isVertical ? 'left' : 'center');
3542
coerce('xpad');
36-
coerce('y');
37-
coerce('yanchor');
43+
coerce('y', isVertical ? 0.5 : 1.02);
44+
coerce('yanchor', isVertical ? 'middle' : 'bottom');
3845
coerce('ypad');
3946
Lib.noneOrAll(colorbarIn, colorbarOut, ['x', 'y']);
4047

@@ -44,7 +51,22 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
4451
coerce('borderwidth');
4552
coerce('bgcolor');
4653

47-
var ticklabelposition = coerce('ticklabelposition');
54+
var ticklabelposition = Lib.coerce(colorbarIn, colorbarOut, {
55+
ticklabelposition: {
56+
valType: 'enumerated',
57+
dflt: 'outside',
58+
values: isVertical ? [
59+
'outside', 'inside',
60+
'outside top', 'inside top',
61+
'outside bottom', 'inside bottom'
62+
] : [
63+
'outside', 'inside',
64+
'outside left', 'inside left',
65+
'outside right', 'inside right'
66+
]
67+
}
68+
}, 'ticklabelposition');
69+
4870
coerce('ticklabeloverflow', ticklabelposition.indexOf('inside') !== -1 ? 'hide past domain' : 'hide past div');
4971

5072
handleTickValueDefaults(colorbarIn, colorbarOut, coerce, 'linear');
@@ -66,5 +88,5 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
6688
size: Lib.bigFont(tickFont.size)
6789
});
6890
Lib.coerceFont(coerce, 'title.font', dfltTitleFont);
69-
coerce('title.side');
91+
coerce('title.side', isVertical ? 'top' : 'right');
7092
};

0 commit comments

Comments
 (0)