Skip to content

Commit 14c7632

Browse files
authored
Merge pull request #4950 from plotly/fix4945-heatmapgl-coerce
Fix heatmapgl supplyDefaults
2 parents 3f1b015 + 682973d commit 14c7632

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

src/traces/heatmap/xyz_defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module.exports = function handleXYZDefaults(traceIn, traceOut, coerce, layout, x
4444
traceOut._length = null;
4545
}
4646

47+
if(traceIn.type === 'heatmapgl') return true; // until we handle calendars for heatmapgl
48+
4749
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
4850
handleCalendarDefaults(traceIn, traceOut, [xName, yName], layout);
4951

src/traces/heatmapgl/defaults.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2012-2020, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('../../lib');
13+
14+
var handleXYZDefaults = require('../heatmap/xyz_defaults');
15+
var colorscaleDefaults = require('../../components/colorscale/defaults');
16+
var attributes = require('./attributes');
17+
18+
19+
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
20+
function coerce(attr, dflt) {
21+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
22+
}
23+
24+
var validData = handleXYZDefaults(traceIn, traceOut, coerce, layout);
25+
if(!validData) {
26+
traceOut.visible = false;
27+
return;
28+
}
29+
30+
coerce('text');
31+
32+
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'});
33+
};

src/traces/heatmapgl/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
module.exports = {
1212
attributes: require('./attributes'),
13-
supplyDefaults: require('../heatmap/defaults'),
13+
supplyDefaults: require('./defaults'),
1414
colorbar: require('../heatmap/colorbar'),
1515

1616
calc: require('../heatmap/calc'),

test/jasmine/tests/heatmapgl_test.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
var supplyDefaults = require('@src/traces/heatmapgl').supplyDefaults;
2+
var Plots = require('@src/plots/plots');
3+
var Plotly = require('@lib/index');
4+
var schema = Plotly.PlotSchema.get();
5+
var attributeList = Object.getOwnPropertyNames(schema.traces.heatmapgl.attributes);
6+
7+
describe('heatmapgl supplyDefaults', function() {
8+
'use strict';
9+
10+
var traceIn;
11+
var traceOut;
12+
13+
var defaultColor = '#444';
14+
var layout = {
15+
font: Plots.layoutAttributes.font,
16+
_dfltTitle: {colorbar: 'cb'},
17+
_subplots: {cartesian: ['xy'], xaxis: ['x'], yaxis: ['y']}
18+
};
19+
20+
beforeEach(function() {
21+
traceOut = {};
22+
});
23+
24+
it('should set visible to false when z is empty', function() {
25+
traceIn = {
26+
z: []
27+
};
28+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
29+
expect(traceOut.visible).toBe(false);
30+
31+
traceIn = {
32+
z: [[]]
33+
};
34+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
35+
expect(traceOut.visible).toBe(false);
36+
37+
traceIn = {
38+
z: [[], [], []]
39+
};
40+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
41+
expect(traceOut.visible).toBe(false);
42+
43+
traceIn = {
44+
type: 'heatmapgl',
45+
z: [[1, 2], []]
46+
};
47+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
48+
expect(traceOut.visible).toBe(false);
49+
50+
traceIn = {
51+
type: 'heatmapgl',
52+
z: [[], [1, 2], [1, 2, 3]]
53+
};
54+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
55+
expect(traceOut.visible).toBe(false);
56+
});
57+
58+
it('should set visible to false when z is non-numeric', function() {
59+
traceIn = {
60+
type: 'heatmapgl',
61+
z: [['a', 'b'], ['c', 'd']]
62+
};
63+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
64+
expect(traceOut.visible).toBe(false);
65+
});
66+
67+
it('should set visible to false when z isn\'t column not a 2d array', function() {
68+
traceIn = {
69+
x: [1, 1, 1, 2, 2],
70+
y: [1, 2, 3, 1, 2],
71+
z: [1, ['this is considered a column'], 1, 2, 3]
72+
};
73+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
74+
expect(traceOut.visible).not.toBe(false);
75+
76+
traceIn = {
77+
x: [1, 1, 1, 2, 2],
78+
y: [1, 2, 3, 1, 2],
79+
z: [[0], ['this is not considered a column'], 1, ['nor 2d']]
80+
};
81+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
82+
expect(traceOut.visible).toBe(false);
83+
});
84+
85+
it('should only coerce attributes that are part of scheme', function() {
86+
traceIn = {
87+
type: 'heatmapgl',
88+
z: [[0, 1], [1, 0]]
89+
};
90+
91+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
92+
var allKeys = Object.getOwnPropertyNames(traceOut);
93+
allKeys.forEach(function(key) {
94+
if(key[0] !== '_') {
95+
expect(attributeList.indexOf(key)).not.toBe(-1, key);
96+
}
97+
});
98+
});
99+
});

0 commit comments

Comments
 (0)