-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhistogram2d_test.js
234 lines (195 loc) · 9.04 KB
/
histogram2d_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var Plotly = require('@lib/index');
var Plots = require('@src/plots/plots');
var Lib = require('@src/lib');
var supplyDefaults = require('@src/traces/histogram2d/defaults');
var calc = require('@src/traces/histogram2d/calc');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var fail = require('../assets/fail_test');
describe('Test histogram2d', function() {
'use strict';
describe('supplyDefaults', function() {
var traceIn,
traceOut;
beforeEach(function() {
traceOut = {};
});
it('should set zsmooth to false when zsmooth is empty', function() {
traceIn = {};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.zsmooth).toBe(false);
});
it('doesnt step on zsmooth when zsmooth is set', function() {
traceIn = {
zsmooth: 'fast'
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.zsmooth).toBe('fast');
});
it('should set xgap and ygap to 0 when xgap and ygap are empty', function() {
traceIn = {};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.xgap).toBe(0);
expect(traceOut.ygap).toBe(0);
});
it('shouldnt step on xgap and ygap when xgap and ygap are set', function() {
traceIn = {
xgap: 10,
ygap: 5
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.xgap).toBe(10);
expect(traceOut.ygap).toBe(5);
});
it('shouldnt coerce gap when zsmooth is set', function() {
traceIn = {
xgap: 10,
ygap: 5,
zsmooth: 'best'
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.xgap).toBe(undefined);
expect(traceOut.ygap).toBe(undefined);
});
it('should inherit layout.calendar', function() {
traceIn = {
x: [1, 2, 3],
y: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, '', {calendar: 'islamic'});
// we always fill calendar attributes, because it's hard to tell if
// we're on a date axis at this point.
expect(traceOut.xcalendar).toBe('islamic');
expect(traceOut.ycalendar).toBe('islamic');
});
it('should take its own calendars', function() {
traceIn = {
x: [1, 2, 3],
y: [1, 2, 3],
xcalendar: 'coptic',
ycalendar: 'ethiopian'
};
supplyDefaults(traceIn, traceOut, '', {calendar: 'islamic'});
// we always fill calendar attributes, because it's hard to tell if
// we're on a date axis at this point.
expect(traceOut.xcalendar).toBe('coptic');
expect(traceOut.ycalendar).toBe('ethiopian');
});
});
describe('calc', function() {
function _calc(opts) {
var base = { type: 'histogram2d' },
trace = Lib.extendFlat({}, base, opts),
gd = { data: [trace] };
Plots.supplyDefaults(gd);
var fullTrace = gd._fullData[0];
var out = calc(gd, fullTrace);
delete out.trace;
return out;
}
// remove tzJan/tzJuly when we move to UTC
var oneDay = 24 * 3600000;
it('should handle both uniform and nonuniform date bins', function() {
var out = _calc({
x: ['1970-01-01', '1970-01-01', '1970-01-02', '1970-01-04'],
nbinsx: 4,
y: ['1970-01-01', '1970-01-01', '1971-01-01', '1973-01-01'],
nbinsy: 4
});
expect(out.x0).toBe('1970-01-01');
expect(out.dx).toBe(oneDay);
// TODO: even though the binning is done on non-uniform bins,
// the display makes them linear (using only y0 and dy)
// Can we also make it display the bins with nonuniform size?
// see https://github.com/plotly/plotly.js/issues/360
expect(out.y0).toBe('1970-01-01 03:00');
expect(out.dy).toBe(365.25 * oneDay);
expect(out.z).toEqual([
[2, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]
]);
});
});
describe('restyle / relayout interaction', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should update paths on zooms', function(done) {
Plotly.newPlot(gd, [{
type: 'histogram2dcontour',
x: [1, 1, 2, 2, 3],
y: [0, 1, 1, 1, 3]
}])
.then(function() {
return Plotly.relayout(gd, 'xaxis.range', [0, 2]);
})
.catch(fail)
.then(done);
});
it('handles autobin correctly on restyles', function() {
var x1 = [
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];
var y1 = [
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
Plotly.newPlot(gd, [{type: 'histogram2d', x: x1, y: y1}]);
expect(gd._fullData[0].xbins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].ybins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].autobinx).toBe(true);
expect(gd._fullData[0].autobiny).toBe(true);
// same range but fewer samples increases sizes
Plotly.restyle(gd, {x: [[1, 3, 4]], y: [[1, 2, 4]]});
expect(gd._fullData[0].xbins).toEqual({start: -0.5, end: 5.5, size: 2, _count: 3});
expect(gd._fullData[0].ybins).toEqual({start: -0.5, end: 5.5, size: 2, _count: 3});
expect(gd._fullData[0].autobinx).toBe(true);
expect(gd._fullData[0].autobiny).toBe(true);
// larger range
Plotly.restyle(gd, {x: [[10, 30, 40]], y: [[10, 20, 40]]});
expect(gd._fullData[0].xbins).toEqual({start: -0.5, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].ybins).toEqual({start: -0.5, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].autobinx).toBe(true);
expect(gd._fullData[0].autobiny).toBe(true);
// explicit changes to bin settings
Plotly.restyle(gd, 'xbins.start', 12);
expect(gd._fullData[0].xbins).toEqual({start: 12, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].ybins).toEqual({start: -0.5, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].autobinx).toBe(false);
expect(gd._fullData[0].autobiny).toBe(true);
Plotly.restyle(gd, {'ybins.end': 12, 'ybins.size': 3});
expect(gd._fullData[0].xbins).toEqual({start: 12, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].ybins).toEqual({start: -0.5, end: 12, size: 3, _count: 3});
expect(gd._fullData[0].autobinx).toBe(false);
expect(gd._fullData[0].autobiny).toBe(false);
// restart autobin
Plotly.restyle(gd, {autobinx: true, autobiny: true});
expect(gd._fullData[0].xbins).toEqual({start: -0.5, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].ybins).toEqual({start: -0.5, end: 59.5, size: 20, _count: 3});
expect(gd._fullData[0].autobinx).toBe(true);
expect(gd._fullData[0].autobiny).toBe(true);
});
it('respects explicit autobin: false as a one-time autobin', function() {
var x1 = [
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];
var y1 = [
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
Plotly.newPlot(gd, [{type: 'histogram2d', x: x1, y: y1, autobinx: false, autobiny: false}]);
expect(gd._fullData[0].xbins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].ybins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].autobinx).toBe(false);
expect(gd._fullData[0].autobiny).toBe(false);
// with autobin false this will no longer update the bins.
Plotly.restyle(gd, {x: [[1, 3, 4]], y: [[1, 2, 4]]});
expect(gd._fullData[0].xbins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].ybins).toEqual({start: 0.5, end: 4.5, size: 1, _count: 4});
expect(gd._fullData[0].autobinx).toBe(false);
expect(gd._fullData[0].autobiny).toBe(false);
});
});
});