7510a52c9b073a1542e4ad13453daebccd4dcbb1
[dygraphs.git] / auto_tests / tests / axis_labels-deprecated.js
1 /**
2 * @fileoverview Test cases for how axis labels are chosen and formatted.
3 *
4 * @author dan@dygraphs.com (Dan Vanderkam)
5 */
6 var AxisLabelsTestCase = TestCase("axis-labels");
7
8 AxisLabelsTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 };
11
12 AxisLabelsTestCase.prototype.tearDown = function() {
13 };
14
15 function getYLabels() {
16 var y_labels = document.getElementsByClassName("dygraph-axis-label-y");
17 var ary = [];
18 for (var i = 0; i < y_labels.length; i++) {
19 ary.push(y_labels[i].innerHTML);
20 }
21 return ary;
22 }
23
24 function getXLabels() {
25 var x_labels = document.getElementsByClassName("dygraph-axis-label-x");
26 var ary = [];
27 for (var i = 0; i < x_labels.length; i++) {
28 ary.push(x_labels[i].innerHTML);
29 }
30 return ary;
31 }
32
33 function makeNumbers(ary) {
34 var ret = [];
35 for (var i = 0; i < ary.length; i++) {
36 ret.push(parseFloat(ary[i]));
37 }
38 return ret;
39 }
40
41 function getLegend() {
42 var legend = document.getElementsByClassName("dygraph-legend")[0];
43 return legend.textContent;
44 }
45
46 AxisLabelsTestCase.prototype.kCloseFloat = 1.0e-10;
47
48 AxisLabelsTestCase.prototype.testMinusOneToOne = function() {
49 var opts = {
50 width: 480,
51 height: 320
52 };
53 var data = "X,Y\n" +
54 "0,-1\n" +
55 "1,0\n" +
56 "2,1\n" +
57 "3,0\n"
58 ;
59
60 var graph = document.getElementById("graph");
61 var g = new Dygraph(graph, data, opts);
62
63 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
64 assertEquals(['-1','-0.5','0','0.5','1'], getYLabels());
65
66 // Go up to 2
67 data += "4,2\n";
68 g.updateOptions({file: data});
69 assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels());
70
71 // Now 10
72 data += "5,10\n";
73 g.updateOptions({file: data});
74 assertEquals(['-2','0','2','4','6','8','10'], getYLabels());
75
76 // Now 100
77 data += "6,100\n";
78 g.updateOptions({file: data});
79 assertEquals(['0','20','40','60','80','100'], getYLabels());
80
81 g.setSelection(0);
82 assertEquals('0: Y:-1', getLegend());
83 };
84
85 AxisLabelsTestCase.prototype.testSmallRangeNearZero = function() {
86 var opts = {
87 drawAxesAtZero: true,
88 width: 480,
89 height: 320
90 };
91 var data = "X,Y\n" +
92 "0,-1\n" +
93 "1,0\n" +
94 "2,1\n" +
95 "3,0\n"
96 ;
97 opts.valueRange = [-0.1, 0.1];
98
99 var graph = document.getElementById("graph");
100 var g = new Dygraph(graph, data, opts);
101 assertEqualsDelta(makeNumbers(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"]),
102 makeNumbers(getYLabels()), this.kCloseFloat);
103
104 opts.valueRange = [-0.05, 0.05];
105 g.updateOptions(opts);
106 // TODO(danvk): why '1.00e-2' and not '0.01'?
107 assertEquals(makeNumbers(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"]),
108 makeNumbers(getYLabels()));
109
110 opts.valueRange = [-0.01, 0.01];
111 g.updateOptions(opts);
112 assertEquals(makeNumbers(["-0.01","-8.00e-3","-6.00e-3","-4.00e-3","-2.00e-3","0","2.00e-3","4.00e-3","6.00e-3","8.00e-3"]), makeNumbers(getYLabels()));
113
114 g.setSelection(1);
115 assertEquals('1: Y:0', getLegend());
116 };
117
118 AxisLabelsTestCase.prototype.testSmallRangeAwayFromZero = function() {
119 var opts = {
120 width: 480,
121 height: 320
122 };
123 var data = "X,Y\n" +
124 "0,-1\n" +
125 "1,0\n" +
126 "2,1\n" +
127 "3,0\n"
128 ;
129 var graph = document.getElementById("graph");
130
131 opts.valueRange = [9.9, 10.1];
132 var g = new Dygraph(graph, data, opts);
133 assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels());
134
135 opts.valueRange = [9.99, 10.01];
136 g.updateOptions(opts);
137 // TODO(danvk): this is bad
138 assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels());
139
140 opts.valueRange = [9.999, 10.001];
141 g.updateOptions(opts);
142 // TODO(danvk): this is even worse!
143 assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels());
144
145 g.setSelection(1);
146 assertEquals('1: Y:0', getLegend());
147 };
148
149 AxisLabelsTestCase.prototype.testXAxisTimeLabelFormatter = function() {
150 var opts = {
151 width: 480,
152 height: 320
153 };
154 var data = [[5.0,0],[5.1,1],[5.2,2],[5.3,3],[5.4,4],[5.5,5],[5.6,6],[5.7,7],[5.8,8],[5.9,9]];
155 var graph = document.getElementById("graph");
156 var g = new Dygraph(graph, data, opts);
157 g.updateOptions({
158 xAxisLabelFormatter: function (totalMinutes) {
159 var hours = Math.floor( totalMinutes / 60);
160 var minutes = Math.floor((totalMinutes - (hours * 60)));
161 var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60));
162
163 if (hours < 10) hours = "0" + hours;
164 if (minutes < 10) minutes = "0" + minutes;
165 if (seconds < 10) seconds = "0" + seconds;
166
167 return hours + ':' + minutes + ':' + seconds;
168 }
169 });
170
171 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], getXLabels());
172
173 // The legend does not use the xAxisLabelFormatter:
174 g.setSelection(1);
175 assertEquals('5.1: Y1:1', getLegend());
176 };
177
178 AxisLabelsTestCase.prototype.testAxisLabelFormatter = function () {
179 var opts = {
180 width: 480,
181 height: 320,
182 xAxisLabelFormatter: function(x, granularity, opts, dg) {
183 assertEquals('number', typeof(x));
184 assertEquals('number', typeof(granularity));
185 assertEquals('function', typeof(opts));
186 assertEquals('[Dygraph graph]', dg.toString());
187 return 'x' + x;
188 },
189 yAxisLabelFormatter: function(y, granularity, opts, dg) {
190 assertEquals('number', typeof(y));
191 assertEquals('number', typeof(granularity));
192 assertEquals('function', typeof(opts));
193 assertEquals('[Dygraph graph]', dg.toString());
194 return 'y' + y;
195 },
196 labels: ['x', 'y']
197 };
198 var data = [];
199 for (var i = 0; i < 10; i++) {
200 data.push([i, 2 * i]);
201 }
202 var graph = document.getElementById("graph");
203 var g = new Dygraph(graph, data, opts);
204
205 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
206 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
207
208 g.setSelection(2);
209 assertEquals("2: y:4", getLegend());
210 };
211
212 AxisLabelsTestCase.prototype.testDateAxisLabelFormatter = function () {
213 var opts = {
214 width: 480,
215 height: 320,
216 xAxisLabelFormatter: function(x, granularity, opts, dg) {
217 assertTrue(Dygraph.isDateLike(x));
218 assertEquals('number', typeof(granularity));
219 assertEquals('function', typeof(opts));
220 assertEquals('[Dygraph graph]', dg.toString());
221 return 'x' + x.strftime('%Y/%m/%d');
222 },
223 yAxisLabelFormatter: function(y, granularity, opts, dg) {
224 assertEquals('number', typeof(y));
225 assertEquals('number', typeof(granularity));
226 assertEquals('function', typeof(opts));
227 assertEquals('[Dygraph graph]', dg.toString());
228 return 'y' + y;
229 },
230 labels: ['x', 'y']
231 };
232 var data = [];
233 for (var i = 1; i < 10; i++) {
234 data.push([new Date("2011/01/0" + i), 2 * i]);
235 }
236 var graph = document.getElementById("graph");
237 var g = new Dygraph(graph, data, opts);
238
239 assertEquals(["x2011/01/01", "x2011/01/02", "x2011/01/03", "x2011/01/04", "x2011/01/05", "x2011/01/06", "x2011/01/07", "x2011/01/08", "x2011/01/09"], getXLabels());
240 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
241
242 g.setSelection(0);
243 assertEquals("2011/01/01: y:2", getLegend());
244 };
245
246 // This test verifies that when a valueFormatter is set (but not an
247 // axisLabelFormatter), then the valueFormatter is used to format the axis
248 // labels.
249 AxisLabelsTestCase.prototype.testValueFormatter = function () {
250 var opts = {
251 width: 480,
252 height: 320,
253 xValueFormatter: function(x, opts, series_name, dg) {
254 assertEquals('number', typeof(x));
255 assertEquals('function', typeof(opts));
256 assertEquals('string', typeof(series_name));
257 assertEquals('[Dygraph graph]', dg.toString());
258 return 'x' + x;
259 },
260 yValueFormatter: function(y, opts, series_name, dg) {
261 assertEquals('number', typeof(y));
262 assertEquals('function', typeof(opts));
263 assertEquals('string', typeof(series_name));
264 assertEquals('[Dygraph graph]', dg.toString());
265 return 'y' + y;
266 },
267 labels: ['x', 'y']
268 };
269 var data = [];
270 for (var i = 0; i < 10; i++) {
271 data.push([i, 2 * i]);
272 }
273 var graph = document.getElementById("graph");
274 var g = new Dygraph(graph, data, opts);
275
276 // the valueFormatter options do not affect the ticks.
277 assertEquals(['0','2','4','6','8'], getXLabels());
278 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
279 getYLabels());
280
281 // they do affect the legend, however.
282 g.setSelection(2);
283 assertEquals("x2: y:y4", getLegend());
284 };
285
286 AxisLabelsTestCase.prototype.testDateValueFormatter = function () {
287 var opts = {
288 width: 480,
289 height: 320,
290 xValueFormatter: function(x, opts, series_name, dg) {
291 assertEquals('number', typeof(x));
292 assertEquals('function', typeof(opts));
293 assertEquals('string', typeof(series_name));
294 assertEquals('[Dygraph graph]', dg.toString());
295 return 'x' + new Date(x).strftime('%Y/%m/%d');
296 },
297 yValueFormatter: function(y, opts, series_name, dg) {
298 assertEquals('number', typeof(y));
299 assertEquals('function', typeof(opts));
300 assertEquals('string', typeof(series_name));
301 assertEquals('[Dygraph graph]', dg.toString());
302 return 'y' + y;
303 },
304 labels: ['x', 'y']
305 };
306
307 var data = [];
308 for (var i = 1; i < 10; i++) {
309 data.push([new Date("2011/01/0" + i), 2 * i]);
310 }
311 var graph = document.getElementById("graph");
312 var g = new Dygraph(graph, data, opts);
313
314 // valueFormatters do not affect ticks.
315 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], getXLabels());
316 assertEquals(['2','4','6','8','10','12','14','16','18'], getYLabels());
317
318 // the valueFormatter options also affect the legend.
319 g.setSelection(2);
320 assertEquals('x2011/01/03: y:y6', getLegend());
321 };
322
323 // This test verifies that when both a valueFormatter and an axisLabelFormatter
324 // are specified, the axisLabelFormatter takes precedence.
325 AxisLabelsTestCase.prototype.testAxisLabelFormatterPrecedence = function () {
326 var opts = {
327 width: 480,
328 height: 320,
329 xValueFormatter: function(x) {
330 return 'xvf' + x;
331 },
332 yValueFormatter: function(y) {
333 return 'yvf' + y;
334 },
335 xAxisLabelFormatter: function(x, granularity) {
336 return 'x' + x;
337 },
338 yAxisLabelFormatter: function(y) {
339 return 'y' + y;
340 },
341 labels: ['x', 'y']
342 };
343 var data = [];
344 for (var i = 0; i < 10; i++) {
345 data.push([i, 2 * i]);
346 }
347 var graph = document.getElementById("graph");
348 var g = new Dygraph(graph, data, opts);
349
350 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
351 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
352
353 g.setSelection(9);
354 assertEquals("xvf9: y:yvf18", getLegend());
355 };
356
357 // This is the same as the previous test, except that options are added
358 // one-by-one.
359 AxisLabelsTestCase.prototype.testAxisLabelFormatterIncremental = function () {
360 var opts = {
361 width: 480,
362 height: 320,
363 labels: ['x', 'y']
364 };
365 var data = [];
366 for (var i = 0; i < 10; i++) {
367 data.push([i, 2 * i]);
368 }
369 var graph = document.getElementById("graph");
370 var g = new Dygraph(graph, data, opts);
371 g.updateOptions({
372 xValueFormatter: function(x) {
373 return 'xvf' + x;
374 }
375 });
376 g.updateOptions({
377 yValueFormatter: function(y) {
378 return 'yvf' + y;
379 }
380 });
381 g.updateOptions({
382 xAxisLabelFormatter: function(x, granularity) {
383 return 'x' + x;
384 }
385 });
386 g.updateOptions({
387 yAxisLabelFormatter: function(y) {
388 return 'y' + y;
389 }
390 });
391
392 assertEquals(["x0","x2","x4","x6","x8"], getXLabels());
393 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
394
395 g.setSelection(9);
396 assertEquals("xvf9: y:yvf18", getLegend());
397 };
398
399 AxisLabelsTestCase.prototype.testGlobalFormatters = function() {
400 var opts = {
401 width: 480,
402 height: 320,
403 labels: ['x', 'y'],
404 valueFormatter: function(x) {
405 return 'vf' + x;
406 },
407 axisLabelFormatter: function(x) {
408 return 'alf' + x;
409 }
410 };
411 var data = [];
412 for (var i = 0; i < 10; i++) {
413 data.push([i, 2 * i]);
414 }
415 var graph = document.getElementById("graph");
416 var g = new Dygraph(graph, data, opts);
417
418 assertEquals(['alf0','alf2','alf4','alf6','alf8'], getXLabels());
419 assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], getYLabels());
420
421 g.setSelection(9);
422 assertEquals("vf9: y:vf18", getLegend());
423 };
424
425 AxisLabelsTestCase.prototype.testSeriesOrder = function() {
426 var opts = {
427 width: 480,
428 height: 320
429 };
430 var data = "x,00,01,10,11\n" +
431 "0,101,201,301,401\n" +
432 "1,102,202,302,402\n" +
433 "2,103,203,303,403\n" +
434 "3,104,204,304,404\n"
435 ;
436
437 var graph = document.getElementById("graph");
438 var g = new Dygraph(graph, data, opts);
439
440 g.setSelection(2);
441 assertEquals('2: 00:103 01:203 10:303 11:403', getLegend());
442
443 // Sanity checks for indexFromSetName
444 assertEquals(0, g.indexFromSetName("x"));
445 assertEquals(1, g.indexFromSetName("00"));
446 assertEquals(null, g.indexFromSetName("abcde"));
447
448 // Verify that we get the label list back in the right order
449 assertEquals(["x", "00", "01", "10", "11"], g.getLabels());
450 };