Skip to content

Commit 9046da1

Browse files
committed
fix contour label rendering for non-monotonically increasing x/y
1 parent 7a1ed7b commit 9046da1

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

src/traces/contour/plot.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ exports.plot = function plot(gd, plotinfo, cdcontours, contourLayer) {
7070
// draw everything
7171
makeBackground(plotGroup, perimeter, contours);
7272
makeFills(plotGroup, fillPathinfo, perimeter, contours);
73-
makeLinesAndLabels(plotGroup, pathinfo, gd, cd0, contours, perimeter);
73+
makeLinesAndLabels(plotGroup, pathinfo, gd, cd0, contours);
7474
clipGaps(plotGroup, plotinfo, gd, cd0, perimeter);
7575
});
7676
};
@@ -209,7 +209,7 @@ function joinAllPaths(pi, perimeter) {
209209
return fullpath;
210210
}
211211

212-
function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
212+
function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours) {
213213
var lineContainer = Lib.ensureSingle(plotgroup, 'g', 'contourlines');
214214
var showLines = contours.showlines !== false;
215215
var showLabels = contours.showlabels;
@@ -244,16 +244,19 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
244244
.attr('data-notex', 1)
245245
.call(Drawing.font, contours.labelfont);
246246

247+
var _x = cd0.x.slice().sort(Lib.sorterAsc);
248+
var _y = cd0.y.slice().sort(Lib.sorterAsc);
249+
247250
var xa = pathinfo[0].xaxis;
248251
var ya = pathinfo[0].yaxis;
249252
var xLen = xa._length;
250253
var yLen = ya._length;
251254
var xRng = xa.range;
252255
var yRng = ya.range;
253-
var x0 = Math.max(perimeter[0][0], 0);
254-
var x1 = Math.min(perimeter[2][0], xLen);
255-
var y0 = Math.max(perimeter[0][1], 0);
256-
var y1 = Math.min(perimeter[2][1], yLen);
256+
var x0 = Math.max(xa.c2p(_x[0], true), 0);
257+
var x1 = Math.min(xa.c2p(_x[_x.length - 1], true), xLen);
258+
var y0 = Math.max(ya.c2p(_y[_y.length - 1], true), 0);
259+
var y1 = Math.min(ya.c2p(_y[0], true), yLen);
257260

258261
// visible bounds of the contour trace (and the midpoints, to
259262
// help with cost calculations)
Loading
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"data": [
3+
{
4+
"type": "contour",
5+
"name": "▲ x ▲ y",
6+
"x": [1, 2, 3, 4],
7+
"y": [1, 2, 3, 4],
8+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
9+
"contours": {"showlabels": true},
10+
"showscale": false
11+
},
12+
{
13+
"type": "contour",
14+
"name": "▼ x ▲ y",
15+
"x": [4, 3, 2, 1],
16+
"y": [1, 2, 3, 4],
17+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
18+
"contours": {"showlabels": true},
19+
"showscale": false,
20+
"xaxis": "x2",
21+
"yaxis": "y2"
22+
},
23+
{
24+
"type": "contour",
25+
"name": "▲ x ▼ y",
26+
"x": [1, 2, 3, 4],
27+
"y": [4, 3, 2, 1],
28+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
29+
"contours": {"showlabels": true},
30+
"showscale": false,
31+
"xaxis": "x3",
32+
"yaxis": "y3"
33+
},
34+
{
35+
"type": "contour",
36+
"name": "▼ x ▼ y",
37+
"x": [4, 3, 2, 1],
38+
"y": [4, 3, 2, 1],
39+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
40+
"contours": {"showlabels": true},
41+
"showscale": false,
42+
"xaxis": "x4",
43+
"yaxis": "y4"
44+
},
45+
46+
{
47+
"type": "contour",
48+
"name": "▲ x ▲ y (dup)",
49+
"x": [1, 2, 3, 4],
50+
"y": [1, 2, 3, 4],
51+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
52+
"contours": {"showlabels": true},
53+
"showscale": false,
54+
"xaxis": "x5",
55+
"yaxis": "y5"
56+
},
57+
{
58+
"type": "contour",
59+
"name": "▼ x (reversed rng) ▲ y",
60+
"x": [4, 3, 2, 1],
61+
"y": [1, 2, 3, 4],
62+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
63+
"contours": {"showlabels": true},
64+
"showscale": false,
65+
"xaxis": "x6",
66+
"yaxis": "y6"
67+
},
68+
{
69+
"type": "contour",
70+
"name": "▲ x ▼ y (reversed rng)",
71+
"x": [1, 2, 3, 4],
72+
"y": [4, 3, 2, 1],
73+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
74+
"contours": {"showlabels": true},
75+
"showscale": false,
76+
"xaxis": "x7",
77+
"yaxis": "y7"
78+
},
79+
{
80+
"type": "contour",
81+
"name": "▼ x (reversed rng) ▼ y (reversed rng)",
82+
"x": [4, 3, 2, 1],
83+
"y": [4, 3, 2, 1],
84+
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
85+
"contours": {"showlabels": true},
86+
"showscale": false,
87+
"xaxis": "x8",
88+
"yaxis": "y8"
89+
}
90+
],
91+
"layout": {
92+
"grid": {"rows": 4, "columns": 2, "pattern": "independent"},
93+
94+
"template": {
95+
"data": {
96+
"contour": [{
97+
"hoverlabel": {"namelength": -1}
98+
}]
99+
}
100+
},
101+
102+
"xaxis6": {"autorange": "reversed"},
103+
104+
"yaxis7": {"autorange": "reversed"},
105+
106+
"xaxis8": {"autorange": "reversed"},
107+
"yaxis8": {"autorange": "reversed"},
108+
109+
"width": 500,
110+
"height": 800
111+
}
112+
}

0 commit comments

Comments
 (0)