forked from Autodesk/hubble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharts.js
215 lines (199 loc) · 8.96 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
/* global
aggregateTimeData,
createChordChart,
createHistoryChart,
createList,
createTable,
createSpinner,
*/
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();
});
it('should apply aggregation on single view and multiview charts equally', function()
{
spyOn(d3, 'tsv').and.callFake(function(url, rowHandler, callback)
{
callback(null, [
{date: 'Jan 1 2018', 'times was awesome': 3},
{date: 'Jan 2 2018', 'times was awesome': 13}
]);
return {on: function(evt, cb) { cb(); }};
});
let canvas = document.createElement('canvas');
createHistoryChart(canvas, {find: function() { return {click: function(){}}; }});
// TODO: add expectations on behaviour of createHistory chart.
});
});
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 = 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 = 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 = 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 = 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 = 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);
});
});
});