Skip to content

Commit 6a47d66

Browse files
committed
fixes #3100 - make colorscales work from templates
... trace that have `autocolorscale:true` by default
1 parent a6517a8 commit 6a47d66

File tree

4 files changed

+90
-13
lines changed

4 files changed

+90
-13
lines changed

src/components/colorscale/defaults.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@ var colorbarDefaults = require('../colorbar/defaults');
1616

1717
var isValidScale = require('./scales').isValid;
1818

19-
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
20-
var prefix = opts.prefix,
21-
cLetter = opts.cLetter,
22-
containerStr = prefix.slice(0, prefix.length - 1),
23-
containerIn = prefix ?
24-
Lib.nestedProperty(traceIn, containerStr).get() || {} :
25-
traceIn,
26-
containerOut = prefix ?
27-
Lib.nestedProperty(traceOut, containerStr).get() || {} :
28-
traceOut,
29-
minIn = containerIn[cLetter + 'min'],
30-
maxIn = containerIn[cLetter + 'max'],
31-
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+
}
3225

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'];
3335
var validMinMax = isNumeric(minIn) && isNumeric(maxIn) && (minIn < maxIn);
3436
coerce(prefix + cLetter + 'auto', !validMinMax);
3537
coerce(prefix + cLetter + 'min');
3638
coerce(prefix + cLetter + 'max');
3739

3840
// handles both the trace case (autocolorscale is false by default) and
3941
// the marker and marker.line case (autocolorscale is true by default)
42+
var sclIn = containerIn.colorscale;
43+
var sclTemplate = template.colorscale;
4044
var autoColorscaleDflt;
4145
if(sclIn !== undefined) autoColorscaleDflt = !isValidScale(sclIn);
46+
if(sclTemplate !== undefined) autoColorscaleDflt = !isValidScale(sclTemplate);
4247
coerce(prefix + 'autocolorscale', autoColorscaleDflt);
4348

4449
coerce(prefix + 'colorscale');
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"data": [
3+
{
4+
"type": "scatter",
5+
"y": [1, 2, 3],
6+
"marker": {
7+
"size": 20,
8+
"color": [1, 2, 3],
9+
"showscale": true
10+
},
11+
"mode": "markers"
12+
}
13+
],
14+
"layout": {
15+
"template": {
16+
"data": {
17+
"scatter": [
18+
{
19+
"marker": {
20+
"symbol": "square",
21+
"colorscale": "Viridis",
22+
"colorbar": {
23+
"ticks": "inside",
24+
"ticklen": 10,
25+
"tickcolor": "white"
26+
}
27+
}
28+
}
29+
]
30+
}
31+
}
32+
}
33+
}

test/jasmine/tests/colorscale_test.js

+39
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,43 @@ describe('Test colorscale restyle calls:', function() {
863863
.catch(failTest)
864864
.then(done);
865865
});
866+
867+
it('should work with templates', function(done) {
868+
function _assert(msg, exp) {
869+
var mcc = [];
870+
d3.selectAll('path.point').each(function() {
871+
mcc.push(getFill(this));
872+
});
873+
874+
expect(mcc).toEqual(exp.mcc);
875+
}
876+
877+
var template = {
878+
data: {
879+
scatter: [{
880+
marker: {colorscale: 'Viridis'}
881+
}]
882+
}
883+
};
884+
885+
Plotly.plot(gd, [{
886+
y: [1, 2, 3],
887+
marker: {color: [1, 2, 3]}
888+
}])
889+
.then(function() {
890+
_assert('base - no templates', {
891+
mcc: ['rgb(220, 220, 220)', 'rgb(234, 135, 92)', 'rgb(178, 10, 28)']
892+
});
893+
})
894+
.then(function() {
895+
return Plotly.relayout(gd, 'template', template);
896+
})
897+
.then(function() {
898+
_assert('after relayouting in template', {
899+
mcc: ['rgb(68, 1, 84)', 'rgb(33, 145, 140)', 'rgb(253, 231, 37)']
900+
});
901+
})
902+
.catch(failTest)
903+
.then(done);
904+
});
866905
});

0 commit comments

Comments
 (0)