Skip to content

Commit bd81ac2

Browse files
committed
make contour zmin/zmax logic compatible with color axes
- remove zmin/zmax mutations happening after the calc stpe - make traceIs('contour') traces call Colorscale.calc with their own options.
1 parent eab822c commit bd81ac2

File tree

8 files changed

+71
-52
lines changed

8 files changed

+71
-52
lines changed

src/components/colorbar/draw.js

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ function makeColorBarData(gd) {
158158
opts._propPrefix = k + '.colorbar.';
159159

160160
cbOpt = {min: 'cmin', max: 'cmax'};
161+
if(colorAxOpts[0] !== 'heatmap') {
162+
trace = colorAxOpts[1];
163+
cbOpt.calc = trace._module.colorbar.calc;
164+
}
165+
161166
calcOpts();
162167
out.push(opts);
163168
}

src/traces/contour/calc.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,46 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

11+
var Colorscale = require('../../components/colorscale');
12+
1213
var heatmapCalc = require('../heatmap/calc');
1314
var setContours = require('./set_contours');
15+
var endPlus = require('./end_plus');
1416

1517
// most is the same as heatmap calc, then adjust it
1618
// though a few things inside heatmap calc still look for
1719
// contour maps, because the makeBoundArray calls are too entangled
1820
module.exports = function calc(gd, trace) {
1921
var cd = heatmapCalc(gd, trace);
20-
setContours(trace);
22+
23+
var zOut = cd[0].z;
24+
setContours(trace, zOut);
25+
26+
var contours = trace.contours;
27+
var cOpts = Colorscale.extractOpts(trace);
28+
var cVals;
29+
30+
if(contours.coloring === 'heatmap' && cOpts.auto && trace.autocontour === false) {
31+
var start = contours.start;
32+
var end = endPlus(contours);
33+
var cs = contours.size || 1;
34+
var nc = Math.floor((end - start) / cs) + 1;
35+
36+
if(!isFinite(cs)) {
37+
cs = 1;
38+
nc = 1;
39+
}
40+
41+
var min0 = start - cs / 2;
42+
var max0 = min0 + nc * cs;
43+
cVals = [min0, max0];
44+
} else {
45+
cVals = zOut;
46+
}
47+
48+
Colorscale.calc(gd, trace, {vals: cVals, cLetter: 'z'});
49+
2150
return cd;
2251
};

src/traces/contour/colorbar.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88

99
'use strict';
1010

11+
var extractOpts = require('../../components/colorscale').extractOpts;
1112
var makeColorMap = require('./make_color_map');
1213
var endPlus = require('./end_plus');
1314

14-
function calc(gd, cd, opts) {
15-
var trace = cd[0].trace;
15+
function calc(gd, trace, opts) {
1616
var contours = trace.contours;
1717
var line = trace.line;
1818
var cs = contours.size || 1;
1919
var coloring = contours.coloring;
2020
var colorMap = makeColorMap(trace, {isColorbar: true});
2121

2222
if(coloring === 'heatmap') {
23+
var cOpts = extractOpts(trace);
2324
opts._fillgradient = trace.colorscale;
24-
opts._zrange = [trace.zmin, trace.zmax];
25+
opts._zrange = [cOpts.min, cOpts.max];
2526
} else if(coloring === 'fill') {
2627
opts._fillcolor = colorMap;
2728
}

src/traces/contour/make_color_map.js

+19-24
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 d3 = require('d3');
12+
1313
var Colorscale = require('../../components/colorscale');
1414
var endPlus = require('./end_plus');
1515

@@ -20,15 +20,16 @@ module.exports = function makeColorMap(trace) {
2020
var cs = contours.size || 1;
2121
var nc = Math.floor((end - start) / cs) + 1;
2222
var extra = contours.coloring === 'lines' ? 0 : 1;
23+
var cOpts = Colorscale.extractOpts(trace);
2324

2425
if(!isFinite(cs)) {
2526
cs = 1;
2627
nc = 1;
2728
}
2829

29-
var scl = trace.reversescale ?
30-
Colorscale.flipScale(trace.colorscale) :
31-
trace.colorscale;
30+
var scl = cOpts.reversescale ?
31+
Colorscale.flipScale(cOpts.colorscale) :
32+
cOpts.colorscale;
3233

3334
var len = scl.length;
3435
var domain = new Array(len);
@@ -37,51 +38,45 @@ module.exports = function makeColorMap(trace) {
3738
var si, i;
3839

3940
if(contours.coloring === 'heatmap') {
40-
if(trace.zauto && trace.autocontour === false) {
41-
trace.zmin = start - cs / 2;
42-
trace.zmax = trace.zmin + nc * cs;
43-
}
41+
var zmin0 = cOpts.min;
42+
var zmax0 = cOpts.max;
4443

4544
for(i = 0; i < len; i++) {
4645
si = scl[i];
47-
48-
domain[i] = si[0] * (trace.zmax - trace.zmin) + trace.zmin;
46+
domain[i] = si[0] * (zmax0 - zmin0) + zmin0;
4947
range[i] = si[1];
5048
}
5149

5250
// do the contours extend beyond the colorscale?
5351
// if so, extend the colorscale with constants
5452
var zRange = d3.extent([
55-
trace.zmin,
56-
trace.zmax,
53+
zmin0,
54+
zmax0,
5755
contours.start,
5856
contours.start + cs * (nc - 1)
5957
]);
60-
var zmin = zRange[trace.zmin < trace.zmax ? 0 : 1];
61-
var zmax = zRange[trace.zmin < trace.zmax ? 1 : 0];
58+
var zmin = zRange[zmin0 < zmax0 ? 0 : 1];
59+
var zmax = zRange[zmin0 < zmax0 ? 1 : 0];
6260

63-
if(zmin !== trace.zmin) {
61+
if(zmin !== zmin0) {
6462
domain.splice(0, 0, zmin);
65-
range.splice(0, 0, Range[0]);
63+
range.splice(0, 0, range[0]);
6664
}
6765

68-
if(zmax !== trace.zmax) {
66+
if(zmax !== zmax0) {
6967
domain.push(zmax);
7068
range.push(range[range.length - 1]);
7169
}
7270
} else {
7371
for(i = 0; i < len; i++) {
7472
si = scl[i];
75-
7673
domain[i] = (si[0] * (nc + extra - 1) - (extra / 2)) * cs + start;
7774
range[i] = si[1];
7875
}
7976
}
8077

81-
return Colorscale.makeColorScaleFunc({
82-
domain: domain,
83-
range: range,
84-
}, {
85-
noNumericCheck: true
86-
});
78+
return Colorscale.makeColorScaleFunc(
79+
{domain: domain, range: range},
80+
{noNumericCheck: true}
81+
);
8782
};

src/traces/contour/plot.js

-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ exports.plot = function plot(gd, plotinfo, cdcontours, contourLayer) {
4343
var heatmapColoringLayer = Lib.ensureSingle(plotGroup, 'g', 'heatmapcoloring');
4444
var cdheatmaps = [];
4545
if(contours.coloring === 'heatmap') {
46-
if(trace.zauto && (trace.autocontour === false)) {
47-
trace._input.zmin = trace.zmin =
48-
contours.start - contours.size / 2;
49-
trace._input.zmax = trace.zmax =
50-
trace.zmin + pathinfo.length * contours.size;
51-
}
5246
cdheatmaps = [cd];
5347
}
5448
heatmapPlot(gd, plotinfo, cdheatmaps, heatmapColoringLayer);

src/traces/contour/set_contours.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Axes = require('../../plots/cartesian/axes');
1312
var Lib = require('../../lib');
1413

15-
16-
module.exports = function setContours(trace) {
14+
module.exports = function setContours(trace, vals) {
1715
var contours = trace.contours;
1816

1917
// check if we need to auto-choose contour levels
2018
if(trace.autocontour) {
19+
// N.B. do not try to use coloraxis cmin/cmax,
20+
// these values here are meant to remain "per-trace" for now
2121
var zmin = trace.zmin;
2222
var zmax = trace.zmax;
23-
if(zmin === undefined || zmax === undefined) {
24-
zmin = Lib.aggNums(Math.min, null, trace._z);
25-
zmax = Lib.aggNums(Math.max, null, trace._z);
23+
if(trace.zauto || zmin === undefined) {
24+
zmin = Lib.aggNums(Math.min, null, vals);
25+
}
26+
if(trace.zauto || zmax === undefined) {
27+
zmax = Lib.aggNums(Math.max, null, vals);
2628
}
27-
var dummyAx = autoContours(zmin, zmax, trace.ncontours);
2829

30+
var dummyAx = autoContours(zmin, zmax, trace.ncontours);
2931
contours.size = dummyAx.dtick;
30-
3132
contours.start = Axes.tickFirst(dummyAx);
3233
dummyAx.range.reverse();
3334
contours.end = Axes.tickFirst(dummyAx);

src/traces/contourcarpet/calc.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ module.exports = function calc(gd, trace) {
4545
}
4646

4747
var cd = heatmappishCalc(gd, trace);
48-
49-
setContours(trace);
48+
setContours(trace, trace._z);
5049

5150
return cd;
5251
};

src/traces/heatmap/calc.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,8 @@ module.exports = function calc(gd, trace) {
142142
cd0.pts = binned.pts;
143143
}
144144

145-
// auto-z and autocolorscale if applicable
146-
if(!isContour || trace.contours.type !== 'constraint') {
147-
colorscaleCalc(gd, trace, {
148-
vals: z,
149-
containerStr: '',
150-
cLetter: 'z'
151-
});
145+
if(!isContour) {
146+
colorscaleCalc(gd, trace, {vals: z, cLetter: 'z'});
152147
}
153148

154149
if(isContour && trace.contours && trace.contours.coloring === 'heatmap') {

0 commit comments

Comments
 (0)