Skip to content

Commit fa04037

Browse files
committed
image: fix log axis range, add support for categorical axis
1 parent 2d393dd commit fa04037

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

src/traces/image/attributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ module.exports = extendFlat({
7171
].join(' ')
7272
},
7373
x0: {
74-
valType: 'number',
74+
valType: 'any',
7575
dflt: 0,
7676
role: 'info',
7777
editType: 'calc',
7878
description: 'Set the image\'s x position.'
7979
},
8080
y0: {
81-
valType: 'number',
81+
valType: 'any',
8282
dflt: 0,
8383
role: 'info',
8484
editType: 'calc',

src/traces/image/calc.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ module.exports = function calc(gd, trace) {
1515
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
1616
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
1717

18-
var x0 = trace.x0 - trace.dx / 2;
19-
var y0 = trace.y0 - trace.dy / 2;
18+
var x0 = xa.d2c(trace.x0) - trace.dx / 2;
19+
var y0 = ya.d2c(trace.y0) - trace.dy / 2;
2020
var h = trace.z.length;
2121
var w = maxRowLength(trace.z);
2222

23-
trace._extremes[xa._id] = Axes.findExtremes(xa, [x0, x0 + w * trace.dx]);
24-
trace._extremes[ya._id] = Axes.findExtremes(ya, [y0, y0 + h * trace.dy]);
23+
// Set axis range
24+
var i;
25+
var xrange = [x0, x0 + w * trace.dx];
26+
var yrange = [y0, y0 + h * trace.dy];
27+
if(xa && xa.type === 'log') for(i = 0; i < w; i++) xrange.push(x0 + i * trace.dx);
28+
if(ya && ya.type === 'log') for(i = 0; i < h; i++) yrange.push(y0 + i * trace.dy);
29+
trace._extremes[xa._id] = Axes.findExtremes(xa, xrange);
30+
trace._extremes[ya._id] = Axes.findExtremes(ya, yrange);
2531

2632
var cd0 = {
2733
x0: x0,

src/traces/image/plot.js

+29-9
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,30 @@ module.exports.plot = function(gd, plotinfo, cdimage, imageLayer) {
7171
var h = cd0.h;
7272
var dx = trace.dx;
7373
var dy = trace.dy;
74-
var left = xa.c2p(x0);
75-
var right = xa.c2p(x0 + w * dx);
76-
var top = ya.c2p(y0);
77-
var bottom = ya.c2p(y0 + h * dy);
7874

79-
var temp;
75+
var left, right, temp, top, bottom, i;
76+
// in case of log of a negative
77+
i = 0;
78+
while(left === undefined && i < w) {
79+
left = xa.c2p(x0 + i * dx);
80+
i++;
81+
}
82+
i = w;
83+
while(right === undefined && i > 0) {
84+
right = xa.c2p(x0 + i * dx);
85+
i--;
86+
}
87+
i = 0;
88+
while(top === undefined && i < h) {
89+
top = ya.c2p(y0 + i * dy);
90+
i++;
91+
}
92+
i = h;
93+
while(bottom === undefined && i > 0) {
94+
bottom = ya.c2p(y0 + i * dy);
95+
i--;
96+
}
97+
8098
if(right < left) {
8199
temp = right;
82100
right = left;
@@ -117,13 +135,15 @@ module.exports.plot = function(gd, plotinfo, cdimage, imageLayer) {
117135
trace._scaler = scaler(trace);
118136
var fmt = constants.colormodel[trace.colormodel].fmt;
119137
var c;
120-
for(var i = 0; i < cd0.w; i++) {
121-
if(ipx(i + 1) === ipx(i)) continue;
138+
for(i = 0; i < cd0.w; i++) {
139+
var ipx0 = ipx(i); var ipx1 = ipx(i + 1);
140+
if(ipx1 === ipx0 || isNaN(ipx1) || isNaN(ipx0)) continue;
122141
for(var j = 0; j < cd0.h; j++) {
123-
if(jpx(j + 1) === jpx(j) || !z[j][i]) continue;
142+
var jpx0 = jpx(j); var jpx1 = jpx(j + 1);
143+
if(jpx1 === jpx0 || isNaN(jpx1) || isNaN(jpx0) || !z[j][i]) continue;
124144
c = trace._scaler(z[j][i]);
125145
context.fillStyle = trace.colormodel + '(' + fmt(c).join(',') + ')';
126-
context.fillRect(ipx(i), jpx(j), ipx(i + 1) - ipx(i), jpx(j + 1) - jpx(j));
146+
context.fillRect(ipx0, jpx0, ipx1 - ipx0, jpx1 - jpx0);
127147
}
128148
}
129149

20.2 KB
Loading

test/image/mocks/image_axis_type.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"data": [{
3+
"type": "scatter",
4+
"y": ["E", "F", "G", "H"],
5+
"x": ["A", "B", "C", "D"]
6+
}, {
7+
"type": "image",
8+
"zmax": [1, 1, 1],
9+
"x0": "B",
10+
"y0": "F",
11+
"z": [
12+
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]],
13+
[[0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 0]],
14+
[[1, 0, 0], [1, 0, 0], [0, 0, 1], [0, 0, 0]]
15+
]
16+
}, {
17+
"type": "image",
18+
"zmax": [1, 1, 1],
19+
"xaxis": "x2",
20+
"yaxis": "y2",
21+
"z": [
22+
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]],
23+
[[0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 0]],
24+
[[1, 0, 0], [1, 0, 0], [0, 0, 1], [0, 0, 0]]
25+
]
26+
}],
27+
"layout": {
28+
"width": 400, "height": 600, "title": {"text": "Image on categorical and log axes"},
29+
"grid": {"rows": 2, "columns": 1, "pattern": "independent"},
30+
"xaxis2": {"type": "log"},
31+
"yaxis2": {"type": "log"}
32+
}
33+
}

0 commit comments

Comments
 (0)