Skip to content

Commit d69f27c

Browse files
Add midpoint attr to color scales
1 parent 5925c10 commit d69f27c

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/components/colorscale/attributes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ module.exports = function colorScaleAttrs(context, opts) {
8787
var auto = cLetter + 'auto';
8888
var min = cLetter + 'min';
8989
var max = cLetter + 'max';
90+
var mid = cLetter + 'mid';
91+
var autoFull = code(contextHead + auto);
9092
var minFull = code(contextHead + min);
9193
var maxFull = code(contextHead + max);
9294
var minmaxFull = minFull + ' and ' + maxFull;
@@ -160,6 +162,21 @@ module.exports = function colorScaleAttrs(context, opts) {
160162
].join('')
161163
};
162164

165+
attrs[mid] = {
166+
valType: 'number',
167+
role: 'info',
168+
dflt: null,
169+
editType: editTypeOverride || 'plot',
170+
impliedEdits: minmaxImpliedEdits,
171+
description: [
172+
'Sets the mid-point of the color domain by scaling ', minFull,
173+
' and/or ', maxFull, ' to be equidistant to this point.',
174+
effectDesc,
175+
' Value should have the same units as ', colorAttrFull, '. ',
176+
'Has no effect when ', autoFull, ' is `false`.'
177+
].join('')
178+
};
179+
163180
attrs.colorscale = {
164181
valType: 'colorscale',
165182
role: 'style',

src/components/colorscale/calc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ module.exports = function calc(gd, trace, opts) {
2323
var autoAttr = cLetter + 'auto';
2424
var minAttr = cLetter + 'min';
2525
var maxAttr = cLetter + 'max';
26+
var midAttr = cLetter + 'mid';
2627
var auto = container[autoAttr];
2728
var min = container[minAttr];
2829
var max = container[maxAttr];
30+
var mid = container[midAttr];
2931
var scl = container.colorscale;
3032

3133
if(auto !== false || min === undefined) {
@@ -36,6 +38,15 @@ module.exports = function calc(gd, trace, opts) {
3638
max = Lib.aggNums(Math.max, null, vals);
3739
}
3840

41+
if(auto !== false && mid !== undefined) {
42+
if(max - mid > mid - min) {
43+
min = mid - (max - mid);
44+
}
45+
else if(max - mid < mid - min) {
46+
max = mid + (mid - min);
47+
}
48+
}
49+
3950
if(min === max) {
4051
min -= 0.5;
4152
max += 0.5;

src/components/colorscale/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce,
3636
coerce(prefix + cLetter + 'auto', !validMinMax);
3737
coerce(prefix + cLetter + 'min');
3838
coerce(prefix + cLetter + 'max');
39+
coerce(prefix + cLetter + 'mid');
3940

4041
// handles both the trace case (autocolorscale is false by default) and
4142
// the marker and marker.line case (autocolorscale is true by default)

test/image/mocks/cmid.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": [
3+
{
4+
"mode": "markers",
5+
"x": [1,2,3],
6+
"y": [1,2,3],
7+
"marker": {
8+
"color": [-1,0,10],
9+
"showscale": true,
10+
"cmid": 0
11+
}
12+
}
13+
],
14+
"layout": {
15+
}
16+
}

0 commit comments

Comments
 (0)