-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcharts.js
267 lines (241 loc) · 11.6 KB
/
charts.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* global
aggregateTimeData,
createChordChart,
createHistoryChart,
createList,
createTable,
createSpinner,
formatDate,
formatDateRange,
*/
describe('global charts.js', function()
{
describe('createChordChart function', function()
{
it('should exist', function()
{
expect(createChordChart).toBeDefined();
});
});
describe('createHistoryChart function', function()
{
it('should exist', function()
{
expect(createHistoryChart).toBeDefined();
});
});
describe('createList function', function()
{
it('should exist', function()
{
expect(createList).toBeDefined();
});
});
describe('createTable function', function()
{
it('should exist', function()
{
expect(createTable).toBeDefined();
});
});
describe('createSpinner function', function()
{
let canvas;
beforeEach(function()
{
canvas = $('<div id=\'test\'></div>');
$('body').append(canvas);
});
afterEach(function()
{
canvas.remove();
});
it('should exist', function()
{
expect(createSpinner).toBeDefined();
});
it('should return an object with a stop method', function()
{
let spinner = createSpinner(canvas[0]);
expect(spinner.stop).toBeDefined();
spinner.stop();
});
describe('stop method', function()
{
it('should destroy the spinner container after execution', function()
{
let spinner = createSpinner(canvas[0]);
spinner.stop();
expect($('.spinner-container').length).toEqual(0);
});
});
});
describe('aggregation for time series', function()
{
// Generate data from startDate to endDate (both inclusive) with a generator functor
function generateData(startDate, endDate, generator)
{
let dates = d3.utcDay.range(d3.isoParse(startDate), d3.utcDay.offset(d3.isoParse(endDate), 1));
let data = Array();
for (let i = 0; i < dates.length; i++)
data.push({'date': dates[i], 'value': generator(i)});
return data;
}
// Integer range generator
function integerRangeGenerator(start, modulo)
{
if (modulo)
return (i => (start + i) % modulo);
return (i => start + i);
}
const dateToString = d3.utcFormat('%Y-%m-%d');
it('should aggregate over weeks correctly', function()
{
const aggregationConfig = {'period': 'week', 'method': 'max'};
const generator = integerRangeGenerator(0, 28);
// 2018-01-01 is a Monday, and 2018-09-30 is a Sunday
const data = generateData('2018-01-01', '2018-09-30', generator);
const aggregatedData = aggregateTimeData(data, aggregationConfig);
expect(aggregatedData.length).toEqual(39);
expect(dateToString(aggregatedData[0]['date'])).toEqual('2018-01-01');
expect(dateToString(aggregatedData[1]['date'])).toEqual('2018-01-08');
expect(dateToString(aggregatedData[2]['date'])).toEqual('2018-01-15');
expect(dateToString(aggregatedData[37]['date'])).toEqual('2018-09-17');
expect(dateToString(aggregatedData[38]['date'])).toEqual('2018-09-24');
expect(aggregatedData[0]['value']).toEqual(6);
expect(aggregatedData[1]['value']).toEqual(13);
expect(aggregatedData[2]['value']).toEqual(20);
expect(aggregatedData[4]['value']).toEqual(6);
expect(aggregatedData[5]['value']).toEqual(13);
expect(aggregatedData[36]['value']).toEqual(6);
expect(aggregatedData[37]['value']).toEqual(13);
expect(aggregatedData[38]['value']).toEqual(20);
});
it('should not have off-by-one errors (1)', function()
{
const aggregationConfig = {'period': 'week', 'method': 'max'};
const generator = integerRangeGenerator(27, 28);
// 2017-12-31 is a Sunday, and 2018-10-01 is a Monday
const data = generateData('2017-12-31', '2018-10-01', generator);
const aggregatedData = aggregateTimeData(data, aggregationConfig);
expect(aggregatedData.length).toEqual(39);
expect(dateToString(aggregatedData[0]['date'])).toEqual('2018-01-01');
expect(dateToString(aggregatedData[1]['date'])).toEqual('2018-01-08');
expect(dateToString(aggregatedData[2]['date'])).toEqual('2018-01-15');
expect(dateToString(aggregatedData[37]['date'])).toEqual('2018-09-17');
expect(dateToString(aggregatedData[38]['date'])).toEqual('2018-09-24');
expect(aggregatedData[0]['value']).toEqual(6);
expect(aggregatedData[1]['value']).toEqual(13);
expect(aggregatedData[2]['value']).toEqual(20);
expect(aggregatedData[4]['value']).toEqual(6);
expect(aggregatedData[5]['value']).toEqual(13);
expect(aggregatedData[36]['value']).toEqual(6);
expect(aggregatedData[37]['value']).toEqual(13);
expect(aggregatedData[38]['value']).toEqual(20);
});
it('should not have off-by-one errors (2)', function()
{
const aggregationConfig = {'period': 'week', 'method': 'max'};
const generator = integerRangeGenerator(1, 28);
// 2018-01-02 is a Tuesday, and 2018-09-29 is a Saturday
const data = generateData('2018-01-02', '2018-09-29', generator);
const aggregatedData = aggregateTimeData(data, aggregationConfig);
expect(aggregatedData.length).toEqual(37);
expect(dateToString(aggregatedData[0]['date'])).toEqual('2018-01-08');
expect(dateToString(aggregatedData[1]['date'])).toEqual('2018-01-15');
expect(dateToString(aggregatedData[35]['date'])).toEqual('2018-09-10');
expect(dateToString(aggregatedData[36]['date'])).toEqual('2018-09-17');
expect(aggregatedData[0]['value']).toEqual(13);
expect(aggregatedData[1]['value']).toEqual(20);
expect(aggregatedData[3]['value']).toEqual(6);
expect(aggregatedData[4]['value']).toEqual(13);
expect(aggregatedData[35]['value']).toEqual(6);
expect(aggregatedData[36]['value']).toEqual(13);
});
it('should aggregate sums correctly', function()
{
const aggregationConfig = {'period': 'week', 'method': 'sum'};
const generator = integerRangeGenerator(0, 10);
// 2018-01-01 is a Monday, and 2018-09-30 is a Sunday
const data = generateData('2018-01-01', '2018-09-30', generator);
const aggregatedData = aggregateTimeData(data, aggregationConfig);
expect(aggregatedData.length).toEqual(39);
expect(aggregatedData[0]['value']).toEqual(21);
expect(aggregatedData[1]['value']).toEqual(30);
expect(aggregatedData[2]['value']).toEqual(39);
expect(aggregatedData[36]['value']).toEqual(35);
expect(aggregatedData[37]['value']).toEqual(24);
expect(aggregatedData[38]['value']).toEqual(33);
});
it('should aggregate over months correctly', function()
{
const aggregationConfig = {'period': 'month', 'method': 'first'};
const generator = integerRangeGenerator(9, 10);
const data = generateData('2017-12-31', '2019-01-01', generator);
const aggregatedData = aggregateTimeData(data, aggregationConfig);
expect(aggregatedData.length).toEqual(12);
expect(dateToString(aggregatedData[0]['date'])).toEqual('2018-01-01');
expect(dateToString(aggregatedData[1]['date'])).toEqual('2018-02-01');
expect(dateToString(aggregatedData[10]['date'])).toEqual('2018-11-01');
expect(dateToString(aggregatedData[11]['date'])).toEqual('2018-12-01');
expect(aggregatedData[0]['value']).toEqual(0);
expect(aggregatedData[1]['value']).toEqual(1);
expect(aggregatedData[10]['value']).toEqual(4);
expect(aggregatedData[11]['value']).toEqual(4);
});
it('should format dates correctly', function()
{
// The local time zone should not affect the displayed dates, which are to be interpreted as UTC
const testCases =
[
{localTimeZone: 'Etc/GMT-1', date: '2018-12-31T00:00:00.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT-1', date: '2018-12-31T23:59:59.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT-1', date: '2019-01-01T00:30:00.000Z', expectedResult: '1 Jan 2019'},
{localTimeZone: 'Etc/GMT', date: '2018-12-31T00:00:00.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT', date: '2018-12-31T23:59:59.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT', date: '2019-01-01T00:30:00.000Z', expectedResult: '1 Jan 2019'},
{localTimeZone: 'Etc/GMT+1', date: '2018-12-31T00:00:00.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT+1', date: '2018-12-31T23:59:59.000Z', expectedResult: '31 Dec 2018'},
{localTimeZone: 'Etc/GMT+1', date: '2019-01-01T00:30:00.000Z', expectedResult: '1 Jan 2019'}
];
for (let i = 0; i < testCases.length; i++)
{
moment.tz.setDefault(testCases[i].localTimeZone);
expect(formatDate(testCases[i].date)).toEqual(testCases[i].expectedResult);
}
});
it('should format date ranges correctly', function()
{
// The local time zone should not affect the displayed dates, which are to be interpreted as UTC
// Ranges are exclusive of the end date and should handle this correctly
const testCases =
[
{localTimeZone: 'Etc/GMT-1', dateRange: ['2018-06-01T00:00:00.000Z', '2018-06-08T00:00:00.000Z'],
expectedResult: '1 Jun 2018 to 7 Jun 2018'},
{localTimeZone: 'Etc/GMT', dateRange: ['2018-06-01T00:00:00.000Z', '2018-06-08T00:00:00.000Z'],
expectedResult: '1 Jun 2018 to 7 Jun 2018'},
{localTimeZone: 'Etc/GMT+1', dateRange: ['2018-06-01T00:00:00.000Z', '2018-06-08T00:00:00.000Z'],
expectedResult: '1 Jun 2018 to 7 Jun 2018'}
];
for (let i = 0; i < testCases.length; i++)
{
moment.tz.setDefault(testCases[i].localTimeZone);
expect(formatDateRange(testCases[i].dateRange)).toEqual(testCases[i].expectedResult);
}
});
it('formatting date ranges repeatedly should not modify the data', function()
{
moment.tz.setDefault('Etc/GMT');
const date1 = new Date('2018-06-01T00:00:00.000Z');
const date2 = new Date('2018-06-08T00:00:00.000Z');
const date3 = new Date('2018-06-15T00:00:00.000Z');
const dateRange1 = [date1, date2];
const dateRange2 = [date2, date3];
for (let i = 0; i < 20; i++)
{
expect(formatDateRange(dateRange1)).toEqual('1 Jun 2018 to 7 Jun 2018');
expect(formatDateRange(dateRange2)).toEqual('8 Jun 2018 to 14 Jun 2018');
}
});
});
});