Skip to content

Commit 259abab

Browse files
authored
Merge pull request #6819 from andrew-matteson/log-scale-heatmap
Log-scale layout of heatmap bricks for log-scale axes (fixes #5991)
2 parents b31d45b + f9db904 commit 259abab

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

draftlogs/6891_change.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Improve rendering of heatmap bricks for log-scale axes [[#5991](https://github.com/plotly/plotly.js/issues/5991)], with thanks to @andrew-matteson for the contribution!

src/traces/heatmap/make_bound_array.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,25 @@ module.exports = function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks,
2424
// contour plots only want the centers
2525
if(isContour || isGL2D) arrayOut = Array.from(arrayIn).slice(0, numbricks);
2626
else if(numbricks === 1) {
27-
arrayOut = [arrayIn[0] - 0.5, arrayIn[0] + 0.5];
27+
if(ax.type === 'log') {
28+
arrayOut = [0.5 * arrayIn[0], 2 * arrayIn[0]];
29+
} else {
30+
arrayOut = [arrayIn[0] - 0.5, arrayIn[0] + 0.5];
31+
}
32+
} else if(ax.type === 'log') {
33+
arrayOut = [Math.pow(arrayIn[0], 1.5) / Math.pow(arrayIn[1], 0.5)];
34+
35+
for(i = 1; i < len; i++) {
36+
// Geomean
37+
arrayOut.push(Math.sqrt(arrayIn[i - 1] * arrayIn[i]));
38+
}
39+
40+
arrayOut.push(Math.pow(arrayIn[len - 1], 1.5) / Math.pow(arrayIn[len - 2], 0.5));
2841
} else {
2942
arrayOut = [1.5 * arrayIn[0] - 0.5 * arrayIn[1]];
3043

3144
for(i = 1; i < len; i++) {
45+
// Arithmetic mean
3246
arrayOut.push((arrayIn[i - 1] + arrayIn[i]) * 0.5);
3347
}
3448

@@ -37,11 +51,21 @@ module.exports = function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks,
3751

3852
if(len < numbricks) {
3953
var lastPt = arrayOut[arrayOut.length - 1];
40-
var delta = lastPt - arrayOut[arrayOut.length - 2];
54+
var delta; // either multiplicative delta (log axis type) or arithmetic delta (all other axis types)
55+
if(ax.type === 'log') {
56+
delta = lastPt / arrayOut[arrayOut.length - 2];
57+
58+
for(i = len; i < numbricks; i++) {
59+
lastPt *= delta;
60+
arrayOut.push(lastPt);
61+
}
62+
} else {
63+
delta = lastPt - arrayOut[arrayOut.length - 2];
4164

42-
for(i = len; i < numbricks; i++) {
43-
lastPt += delta;
44-
arrayOut.push(lastPt);
65+
for(i = len; i < numbricks; i++) {
66+
lastPt += delta;
67+
arrayOut.push(lastPt);
68+
}
4569
}
4670
}
4771
} else {
25.3 KB
Loading
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"data": [
3+
{
4+
"type": "heatmap",
5+
"x": [
6+
0.0001,
7+
0.001,
8+
0.01
9+
],
10+
"y": [
11+
0.0001,
12+
0.001,
13+
0.01
14+
],
15+
"z": [
16+
[
17+
1,
18+
2,
19+
3
20+
],
21+
[
22+
3,
23+
2,
24+
1
25+
],
26+
[
27+
7,
28+
2,
29+
7
30+
]
31+
]
32+
}
33+
],
34+
"layout": {
35+
"title": {"text": "log scale heatmap"},
36+
"xaxis": {
37+
"type": "log"
38+
},
39+
"yaxis": {
40+
"type": "log"
41+
},
42+
"height": 598,
43+
"width": 1080,
44+
"autosize": true,
45+
"showlegend": false
46+
}
47+
}

0 commit comments

Comments
 (0)