Skip to content

Commit 34af401

Browse files
authored
Merge pull request #3934 from plotly/contour-label-reversed-xy
Fix contour label rendering for non-monotonically increasing x/y
2 parents 7a1ed7b + 9f6d1b6 commit 34af401

File tree

4 files changed

+130
-12
lines changed

4 files changed

+130
-12
lines changed

src/traces/carpet/plot.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,22 @@ function drawAxisLabels(gd, xaxis, yaxis, trace, t, layer, labels, labelClass) {
173173
function drawAxisTitles(gd, layer, trace, t, xa, ya, labelOrientationA, labelOrientationB) {
174174
var a, b, xy, dxy;
175175

176-
var _a = trace.a.slice().sort(Lib.sorterAsc);
177-
var _b = trace.b.slice().sort(Lib.sorterAsc);
176+
var aMin = Lib.aggNums(Math.min, null, trace.a);
177+
var aMax = Lib.aggNums(Math.max, null, trace.a);
178+
var bMin = Lib.aggNums(Math.min, null, trace.b);
179+
var bMax = Lib.aggNums(Math.max, null, trace.b);
178180

179-
a = 0.5 * (_a[0] + _a[_a.length - 1]);
180-
b = _b[0];
181+
a = 0.5 * (aMin + aMax);
182+
b = bMin;
181183
xy = trace.ab2xy(a, b, true);
182184
dxy = trace.dxyda_rough(a, b);
183185
if(labelOrientationA.angle === undefined) {
184186
Lib.extendFlat(labelOrientationA, orientText(trace, xa, ya, xy, trace.dxydb_rough(a, b)));
185187
}
186188
drawAxisTitle(gd, layer, trace, t, xy, dxy, trace.aaxis, xa, ya, labelOrientationA, 'a-title');
187189

188-
a = _a[0];
189-
b = 0.5 * (_b[0] + _b[_b.length - 1]);
190+
a = aMin;
191+
b = 0.5 * (bMin + bMax);
190192
xy = trace.ab2xy(a, b, true);
191193
dxy = trace.dxydb_rough(a, b);
192194
if(labelOrientationB.angle === undefined) {

src/traces/contour/plot.js

+10-6
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;
@@ -250,10 +250,14 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
250250
var yLen = ya._length;
251251
var xRng = xa.range;
252252
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);
253+
var xMin = Lib.aggNums(Math.min, null, cd0.x);
254+
var xMax = Lib.aggNums(Math.max, null, cd0.x);
255+
var yMin = Lib.aggNums(Math.min, null, cd0.y);
256+
var yMax = Lib.aggNums(Math.max, null, cd0.y);
257+
var x0 = Math.max(xa.c2p(xMin, true), 0);
258+
var x1 = Math.min(xa.c2p(xMax, true), xLen);
259+
var y0 = Math.max(ya.c2p(yMax, true), 0);
260+
var y1 = Math.min(ya.c2p(yMin, true), yLen);
257261

258262
// visible bounds of the contour trace (and the midpoints, to
259263
// help with cost calculations)
Loading
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)