Skip to content

Commit 0a686dd

Browse files
committed
flip scale when reversescale:true just before making scaleFn
- that we don't mutate fullData[i].colorscale in defaults and or calc.
1 parent 8046bb6 commit 0a686dd

File tree

13 files changed

+80
-62
lines changed

13 files changed

+80
-62
lines changed

src/components/colorbar/connect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var drawColorbar = require('./draw');
12+
var flipScale = require('../colorscale/helpers').flipScale;
1313

1414
/**
1515
* connectColorbar: create a colorbar from a trace, using its module to
@@ -49,7 +49,11 @@ module.exports = function connectColorbar(gd, cd, moduleOpts) {
4949

5050
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
5151

52-
cb.fillgradient(container.colorscale)
52+
var scl = container.reversescale ?
53+
flipScale(container.colorscale) :
54+
container.colorscale;
55+
56+
cb.fillgradient(scl)
5357
.zrange([container[moduleOpts.min], container[moduleOpts.max]])
5458
.options(container.colorbar)();
5559
};

src/components/colorscale/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ module.exports = function colorScaleAttrs(context, opts) {
197197
valType: 'boolean',
198198
role: 'style',
199199
dflt: false,
200-
editType: 'calc',
200+
editType: 'plot',
201201
description: [
202202
'Reverses the color mapping if true.',
203203
effectDesc,

src/components/colorscale/calc.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
'use strict';
1010

1111
var Lib = require('../../lib');
12-
var flipScale = require('./helpers').flipScale;
1312

1413
module.exports = function calc(gd, trace, opts) {
1514
var fullLayout = gd._fullLayout;
@@ -89,7 +88,7 @@ module.exports = function calc(gd, trace, opts) {
8988
else scl = gd._fullLayout.colorscale.sequentialminus;
9089

9190
// reversescale is handled at the containerOut level
92-
doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl);
91+
doUpdate('colorscale', scl);
9392

9493
// We pushed a colorscale back to input, which will change the default autocolorscale next time
9594
// to avoid spurious redraws from Plotly.react, update resulting autocolorscale now

src/components/colorscale/defaults.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce,
4040
var autoColorscaleDflt;
4141
if(sclIn !== undefined) autoColorscaleDflt = !isValidScale(sclIn);
4242
coerce(prefix + 'autocolorscale', autoColorscaleDflt);
43-
var sclOut = coerce(prefix + 'colorscale');
4443

45-
// reversescale is handled at the containerOut level
46-
var reverseScale = coerce(prefix + 'reversescale');
47-
if(reverseScale) containerOut.colorscale = helpers.flipScale(sclOut);
44+
coerce(prefix + 'colorscale');
45+
coerce(prefix + 'reversescale');
4846

4947
if(!opts.noScale && prefix !== 'marker.line.') {
5048
// handles both the trace case where the dflt is listed in attributes and

src/components/colorscale/helpers.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,36 @@ function hasColorscale(trace, containerStr) {
4747
/**
4848
* Extract colorscale into numeric domain and color range.
4949
*
50-
* @param {array} scl colorscale array of arrays
51-
* @param {number} cmin minimum color value (used to clamp scale)
52-
* @param {number} cmax maximum color value (used to clamp scale)
50+
* @param {object} cont colorscale container (e.g. trace, marker)
51+
* - colorscale {array of arrays}
52+
* - cmin/zmin {number}
53+
* - cmax/zmax {number}
54+
* - reversescale {boolean}
55+
* @param {object} opts
56+
* - cLetter {string} 'c' (for cmin/cmax) or 'z' (for zmin/zmax)
57+
*
58+
* @return {object}
59+
* - domain {array}
60+
* - range {array}
5361
*/
54-
function extractScale(scl, cmin, cmax) {
62+
function extractScale(cont, opts) {
63+
var cLetter = opts.cLetter;
64+
65+
var scl = cont.reversescale ?
66+
flipScale(cont.colorscale) :
67+
cont.colorscale;
68+
69+
// minimum color value (used to clamp scale)
70+
var cmin = cont[cLetter + 'min'];
71+
// maximum color value (used to clamp scale)
72+
var cmax = cont[cLetter + 'max'];
73+
5574
var N = scl.length;
5675
var domain = new Array(N);
5776
var range = new Array(N);
5877

5978
for(var i = 0; i < N; i++) {
6079
var si = scl[i];
61-
6280
domain[i] = cmin + si[0] * (cmax - cmin);
6381
range[i] = si[1];
6482
}
@@ -72,13 +90,11 @@ function extractScale(scl, cmin, cmax) {
7290
function flipScale(scl) {
7391
var N = scl.length;
7492
var sclNew = new Array(N);
75-
var si;
7693

7794
for(var i = N - 1, j = 0; i >= 0; i--, j++) {
78-
si = scl[i];
95+
var si = scl[i];
7996
sclNew[j] = [1 - si[0], si[1]];
8097
}
81-
8298
return sclNew;
8399
}
84100

src/components/drawing/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ drawing.tryColorscale = function(marker, prefix) {
643643

644644
if(scl && Lib.isArrayOrTypedArray(colorArray)) {
645645
return Colorscale.makeColorScaleFunc(
646-
Colorscale.extractScale(scl, cont.cmin, cont.cmax)
646+
Colorscale.extractScale(cont, {cLetter: 'c'})
647647
);
648648
}
649649
}

src/lib/gl_format_color.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ function formatColor(containerIn, opacityIn, len) {
4848

4949
if(containerIn.colorscale !== undefined) {
5050
sclFunc = Colorscale.makeColorScaleFunc(
51-
Colorscale.extractScale(
52-
containerIn.colorscale,
53-
containerIn.cmin,
54-
containerIn.cmax
55-
)
51+
Colorscale.extractScale(containerIn, {cLetter: 'c'})
5652
);
5753
}
5854
else {

src/traces/choropleth/style.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ function styleTrace(gd, calcTrace) {
2525
var markerLine = marker.line || {};
2626

2727
var sclFunc = Colorscale.makeColorScaleFunc(
28-
Colorscale.extractScale(
29-
trace.colorscale,
30-
trace.zmin,
31-
trace.zmax
32-
)
28+
Colorscale.extractScale(trace, {cLetter: 'z'})
3329
);
3430

3531
locs.each(function(d) {

src/traces/contour/make_color_map.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ module.exports = function makeColorMap(trace) {
2626
nc = 1;
2727
}
2828

29-
var scl = trace.colorscale,
30-
len = scl.length;
29+
var scl = trace.reversescale ?
30+
Colorscale.flipScale(trace.colorscale) :
31+
trace.colorscale;
3132

32-
var domain = new Array(len),
33-
range = new Array(len);
33+
var len = scl.length;
34+
var domain = new Array(len);
35+
var range = new Array(len);
3436

3537
var si, i;
3638

src/traces/heatmap/plot.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ module.exports = function(gd, plotinfo, cdheatmaps, heatmapLayer) {
141141
var context = canvas.getContext('2d');
142142

143143
var sclFunc = Colorscale.makeColorScaleFunc(
144-
Colorscale.extractScale(
145-
trace.colorscale,
146-
trace.zmin,
147-
trace.zmax
148-
),
144+
Colorscale.extractScale(trace, {cLetter: 'z'}),
149145
{ noNumericCheck: true, returnArray: true }
150146
);
151147

0 commit comments

Comments
 (0)