Skip to content

Commit aef563b

Browse files
authored
Merge pull request #5985 from andrew-matteson/log-contour
Interpolate contours in linear space, not data space
2 parents 354a7a1 + 1dea4a7 commit aef563b

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

draftlogs/5985_change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Improve drawing the contour lines in non-linear space e.g. on log axes [[#5985](https://github.com/plotly/plotly.js/pull/5985)], with thanks to @andrew-matteson for the contribution!

src/traces/contour/find_all_paths.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,25 @@ function getInterpPx(pi, loc, step) {
269269
var xa = pi.xaxis;
270270
var ya = pi.yaxis;
271271

272+
// Interpolate in linear space, then convert to pixel
272273
if(step[1]) {
273274
var dx = (pi.level - zxy) / (pi.z[locy][locx + 1] - zxy);
275+
// Interpolate, but protect against NaN linear values for log axis (dx will equal 1 or 0)
276+
var dxl =
277+
(dx !== 1 ? (1 - dx) * xa.c2l(pi.x[locx]) : 0) +
278+
(dx !== 0 ? dx * xa.c2l(pi.x[locx + 1]) : 0);
274279

275-
return [xa.c2p((1 - dx) * pi.x[locx] + dx * pi.x[locx + 1], true),
280+
return [xa.c2p(xa.l2c(dxl), true),
276281
ya.c2p(pi.y[locy], true),
277282
locx + dx, locy];
278283
} else {
279284
var dy = (pi.level - zxy) / (pi.z[locy + 1][locx] - zxy);
285+
var dyl =
286+
(dy !== 1 ? (1 - dy) * ya.c2l(pi.y[locy]) : 0) +
287+
(dy !== 0 ? dy * ya.c2l(pi.y[locy + 1]) : 0);
288+
280289
return [xa.c2p(pi.x[locx], true),
281-
ya.c2p((1 - dy) * pi.y[locy] + dy * pi.y[locy + 1], true),
290+
ya.c2p(ya.l2c(dyl), true),
282291
locx, locy + dy];
283292
}
284293
}

test/image/baselines/contour_log.png

-1.05 KB
Loading
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"data": [
3+
{
4+
"x": [1,2,3,4,5],
5+
"y": [1,2,3,4,5],
6+
"z": [
7+
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
8+
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
9+
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
10+
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
11+
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
12+
],
13+
"contours": {"start": 10, "end": 90, "size": 10},
14+
"type": "contour"
15+
},
16+
{
17+
"x": [1,10,100,1000,10000],
18+
"y": [1,2,3,4,5],
19+
"z": [
20+
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
21+
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
22+
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
23+
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
24+
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
25+
],
26+
"contours": {"start": 10, "end": 90, "size": 10},
27+
"type": "contour",
28+
"showscale": false,
29+
"xaxis": "x2"
30+
},
31+
{
32+
"x": [1,2,3,4,5],
33+
"y": [1,10,100,1000,10000],
34+
"z": [
35+
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
36+
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
37+
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
38+
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
39+
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
40+
],
41+
"contours": {"start": 10, "end": 90, "size": 10},
42+
"type": "contour",
43+
"showscale": false,
44+
"yaxis": "y2"
45+
},
46+
{
47+
"x": [1,10,100,1000,10000],
48+
"y": [1,10,100,1000,10000],
49+
"z": [
50+
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
51+
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
52+
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
53+
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
54+
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
55+
],
56+
"contours": {"start": 10, "end": 90, "size": 10},
57+
"type": "contour",
58+
"showscale": false,
59+
"xaxis": "x2",
60+
"yaxis": "y2"
61+
}
62+
],
63+
"layout": {
64+
"xaxis": {"domain": [0, 0.45]},
65+
"xaxis2": {"domain": [0.55, 1], "type": "log"},
66+
"yaxis": {"domain": [0, 0.45]},
67+
"yaxis2": {"domain": [0.55, 1], "type": "log"},
68+
"margin": {"l": 50, "b": 50, "r": 110, "t": 10},
69+
"width": 500,
70+
"height": 400
71+
}
72+
}

0 commit comments

Comments
 (0)