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
|
module('init');
test('MG properly detects time series vs. not.', function () {
var params1 = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
x_accessor: 'date'
};
var params2 = {
target: '#qunit-fixture',
data: [{'date': 5434, 'value': 12},
{'date': 5435, 'value': 18}],
x_accessor: 'date'
};
var params3 = {
target: '#qunit-fixture',
data: [[{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
[{'date': new Date('2014-11-01'), 'value': 32},
{'date': new Date('2014-11-02'), 'value': 43}]],
x_accessor: 'date'
};
mg_merge_args_with_defaults(params1);
mg_merge_args_with_defaults(params2);
mg_merge_args_with_defaults(params3);
mg_is_time_series(params1);
mg_is_time_series(params2);
mg_is_time_series(params3);
ok(params1.time_series === true, 'Date-accessed data set is a time series.');
ok(params2.time_series === false, 'Number-accessed data set is not a time series.');
ok(params3.time_series === true, 'Nested data set w/ dates detected as time series.');
});
test("Chart's width is set correctly on subsequent calls to existing chart", function () {
var params_0 = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
};
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
width: 200,
height: 100,
};
MG.data_graphic(params_0);
MG.data_graphic(params);
var width = document.querySelector(params.target + ' svg').clientWidth;
ok(width === 200, "SVG's width matches latest specified width");
});
test("Chart's width is set to parents if full_width: true", function () {
var params = {
target: '#qunit-fixture',
full_width: true,
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
height: 100
};
MG.data_graphic(params);
var svg_width = document.querySelector(params.target + ' svg').clientWidth;
var div_width = document.querySelector(params.target).clientWidth;
equal(div_width, svg_width, "SVG's width matches parent upon using full_width: true");
});
test("Chart's height is set to parents if full_height: true", function () {
var params = {
target: '#qunit-fixture',
full_height: true,
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
width: 500
};
document.querySelector(params.target).style.height = '500px';
MG.data_graphic(params);
var svg_height = document.querySelector(params.target + ' svg').clientHeight;
var div_height = document.querySelector(params.target).clientHeight;
equal(div_height, svg_height, "SVG's height matches parent upon using full_height: true");
});
test("Won't add SVG element if an SVG element already exists in parent.", function () {
var args1 = {
target: '#qunit-fixture div#exists',
width: 500,
height: 200,
linked: false,
svg: 'FLAG'
};
var qunit = document.querySelector('#qunit-fixture');
var div = document.createElement('div');
div.id = 'exists';
div.appendChild(document.createElement('svg'));
qunit.appendChild(div);
var first_number = document.querySelectorAll('svg').length;
mg_add_svg_if_it_doesnt_exist('', args1);
var second_number = document.querySelectorAll('svg').length;
equal(first_number, second_number, 'SVG element not added if it already exists.');
});
test("Chart's height is set correctly on subsequent calls to existing chart", function () {
var params_0 = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
};
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
width: 200,
height: 100,
};
MG.data_graphic(params_0);
MG.data_graphic(params);
var height = document.querySelector(params.target + ' svg').clientHeight;
ok(height == params.height, "SVG's height matches latest specified height");
});
test('Charts are plotted correctly when MG is called multiple times on the same target element', function () {
var params_0 = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}]
};
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
width: 200,
height: 100,
};
MG.data_graphic(params_0);
MG.data_graphic(params);
// ensure chart types change appropriately
var line = document.querySelector('.mg-main-line');
ok(line, 'chart_type is `line`, line chart is plotted');
// check all the other chart types
var chart_types = [{id: 'point', domElement: '.mg-points'},
{id: 'histogram', domElement: '.mg-histogram'},
{id: 'bar', domElement: '.mg-barplot'}];
for (var i = 0; i < chart_types.length; i++) {
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
chart_type: chart_types[i].id,
width: 200,
height: 100,
};
MG.data_graphic(params);
ok(document.querySelector(chart_types[i].domElement),
'chart_type switched to `' + chart_types[i].id + '`, the correct chart type is plotted');
// ensure old chart was removed
equal(document.querySelectorAll('.mg-main-line').length, 0, 'line chart (old one) was removed');
}
});
test('Missing chart has required class name set', function () {
expect(1);
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
chart_type: 'missing-data'
};
MG.data_graphic(params);
var matches = document.querySelector(params.target + ' svg').getAttribute('class').match(/mg-missing/);
ok(matches, 'Missing chart has class `missing` set');
});
test('Linked chart has the required class set', function () {
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-11-01'), 'value': 12},
{'date': new Date('2014-11-02'), 'value': 18}],
linked: true
};
MG.data_graphic(params);
var matches = document.querySelector(params.target + ' svg').getAttribute('class').match(/linked/);
ok(matches, 'Linked chart has class `linked` set');
});
test('args.time_series is set to true when data is time-series', function () {
var params = {
target: '#qunit-fixture',
data: [{'foo': new Date('2014-11-01'), 'value': 12},
{'foo': new Date('2014-11-02'), 'value': 18}],
x_accessor: 'foo'
};
MG.data_graphic(params);
ok(params.time_series, 'args.time_series is set to true when data is time-series');
});
test('args.time_series is set to false when data is not time-series', function () {
var params = {
target: '#qunit-fixture',
data: [{'foo': 100, 'value': 12},
{'foo': 200, 'value': 18}],
x_accessor: 'foo'
};
MG.data_graphic(params);
equal(params.time_series, false, 'args.time_series is set to false when data is not time-series');
});
test('Only one clip path is added on multiple calls to the same target element', function () {
var params = {
target: '#qunit-fixture',
data: [{'date': new Date('2014-01-01'), 'value': 12, 'l': 10, 'u': 14},
{'date': new Date('2014-03-01'), 'value': 18, 'l': 16, 'u': 20}]
};
MG.data_graphic(params);
MG.data_graphic(MG.clone(params));
equal(document.querySelectorAll('.mg-clip-path').length, 1, 'We only have one clip path');
});
|