Skip to content

Commit 14133af

Browse files
authored
Merge pull request #4038 from plotly/clean2darray-fix
Fix clean2dArray for categorial x/y with extra items
2 parents 323e79a + 7ef9fd4 commit 14133af

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/traces/heatmap/clean_2d_array.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ module.exports = function clean2dArray(zOld, trace, xa, ya) {
2525
for(i = 0; i < zOld.length; i++) rowlen = Math.max(rowlen, zOld[i].length);
2626
if(rowlen === 0) return false;
2727
getCollen = function(zOld) { return zOld.length; };
28-
old2new = function(zOld, i, j) { return zOld[j][i]; };
28+
old2new = function(zOld, i, j) { return (zOld[j] || [])[i]; };
2929
} else {
3030
rowlen = zOld.length;
3131
getCollen = function(zOld, i) { return zOld[i].length; };
32-
old2new = function(zOld, i, j) { return zOld[i][j]; };
32+
old2new = function(zOld, i, j) { return (zOld[i] || [])[j]; };
3333
}
3434

3535
var padOld2new = function(zOld, i, j) {
@@ -58,9 +58,9 @@ module.exports = function clean2dArray(zOld, trace, xa, ya) {
5858
var xMap = axisMapping(xa);
5959
var yMap = axisMapping(ya);
6060

61+
if(ya && ya.type === 'category') rowlen = ya._categories.length;
6162
var zNew = new Array(rowlen);
6263

63-
if(ya && ya.type === 'category') rowlen = ya._categories.length;
6464
for(i = 0; i < rowlen; i++) {
6565
if(xa && xa.type === 'category') {
6666
collen = xa._categories.length;

test/jasmine/tests/heatmap_test.js

+77-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
1212
var supplyAllDefaults = require('../assets/supply_defaults');
1313
var failTest = require('../assets/fail_test');
1414

15-
1615
describe('heatmap supplyDefaults', function() {
1716
'use strict';
1817

@@ -542,6 +541,83 @@ describe('heatmap calc', function() {
542541
expect(out.z[0][0]).toEqual(0);
543542
});
544543
});
544+
545+
describe('should clean z array linked to category x/y coordinates', function() {
546+
var z = [
547+
[1, 20, 30, 50, 1],
548+
[20, 1, 60, 80, 30],
549+
[30, 60, 1, -10, 20]
550+
];
551+
552+
it('- base case', function() {
553+
var out = _calc({
554+
z: z,
555+
x: ['a', 'b', 'c', 'd', 'f'],
556+
y: ['A', 'B', 'C']
557+
});
558+
expect(out.z).toBeCloseTo2DArray([
559+
[1, 20, 30, 50, 1],
560+
[20, 1, 60, 80, 30],
561+
[30, 60, 1, -10, 20]
562+
]);
563+
});
564+
565+
it('- with extra x items', function() {
566+
var out = _calc({
567+
z: z,
568+
x: ['a', 'b', 'c', 'd', 'f', ''],
569+
y: ['A', 'B', 'C']
570+
});
571+
expect(out.z).toBeCloseTo2DArray([
572+
[1, 20, 30, 50, 1, undefined],
573+
[20, 1, 60, 80, 30, undefined],
574+
[30, 60, 1, -10, 20, undefined]
575+
]);
576+
});
577+
578+
it('- with extra y items', function() {
579+
var out = _calc({
580+
z: z,
581+
x: ['a', 'b', 'c', 'd', 'f'],
582+
y: ['A', 'B', 'C', '']
583+
});
584+
expect(out.z).toBeCloseTo2DArray([
585+
[1, 20, 30, 50, 1],
586+
[20, 1, 60, 80, 30],
587+
[30, 60, 1, -10, 20],
588+
new Array(5)
589+
]);
590+
});
591+
592+
it('- with extra x and y items', function() {
593+
var out = _calc({
594+
z: z,
595+
x: ['a', 'b', 'c', 'd', 'f', ''],
596+
y: ['A', 'B', 'C', '']
597+
});
598+
expect(out.z).toBeCloseTo2DArray([
599+
[1, 20, 30, 50, 1, undefined],
600+
[20, 1, 60, 80, 30, undefined],
601+
[30, 60, 1, -10, 20, undefined],
602+
new Array(6)
603+
]);
604+
});
605+
606+
it('- transposed, with extra x and y items', function() {
607+
var out = _calc({
608+
transpose: true,
609+
z: z,
610+
x: ['a', 'b', 'c', 'd', 'f', ''],
611+
y: ['A', 'B', 'C', '']
612+
});
613+
expect(out.z).toBeCloseTo2DArray([
614+
[1, 20, 30, undefined, undefined, undefined],
615+
[20, 1, 60, undefined, undefined, undefined],
616+
[30, 60, 1, undefined, undefined, undefined],
617+
[50, 80, -10, undefined, undefined, undefined]
618+
]);
619+
});
620+
});
545621
});
546622

547623
describe('heatmap plot', function() {

0 commit comments

Comments
 (0)