Skip to content

Commit 22cfa82

Browse files
committed
🔒 down category ordering in histogram2d(contour) with tests
1 parent 59edb57 commit 22cfa82

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

test/jasmine/tests/histogram2d_test.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,29 @@ describe('Test histogram2d', function() {
116116

117117

118118
describe('calc', function() {
119-
function _calc(opts) {
119+
function _calc(opts, layout) {
120120
var base = { type: 'histogram2d' };
121121
var trace = Lib.extendFlat({}, base, opts);
122122
var gd = { data: [trace] };
123+
if(layout) gd.layout = layout;
123124

124125
supplyAllDefaults(gd);
125126
var fullTrace = gd._fullData[0];
127+
var fullLayout = gd._fullLayout;
128+
129+
fullTrace._extremes = {};
130+
131+
// we used to call ax.setScale during supplyDefaults, and this had a
132+
// fallback to provide _categories and _categoriesMap. Now neither of
133+
// those is true... anyway the right way to do this though is
134+
// ax.clearCalc.
135+
fullLayout.xaxis.clearCalc();
136+
fullLayout.yaxis.clearCalc();
126137

127138
var out = calc(gd, fullTrace);
128-
delete out.trace;
139+
out._xcategories = fullLayout.xaxis._categories;
140+
out._ycategories = fullLayout.yaxis._categories;
141+
129142
return out;
130143
}
131144

@@ -157,6 +170,64 @@ describe('Test histogram2d', function() {
157170
[0, 0, 0, 1]
158171
]);
159172
});
173+
174+
['histogram2d', 'histogram2dcontour'].forEach(function(traceType) {
175+
it('should sort z data based on axis categoryorder for ' + traceType, function() {
176+
var mock = require('@mocks/heatmap_categoryorder');
177+
var mockCopy = Lib.extendDeep({}, mock);
178+
var data = mockCopy.data[0];
179+
data.type = traceType;
180+
var layout = mockCopy.layout;
181+
182+
// sort x axis categories
183+
var mockLayout = Lib.extendDeep({}, layout);
184+
var out = _calc(data, mockLayout);
185+
mockLayout.xaxis.categoryorder = 'category ascending';
186+
var out1 = _calc(data, mockLayout);
187+
188+
expect(out._xcategories).toEqual(out1._xcategories.slice().reverse());
189+
// Check z data is also sorted
190+
for(var i = 0; i < out.z.length; i++) {
191+
expect(out1.z[i]).toEqual(out.z[i].slice().reverse());
192+
}
193+
194+
// sort y axis categories
195+
mockLayout = Lib.extendDeep({}, layout);
196+
out = _calc(data, mockLayout);
197+
mockLayout.yaxis.categoryorder = 'category ascending';
198+
out1 = _calc(data, mockLayout);
199+
200+
expect(out._ycategories).toEqual(out1._ycategories.slice().reverse());
201+
// Check z data is also sorted
202+
expect(out1.z).toEqual(out.z.slice().reverse());
203+
});
204+
205+
it('should sort z data based on axis categoryarray ' + traceType, function() {
206+
var mock = require('@mocks/heatmap_categoryorder');
207+
var mockCopy = Lib.extendDeep({}, mock);
208+
var data = mockCopy.data[0];
209+
data.type = traceType;
210+
var layout = mockCopy.layout;
211+
212+
layout.xaxis.categoryorder = 'array';
213+
layout.xaxis.categoryarray = ['x', 'z', 'y', 'w'];
214+
layout.yaxis.categoryorder = 'array';
215+
layout.yaxis.categoryarray = ['a', 'd', 'b', 'c'];
216+
217+
var out = _calc(data, layout);
218+
layout.xaxis.categoryorder = 'array';
219+
layout.xaxis.categoryarray = ['x', 'z', 'y', 'w'];
220+
layout.yaxis.categoryorder = 'array';
221+
layout.yaxis.categoryarray = ['a', 'd', 'b', 'c'];
222+
223+
expect(out._xcategories).toEqual(layout.xaxis.categoryarray, 'xaxis should reorder');
224+
expect(out._ycategories).toEqual(layout.yaxis.categoryarray, 'yaxis should reorder');
225+
var offset = 0;
226+
if(traceType === 'histogram2dcontour') offset = 1;
227+
expect(out.z[0 + offset][0 + offset]).toEqual(0);
228+
expect(out.z[0 + offset][3 + offset]).toEqual(1);
229+
});
230+
});
160231
});
161232

162233
describe('restyle / relayout interaction', function() {

0 commit comments

Comments
 (0)