Skip to content

Commit 2ae4c6d

Browse files
authored
Merge pull request #1309 from plotly/contour-bug
Contour bug
2 parents e2105d2 + ebaaca3 commit 2ae4c6d

File tree

8 files changed

+178
-18
lines changed

8 files changed

+178
-18
lines changed

src/traces/contour/colorbar.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Plots = require('../../plots/plots');
1313
var drawColorbar = require('../../components/colorbar/draw');
1414

1515
var makeColorMap = require('./make_color_map');
16+
var endPlus = require('./end_plus');
1617

1718

1819
module.exports = function colorbar(gd, cd) {
@@ -52,7 +53,7 @@ module.exports = function colorbar(gd, cd) {
5253
})
5354
.levels({
5455
start: contours.start,
55-
end: contours.end,
56+
end: endPlus(contours),
5657
size: cs
5758
})
5859
.options(trace.colorbar)();

src/traces/contour/end_plus.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2017, 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+
10+
'use strict';
11+
12+
/*
13+
* tiny helper to move the end of the contours a little to prevent
14+
* losing the last contour to rounding errors
15+
*/
16+
module.exports = function endPlus(contours) {
17+
return contours.end + contours.size / 1e6;
18+
};

src/traces/contour/find_all_paths.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ function makePath(pi, loc, edgeflag) {
8484
if(equalPts(pts[pts.length - 1], pts[pts.length - 2])) pts.pop();
8585
locStr = loc.join(',');
8686

87+
var atEdge = (marchStep[0] && (loc[0] < 0 || loc[0] > n - 2)) ||
88+
(marchStep[1] && (loc[1] < 0 || loc[1] > m - 2)),
89+
closedLoop = (locStr === startLocStr) && (marchStep.join(',') === startStepStr);
90+
8791
// have we completed a loop, or reached an edge?
88-
if((locStr === startLocStr && marchStep.join(',') === startStepStr) ||
89-
(edgeflag && (
90-
(marchStep[0] && (loc[0] < 0 || loc[0] > n - 2)) ||
91-
(marchStep[1] && (loc[1] < 0 || loc[1] > m - 2))))) {
92-
break;
93-
}
92+
if((closedLoop) || (edgeflag && atEdge)) break;
93+
9494
mi = pi.crossings[locStr];
9595
}
9696

src/traces/contour/make_color_map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
var d3 = require('d3');
1313
var Colorscale = require('../../components/colorscale');
14+
var endPlus = require('./end_plus');
1415

1516
module.exports = function makeColorMap(trace) {
1617
var contours = trace.contours,
1718
start = contours.start,
18-
end = contours.end,
19+
end = endPlus(contours),
1920
cs = contours.size || 1,
20-
nc = Math.floor((end + cs / 10 - start) / cs) + 1,
21+
nc = Math.floor((end - start) / cs) + 1,
2122
extra = contours.coloring === 'lines' ? 0 : 1;
2223

2324
var scl = trace.colorscale,

src/traces/contour/plot.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Drawing = require('../../components/drawing');
1717
var heatmapPlot = require('../heatmap/plot');
1818
var makeCrossings = require('./make_crossings');
1919
var findAllPaths = require('./find_all_paths');
20+
var endPlus = require('./end_plus');
2021

2122

2223
module.exports = function plot(gd, plotinfo, cdcontours) {
@@ -81,9 +82,10 @@ function plotOne(gd, plotinfo, cd) {
8182

8283
function emptyPathinfo(contours, plotinfo, cd0) {
8384
var cs = contours.size,
84-
pathinfo = [];
85+
pathinfo = [],
86+
end = endPlus(contours);
8587

86-
for(var ci = contours.start; ci < contours.end + cs / 10; ci += cs) {
88+
for(var ci = contours.start; ci < end; ci += cs) {
8789
pathinfo.push({
8890
level: ci,
8991
// all the cells with nontrivial marching index
@@ -163,7 +165,8 @@ function makeFills(plotgroup, pathinfo, perimeter, contours) {
163165
}
164166

165167
function joinAllPaths(pi, perimeter) {
166-
var fullpath = (pi.edgepaths.length || pi.z[0][0] < pi.level) ?
168+
var edgeVal2 = Math.min(pi.z[0][0], pi.z[0][1]),
169+
fullpath = (pi.edgepaths.length || edgeVal2 <= pi.level) ?
167170
'' : ('M' + perimeter.join('L') + 'Z'),
168171
i = 0,
169172
startsleft = pi.edgepaths.map(function(v, i) { return i; }),

src/traces/contour/style.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,26 @@ module.exports = function style(gd) {
3434

3535
var colorMap = makeColorMap(trace);
3636

37-
c.selectAll('g.contourlevel').each(function(d, i) {
37+
c.selectAll('g.contourlevel').each(function(d) {
3838
d3.select(this).selectAll('path')
3939
.call(Drawing.lineGroupStyle,
4040
line.width,
41-
contours.coloring === 'lines' ? colorMap(start + i * cs) : line.color,
41+
contours.coloring === 'lines' ? colorMap(d.level) : line.color,
4242
line.dash);
4343
});
4444

45-
c.selectAll('g.contourbg path')
46-
.style('fill', colorMap(start - cs / 2));
45+
var firstFill;
4746

4847
c.selectAll('g.contourfill path')
49-
.style('fill', function(d, i) {
50-
return colorMap(start + (i + 0.5) * cs);
48+
.style('fill', function(d) {
49+
if(firstFill === undefined) firstFill = d.level;
50+
return colorMap(d.level + 0.5 * cs);
5151
});
52+
53+
if(firstFill === undefined) firstFill = start;
54+
55+
c.selectAll('g.contourbg path')
56+
.style('fill', colorMap(firstFill - 0.5 * cs));
5257
});
5358

5459
heatmapStyle(gd);
61.6 KB
Loading
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"data": [
3+
{
4+
"x": [0, 5],
5+
"y": [0, 4],
6+
"z": [[4, 3], [2, 1]],
7+
"type": "contour",
8+
"contours": {
9+
"start": 1.1,
10+
"end": 4.09,
11+
"size": 1
12+
},
13+
"colorbar": {"x": 0.4, "y": 0.9, "len": 0.2}
14+
},
15+
{
16+
"x": [0, 5],
17+
"y": [0, 4],
18+
"z": [[4, 3], [2, 1]],
19+
"type": "contour",
20+
"contours": {
21+
"start": 1,
22+
"end": 4,
23+
"size": 0.9999999
24+
},
25+
"colorbar": {"x": 1, "y": 0.9, "len": 0.2},
26+
"xaxis": "x2"
27+
},
28+
{
29+
"z": [[0, 0, 0, 0, 0, 0],
30+
[0, 0, 9, 0, 0, 0],
31+
[0, 0, 0, 0, 0, 0],
32+
[0, 0, 0, -9, 0, 0],
33+
[0, 0, 0, 0, 0, 0]],
34+
"type": "contour",
35+
"contours": {
36+
"start": -0.000001,
37+
"end": 0.000001,
38+
"size": 0.000001
39+
},
40+
"colorbar": {"x": 0.4, "y": 0.65, "len": 0.2},
41+
"yaxis": "y2"
42+
},
43+
{
44+
"z": [[0, 0, 0, 0, 0, 0],
45+
[0, 0, 9, -9, 0, 0],
46+
[0, 0, 9, -9, 0, 0],
47+
[0, 0, 9, -9, 0, 0],
48+
[0, 0, 0, 0, 0, 0]],
49+
"type": "contour",
50+
"contours": {
51+
"start": -0.000001,
52+
"end": 0.000001,
53+
"size": 0.000001
54+
},
55+
"colorbar": {"x": 1, "y": 0.65, "len": 0.2},
56+
"xaxis": "x2",
57+
"yaxis": "y2"
58+
},
59+
{
60+
"z": [[0, 0, 0, 0, 0, 0],
61+
[0, 9, 0, 0, 0, 0],
62+
[0, 0, 0, 0, 0, 0],
63+
[0, 0, 0, 0, -9, 0],
64+
[0, 0, 0, 0, 0, 0]],
65+
"type": "contour",
66+
"contours": {
67+
"start": -0.000001,
68+
"end": 0.000001,
69+
"size": 0.000001
70+
},
71+
"colorbar": {"x": 0.4, "y": 0.4, "len": 0.2},
72+
"yaxis": "y3"
73+
},
74+
{
75+
"z": [[0, 0, 0, 0, 0, 0],
76+
[0, 0, -9, 0, 0, 0],
77+
[0, 0, 0, 0, 0, 0],
78+
[0, 0, 0, 9, 0, 0],
79+
[0, 0, 0, 0, 0, 0]],
80+
"type": "contour",
81+
"contours": {
82+
"start": -0.0000005,
83+
"end": 0.000001,
84+
"size": 0.000001
85+
},
86+
"colorbar": {"x": 1, "y": 0.4, "len": 0.2},
87+
"xaxis": "x2",
88+
"yaxis": "y3"
89+
},
90+
{
91+
"z": [[0, 0, 0, 0, 0, 0],
92+
[0, 9, 0, 0, 0, 0],
93+
[0, 0, 0, 0, 0, 0],
94+
[0, 0, 0, 0, -9, 0],
95+
[0, 0, 0, 0, 0, 0]],
96+
"type": "contour",
97+
"contours": {
98+
"start": -0.0000005,
99+
"end": 0.000001,
100+
"size": 0.000001
101+
},
102+
"colorbar": {"x": 0.4, "y": 0.15, "len": 0.2},
103+
"yaxis": "y4"
104+
},
105+
{
106+
"z": [[0, 0, 0, 0, 0, 0],
107+
[0, 0, 9, 0, 0, 0],
108+
[0, -9, -9, -9, -9, 0],
109+
[0, 0, 0, 9, 0, 0],
110+
[0, 0, 0, 0, 0, 0]],
111+
"type": "contour",
112+
"contours": {
113+
"start": -0.0000005,
114+
"end": 0.000001,
115+
"size": 0.000001
116+
},
117+
"colorbar": {"x": 1, "y": 0.15, "len": 0.2},
118+
"xaxis": "x2",
119+
"yaxis": "y4"
120+
}
121+
],
122+
"layout": {
123+
"xaxis": {"domain": [0, 0.4]},
124+
"xaxis2": {"domain": [0.6, 1]},
125+
"yaxis": {"domain": [0.8, 1]},
126+
"yaxis2": {"domain": [0.55, 0.75]},
127+
"yaxis3": {"domain": [0.3, 0.5]},
128+
"yaxis4": {"domain": [0.05, 0.25]},
129+
"width": 600,
130+
"height": 800
131+
}
132+
}

0 commit comments

Comments
 (0)