Skip to content

Commit e398676

Browse files
authored
Merge pull request #3341 from plotly/colorscale-no-mutate
Don't mutate colorscale, cmin/cmax and zmin/zmax into user traces
2 parents d205d68 + 596bb47 commit e398676

Some content is hidden

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

57 files changed

+950
-559
lines changed

src/components/colorbar/connect.js

+6-2
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var palettes = require('./scales.js');
11+
var palettes = require('./scales.js').scales;
1212
var paletteStr = Object.keys(palettes);
1313

1414
function code(s) {
@@ -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

+7-58
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,19 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

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

14-
var flipScale = require('./flip_scale');
15-
16-
1713
module.exports = function calc(gd, trace, opts) {
1814
var fullLayout = gd._fullLayout;
1915
var vals = opts.vals;
2016
var containerStr = opts.containerStr;
2117
var cLetter = opts.cLetter;
22-
var container = trace;
23-
var inputContainer = trace._input;
24-
var fullInputContainer = trace._fullInput;
2518

26-
// set by traces with groupby transforms
27-
var updateStyle = trace.updateStyle;
28-
29-
function doUpdate(attr, inputVal, fullVal) {
30-
if(fullVal === undefined) fullVal = inputVal;
31-
32-
if(updateStyle) {
33-
updateStyle(trace._input, containerStr ? (containerStr + '.' + attr) : attr, inputVal);
34-
}
35-
else {
36-
inputContainer[attr] = inputVal;
37-
}
38-
39-
container[attr] = fullVal;
40-
if(fullInputContainer && (trace !== trace._fullInput)) {
41-
if(updateStyle) {
42-
updateStyle(trace._fullInput, containerStr ? (containerStr + '.' + attr) : attr, fullVal);
43-
}
44-
else {
45-
fullInputContainer[attr] = fullVal;
46-
}
47-
}
48-
}
49-
50-
if(containerStr) {
51-
container = Lib.nestedProperty(container, containerStr).get();
52-
inputContainer = Lib.nestedProperty(inputContainer, containerStr).get();
53-
fullInputContainer = Lib.nestedProperty(fullInputContainer, containerStr).get() || {};
54-
}
19+
var container = containerStr ?
20+
Lib.nestedProperty(trace, containerStr).get() :
21+
trace;
5522

5623
var autoAttr = cLetter + 'auto';
5724
var minAttr = cLetter + 'min';
@@ -74,32 +41,14 @@ module.exports = function calc(gd, trace, opts) {
7441
max += 0.5;
7542
}
7643

77-
doUpdate(minAttr, min);
78-
doUpdate(maxAttr, max);
79-
80-
/*
81-
* If auto was explicitly false but min or max was missing,
82-
* we filled in the missing piece here but later the trace does
83-
* not look auto.
84-
* Otherwise make sure the trace still looks auto as far as later
85-
* changes are concerned.
86-
*/
87-
doUpdate(autoAttr, (auto !== false || (min === undefined && max === undefined)));
44+
container['_' + minAttr] = container[minAttr] = min;
45+
container['_' + maxAttr] = container[maxAttr] = max;
8846

8947
if(container.autocolorscale) {
9048
if(min * max < 0) scl = fullLayout.colorscale.diverging;
9149
else if(min >= 0) scl = fullLayout.colorscale.sequential;
92-
else scl = gd._fullLayout.colorscale.sequentialminus;
93-
94-
// reversescale is handled at the containerOut level
95-
doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl);
50+
else scl = fullLayout.colorscale.sequentialminus;
9651

97-
// We pushed a colorscale back to input, which will change the default autocolorscale next time
98-
// to avoid spurious redraws from Plotly.react, update resulting autocolorscale now
99-
// This is a conscious decision so that changing the data later does not unexpectedly
100-
// give you a new colorscale
101-
if(!inputContainer.autocolorscale) {
102-
doUpdate('autocolorscale', false);
103-
}
52+
container._colorscale = container.colorscale = scl;
10453
}
10554
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 Lib = require('../../lib');
12+
var hasColorscale = require('./helpers').hasColorscale;
13+
14+
module.exports = function crossTraceDefaults(fullData) {
15+
function replace(cont, k) {
16+
var val = cont['_' + k];
17+
if(val !== undefined) {
18+
cont[k] = val;
19+
}
20+
}
21+
22+
function relinkColorAtts(trace, cAttrs) {
23+
var cont = cAttrs.container ?
24+
Lib.nestedProperty(trace, cAttrs.container).get() :
25+
trace;
26+
27+
if(cont) {
28+
var isAuto = cont.zauto || cont.cauto;
29+
var minAttr = cAttrs.min;
30+
var maxAttr = cAttrs.max;
31+
32+
if(isAuto || cont[minAttr] === undefined) {
33+
replace(cont, minAttr);
34+
}
35+
if(isAuto || cont[maxAttr] === undefined) {
36+
replace(cont, maxAttr);
37+
}
38+
if(cont.autocolorscale) {
39+
replace(cont, 'colorscale');
40+
}
41+
}
42+
}
43+
44+
for(var i = 0; i < fullData.length; i++) {
45+
var trace = fullData[i];
46+
var _module = trace._module;
47+
48+
if(_module.colorbar) {
49+
relinkColorAtts(trace, _module.colorbar);
50+
}
51+
52+
// TODO could generalize _module.colorscale and use it here?
53+
54+
if(hasColorscale(trace, 'marker.line')) {
55+
relinkColorAtts(trace, {
56+
container: 'marker.line',
57+
min: 'cmin',
58+
max: 'cmax'
59+
});
60+
}
61+
62+
if(hasColorscale(trace, 'line')) {
63+
relinkColorAtts(trace, {
64+
container: 'line',
65+
min: 'cmin',
66+
max: 'cmax'
67+
});
68+
}
69+
}
70+
};

src/components/colorscale/default_scale.js

-14
This file was deleted.

src/components/colorscale/defaults.js

+22-25
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,50 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var isNumeric = require('fast-isnumeric');
1312

1413
var Lib = require('../../lib');
15-
1614
var hasColorbar = require('../colorbar/has_colorbar');
1715
var colorbarDefaults = require('../colorbar/defaults');
18-
var isValidScale = require('./is_valid_scale');
19-
var flipScale = require('./flip_scale');
2016

17+
var isValidScale = require('./scales').isValid;
2118

22-
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
23-
var prefix = opts.prefix,
24-
cLetter = opts.cLetter,
25-
containerStr = prefix.slice(0, prefix.length - 1),
26-
containerIn = prefix ?
27-
Lib.nestedProperty(traceIn, containerStr).get() || {} :
28-
traceIn,
29-
containerOut = prefix ?
30-
Lib.nestedProperty(traceOut, containerStr).get() || {} :
31-
traceOut,
32-
minIn = containerIn[cLetter + 'min'],
33-
maxIn = containerIn[cLetter + 'max'],
34-
sclIn = containerIn.colorscale;
19+
function npMaybe(cont, prefix) {
20+
var containerStr = prefix.slice(0, prefix.length - 1);
21+
return prefix ?
22+
Lib.nestedProperty(cont, containerStr).get() || {} :
23+
cont;
24+
}
3525

26+
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
27+
var prefix = opts.prefix;
28+
var cLetter = opts.cLetter;
29+
var containerIn = npMaybe(traceIn, prefix);
30+
var containerOut = npMaybe(traceOut, prefix);
31+
var template = npMaybe(traceOut._template || {}, prefix) || {};
32+
33+
var minIn = containerIn[cLetter + 'min'];
34+
var maxIn = containerIn[cLetter + 'max'];
3635
var validMinMax = isNumeric(minIn) && isNumeric(maxIn) && (minIn < maxIn);
3736
coerce(prefix + cLetter + 'auto', !validMinMax);
3837
coerce(prefix + cLetter + 'min');
3938
coerce(prefix + cLetter + 'max');
4039

4140
// handles both the trace case (autocolorscale is false by default) and
4241
// the marker and marker.line case (autocolorscale is true by default)
42+
var sclIn = containerIn.colorscale;
43+
var sclTemplate = template.colorscale;
4344
var autoColorscaleDflt;
4445
if(sclIn !== undefined) autoColorscaleDflt = !isValidScale(sclIn);
46+
if(sclTemplate !== undefined) autoColorscaleDflt = !isValidScale(sclTemplate);
4547
coerce(prefix + 'autocolorscale', autoColorscaleDflt);
46-
var sclOut = coerce(prefix + 'colorscale');
47-
48-
// reversescale is handled at the containerOut level
49-
var reverseScale = coerce(prefix + 'reversescale');
50-
if(reverseScale) containerOut.colorscale = flipScale(sclOut);
5148

52-
// ... until Scatter.colorbar can handle marker line colorbars
53-
if(prefix === 'marker.line.') return;
49+
coerce(prefix + 'colorscale');
50+
coerce(prefix + 'reversescale');
5451

55-
if(!opts.noScale) {
52+
if(!opts.noScale && prefix !== 'marker.line.') {
5653
// handles both the trace case where the dflt is listed in attributes and
5754
// the marker case where the dflt is determined by hasColorbar
5855
var showScaleDflt;

src/components/colorscale/extract_scale.js

-35
This file was deleted.

src/components/colorscale/flip_scale.js

-23
This file was deleted.

src/components/colorscale/get_scale.js

-38
This file was deleted.

0 commit comments

Comments
 (0)