Skip to content

Commit c740e3f

Browse files
authored
Merge pull request #3691 from plotly/heatmap-category-0
Fix bound arrays with "0" x/y categories
2 parents a4c185a + d3713e1 commit c740e3f

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

src/traces/heatmap/calc.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ module.exports = function calc(gd, trace) {
6767
y = trace.y ? ya.makeCalcdata(trace, 'y') : [];
6868
}
6969

70-
x0 = trace.x0 || 0;
71-
dx = trace.dx || 1;
72-
y0 = trace.y0 || 0;
73-
dy = trace.dy || 1;
70+
x0 = trace.x0;
71+
dx = trace.dx;
72+
y0 = trace.y0;
73+
dy = trace.dy;
7474

7575
z = clean2dArray(zIn, trace.transpose);
7676

src/traces/heatmap/make_bound_array.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ module.exports = function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks,
6363
}
6464
}
6565
else {
66-
dv = dvIn || 1;
67-
6866
var calendar = trace[ax._id.charAt(0) + 'calendar'];
6967

70-
if(isHist || ax.type === 'category' || ax.type === 'multicategory') {
71-
v0 = ax.r2c(v0In, 0, calendar) || 0;
72-
} else if(isArrayOrTypedArray(arrayIn) && arrayIn.length === 1) {
68+
if(isArrayOrTypedArray(arrayIn) && arrayIn.length === 1) {
7369
v0 = arrayIn[0];
7470
} else if(v0In === undefined) {
7571
v0 = 0;
72+
} else if(isHist || ax.type === 'category' || ax.type === 'multicategory') {
73+
v0 = ax.r2c(v0In, 0, calendar);
7674
} else {
7775
v0 = ax.d2c(v0In, 0, calendar);
7876
}
7977

78+
dv = dvIn || 1;
79+
8080
for(i = (isContour || isGL2D) ? 0 : -0.5; i < numbricks; i++) {
8181
arrayOut.push(v0 + dv * i);
8282
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"data": [
3+
{
4+
"x": [ 3, 2, 1, 0 ],
5+
"y": [ 3, 2, 1, 0 ],
6+
"z": [
7+
[ 98, 97, 96, null ],
8+
[ 88, 87, 86, null ],
9+
[ 78, 77, 76, null ],
10+
[ null, null, null, null ]
11+
],
12+
"type": "heatmap",
13+
"colorscale": [
14+
[ 0, "#3D9970" ],
15+
[ 1, "#001f3f" ]
16+
],
17+
"showscale": false,
18+
"xgap": 1,
19+
"ygap": 1
20+
}
21+
],
22+
"layout": {
23+
"height": 450,
24+
"width": 650,
25+
"xaxis": {
26+
"type": "category"
27+
},
28+
"yaxis": {
29+
"type": "category"
30+
}
31+
}
32+
}

test/jasmine/tests/heatmap_test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,11 @@ describe('heatmap convertColumnXYZ', function() {
287287
describe('heatmap calc', function() {
288288
'use strict';
289289

290-
function _calc(opts) {
290+
function _calc(opts, layout) {
291291
var base = { type: 'heatmap' };
292292
var trace = Lib.extendFlat({}, base, opts);
293293
var gd = { data: [trace] };
294+
if(layout) gd.layout = layout;
294295

295296
supplyAllDefaults(gd);
296297
var fullTrace = gd._fullData[0];
@@ -408,6 +409,20 @@ describe('heatmap calc', function() {
408409
expect(out.z).toBeCloseTo2DArray([[17, 18, 19]]);
409410
});
410411

412+
it('should handle the category case (edge case with a *0* category)', function() {
413+
var out = _calc({
414+
x: ['a', 'b', 0],
415+
y: ['z', 0, 'y'],
416+
z: [[17, 18, 19], [10, 20, 30], [40, 30, 20]]
417+
}, {
418+
xaxis: {type: 'category'},
419+
yaxis: {type: 'category'}
420+
});
421+
422+
expect(out.x).toBeCloseToArray([-0.5, 0.5, 1.5, 2.5]);
423+
expect(out.y).toBeCloseToArray([-0.5, 0.5, 1.5, 2.5]);
424+
});
425+
411426
it('should handle the category x/y/z/ column case', function() {
412427
var out = _calc({
413428
x: ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],

0 commit comments

Comments
 (0)