Commit | Line | Data |
---|---|---|
6d6c60b6 RK |
1 | /** |
2 | * @fileoverview Test cases for how axis labels are chosen and formatted. | |
3 | * | |
4 | * @author dan@dygraphs.com (Dan Vanderkam) | |
5 | */ | |
89fdcedb | 6 | describe("axis-labels", function() { |
6d6c60b6 | 7 | |
89fdcedb | 8 | beforeEach(function() { |
6d6c60b6 | 9 | document.body.innerHTML = "<div id='graph'></div>"; |
89fdcedb | 10 | }); |
6d6c60b6 | 11 | |
89fdcedb DV |
12 | afterEach(function() { |
13 | }); | |
6d6c60b6 | 14 | |
319d0361 | 15 | var simpleData = |
d574a45e | 16 | "X,Y,Y2\n" + |
cb136039 RK |
17 | "0,-1,.5\n" + |
18 | "1,0,.7\n" + | |
19 | "2,1,.4\n" + | |
20 | "3,0,.98\n"; | |
6d6c60b6 | 21 | |
319d0361 | 22 | var kCloseFloat = 1.0e-10; |
6d6c60b6 | 23 | |
89fdcedb | 24 | it('testMinusOneToOne', function() { |
6d6c60b6 RK |
25 | var opts = { |
26 | width: 480, | |
27 | height: 320 | |
28 | }; | |
29 | var data = "X,Y\n" + | |
30 | "0,-1\n" + | |
31 | "1,0\n" + | |
32 | "2,1\n" + | |
33 | "3,0\n" | |
34 | ; | |
35 | ||
36 | var graph = document.getElementById("graph"); | |
37 | var g = new Dygraph(graph, data, opts); | |
38 | ||
39 | // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better? | |
89fdcedb | 40 | assert.deepEqual(['-1','-0.5','0','0.5','1'], Util.getYLabels()); |
6d6c60b6 RK |
41 | |
42 | // Go up to 2 | |
43 | data += "4,2\n"; | |
44 | g.updateOptions({file: data}); | |
89fdcedb | 45 | assert.deepEqual(['-1','-0.5','0','0.5','1','1.5','2'], Util.getYLabels()); |
6d6c60b6 RK |
46 | |
47 | // Now 10 | |
48 | data += "5,10\n"; | |
49 | g.updateOptions({file: data}); | |
89fdcedb | 50 | assert.deepEqual(['-2','0','2','4','6','8','10'], Util.getYLabels()); |
6d6c60b6 RK |
51 | |
52 | // Now 100 | |
53 | data += "6,100\n"; | |
54 | g.updateOptions({file: data}); | |
89fdcedb | 55 | assert.deepEqual(['0','20','40','60','80','100'], Util.getYLabels()); |
6d6c60b6 RK |
56 | |
57 | g.setSelection(0); | |
89fdcedb DV |
58 | assert.equal('0: Y: -1', Util.getLegend()); |
59 | }); | |
6d6c60b6 | 60 | |
89fdcedb | 61 | it('testSmallRangeNearZero', function() { |
6d6c60b6 RK |
62 | var opts = { |
63 | drawAxesAtZero: true, | |
64 | width: 480, | |
65 | height: 320 | |
66 | }; | |
67 | var data = "X,Y\n" + | |
68 | "0,-1\n" + | |
69 | "1,0\n" + | |
70 | "2,1\n" + | |
71 | "3,0\n" | |
72 | ; | |
73 | opts.valueRange = [-0.1, 0.1]; | |
74 | ||
75 | var graph = document.getElementById("graph"); | |
76 | var g = new Dygraph(graph, data, opts); | |
dc910fce | 77 | assertDeepCloseTo([-0.1,-0.05,0,0.05], |
319d0361 | 78 | Util.makeNumbers(Util.getYLabels()), kCloseFloat); |
6d6c60b6 RK |
79 | |
80 | opts.valueRange = [-0.05, 0.05]; | |
81 | g.updateOptions(opts); | |
89fdcedb | 82 | assert.deepEqual([-0.04,-0.02,0,0.02,0.04], |
dc910fce | 83 | Util.makeNumbers(Util.getYLabels())); |
6d6c60b6 RK |
84 | |
85 | opts.valueRange = [-0.01, 0.01]; | |
86 | g.updateOptions(opts); | |
89fdcedb | 87 | assert.deepEqual([-0.01,-0.005,0,0.005], |
dc910fce | 88 | Util.makeNumbers(Util.getYLabels())); |
6d6c60b6 RK |
89 | |
90 | g.setSelection(1); | |
89fdcedb DV |
91 | assert.equal('1: Y: 0', Util.getLegend()); |
92 | }); | |
6d6c60b6 | 93 | |
89fdcedb | 94 | it('testSmallRangeAwayFromZero', function() { |
6d6c60b6 RK |
95 | var opts = { |
96 | width: 480, | |
97 | height: 320 | |
98 | }; | |
99 | var data = "X,Y\n" + | |
100 | "0,-1\n" + | |
101 | "1,0\n" + | |
102 | "2,1\n" + | |
103 | "3,0\n" | |
104 | ; | |
105 | var graph = document.getElementById("graph"); | |
106 | ||
107 | opts.valueRange = [9.9, 10.1]; | |
108 | var g = new Dygraph(graph, data, opts); | |
89fdcedb | 109 | assert.deepEqual(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], Util.getYLabels()); |
6d6c60b6 RK |
110 | |
111 | opts.valueRange = [9.99, 10.01]; | |
112 | g.updateOptions(opts); | |
113 | // TODO(danvk): this is bad | |
89fdcedb | 114 | assert.deepEqual(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], Util.getYLabels()); |
6d6c60b6 RK |
115 | |
116 | opts.valueRange = [9.999, 10.001]; | |
117 | g.updateOptions(opts); | |
118 | // TODO(danvk): this is even worse! | |
89fdcedb | 119 | assert.deepEqual(["10","10","10","10"], Util.getYLabels()); |
6d6c60b6 RK |
120 | |
121 | g.setSelection(1); | |
89fdcedb DV |
122 | assert.equal('1: Y: 0', Util.getLegend()); |
123 | }); | |
6d6c60b6 | 124 | |
89fdcedb | 125 | it('testXAxisTimeLabelFormatter', function() { |
6d6c60b6 RK |
126 | var opts = { |
127 | width: 480, | |
65129ba8 DV |
128 | height: 320, |
129 | labels: ['X', 'Y1'] | |
6d6c60b6 RK |
130 | }; |
131 | 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]]; | |
132 | var graph = document.getElementById("graph"); | |
133 | var g = new Dygraph(graph, data, opts); | |
134 | g.updateOptions({ | |
135 | axes : { | |
136 | x : { | |
137 | axisLabelFormatter : function (totalMinutes) { | |
138 | var hours = Math.floor( totalMinutes / 60); | |
139 | var minutes = Math.floor((totalMinutes - (hours * 60))); | |
140 | var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60)); | |
141 | ||
142 | if (hours < 10) hours = "0" + hours; | |
143 | if (minutes < 10) minutes = "0" + minutes; | |
144 | if (seconds < 10) seconds = "0" + seconds; | |
145 | ||
146 | return hours + ':' + minutes + ':' + seconds; | |
147 | } | |
148 | } | |
149 | } | |
150 | }); | |
151 | ||
89fdcedb | 152 | assert.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util.getXLabels()); |
6d6c60b6 RK |
153 | |
154 | // The legend does not use the axisLabelFormatter: | |
155 | g.setSelection(1); | |
89fdcedb DV |
156 | assert.equal('5.1: Y1: 1', Util.getLegend()); |
157 | }); | |
6d6c60b6 | 158 | |
89fdcedb | 159 | it('testAxisLabelFormatter', function() { |
6d6c60b6 RK |
160 | var opts = { |
161 | width: 480, | |
162 | height: 320, | |
163 | axes : { | |
164 | x : { | |
165 | axisLabelFormatter : function(x, granularity, opts, dg) { | |
89fdcedb DV |
166 | assert.equal('number', typeof(x)); |
167 | assert.equal('number', typeof(granularity)); | |
168 | assert.equal('function', typeof(opts)); | |
169 | assert.equal('[Dygraph graph]', dg.toString()); | |
6d6c60b6 RK |
170 | return 'x' + x; |
171 | } | |
172 | }, | |
173 | y : { | |
174 | axisLabelFormatter : function(y, granularity, opts, dg) { | |
89fdcedb DV |
175 | assert.equal('number', typeof(y)); |
176 | assert.equal('number', typeof(granularity)); | |
177 | assert.equal('function', typeof(opts)); | |
178 | assert.equal('[Dygraph graph]', dg.toString()); | |
6d6c60b6 RK |
179 | return 'y' + y; |
180 | } | |
181 | } | |
182 | }, | |
183 | labels: ['x', 'y'] | |
184 | }; | |
185 | var data = []; | |
186 | for (var i = 0; i < 10; i++) { | |
187 | data.push([i, 2 * i]); | |
188 | } | |
189 | var graph = document.getElementById("graph"); | |
190 | var g = new Dygraph(graph, data, opts); | |
191 | ||
89fdcedb DV |
192 | assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels()); |
193 | assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); | |
6d6c60b6 RK |
194 | |
195 | g.setSelection(2); | |
89fdcedb DV |
196 | assert.equal("2: y: 4", Util.getLegend()); |
197 | }); | |
6d6c60b6 | 198 | |
89fdcedb | 199 | it('testDateAxisLabelFormatter', function() { |
6d6c60b6 RK |
200 | var opts = { |
201 | width: 480, | |
202 | height: 320, | |
203 | axes : { | |
204 | x : { | |
e0269a3d | 205 | pixelsPerLabel: 60, |
6d6c60b6 | 206 | axisLabelFormatter : function(x, granularity, opts, dg) { |
89fdcedb DV |
207 | assert.isTrue(Dygraph.isDateLike(x)); |
208 | assert.equal('number', typeof(granularity)); | |
209 | assert.equal('function', typeof(opts)); | |
210 | assert.equal('[Dygraph graph]', dg.toString()); | |
464b5f50 | 211 | return 'x' + Util.formatDate(x); |
6d6c60b6 RK |
212 | } |
213 | }, | |
214 | y : { | |
215 | axisLabelFormatter : function(y, granularity, opts, dg) { | |
89fdcedb DV |
216 | assert.equal('number', typeof(y)); |
217 | assert.equal('number', typeof(granularity)); | |
218 | assert.equal('function', typeof(opts)); | |
219 | assert.equal('[Dygraph graph]', dg.toString()); | |
6d6c60b6 RK |
220 | return 'y' + y; |
221 | } | |
222 | } | |
223 | }, | |
224 | labels: ['x', 'y'] | |
225 | }; | |
226 | var data = []; | |
227 | for (var i = 1; i < 10; i++) { | |
228 | data.push([new Date("2011/01/0" + i), 2 * i]); | |
229 | } | |
230 | var graph = document.getElementById("graph"); | |
231 | var g = new Dygraph(graph, data, opts); | |
232 | ||
89fdcedb DV |
233 | assert.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util.getXLabels()); |
234 | assert.deepEqual(["y5","y10","y15"], Util.getYLabels()); | |
6d6c60b6 RK |
235 | |
236 | g.setSelection(0); | |
89fdcedb DV |
237 | assert.equal("2011/01/01: y: 2", Util.getLegend()); |
238 | }); | |
6d6c60b6 RK |
239 | |
240 | // This test verifies that when a valueFormatter is set (but not an | |
241 | // axisLabelFormatter), then the valueFormatter is used to format the axis | |
242 | // labels. | |
89fdcedb | 243 | it('testValueFormatter', function() { |
6d6c60b6 RK |
244 | var opts = { |
245 | width: 480, | |
246 | height: 320, | |
6addd5b7 DV |
247 | axes: { |
248 | x: { | |
be7543ac | 249 | valueFormatter: function(x, opts, series_name, dg, row, col) { |
89fdcedb DV |
250 | assert.equal('number', typeof(x)); |
251 | assert.equal('function', typeof(opts)); | |
252 | assert.equal('string', typeof(series_name)); | |
253 | assert.equal('[Dygraph graph]', dg.toString()); | |
254 | assert.equal('number', typeof(row)); | |
255 | assert.equal('number', typeof(col)); | |
256 | assert.equal(dg, this); | |
6d6c60b6 RK |
257 | return 'x' + x; |
258 | } | |
259 | }, | |
6addd5b7 | 260 | y: { |
be7543ac | 261 | valueFormatter: function(y, opts, series_name, dg, row, col) { |
89fdcedb DV |
262 | assert.equal('number', typeof(y)); |
263 | assert.equal('function', typeof(opts)); | |
264 | assert.equal('string', typeof(series_name)); | |
265 | assert.equal('[Dygraph graph]', dg.toString()); | |
266 | assert.equal('number', typeof(row)); | |
267 | assert.equal('number', typeof(col)); | |
268 | assert.equal(dg, this); | |
6d6c60b6 RK |
269 | return 'y' + y; |
270 | } | |
271 | } | |
272 | }, | |
273 | labels: ['x', 'y'] | |
274 | }; | |
275 | var data = []; | |
276 | for (var i = 0; i < 10; i++) { | |
277 | data.push([i, 2 * i]); | |
278 | } | |
279 | var graph = document.getElementById("graph"); | |
280 | var g = new Dygraph(graph, data, opts); | |
281 | ||
282 | // the valueFormatter options do not affect the ticks. | |
89fdcedb DV |
283 | assert.deepEqual(['0','2','4','6','8'], Util.getXLabels()); |
284 | assert.deepEqual(["0","5","10","15"], | |
846038aa | 285 | Util.getYLabels()); |
6d6c60b6 RK |
286 | |
287 | // they do affect the legend, however. | |
288 | g.setSelection(2); | |
89fdcedb DV |
289 | assert.equal("x2: y: y4", Util.getLegend()); |
290 | }); | |
6d6c60b6 | 291 | |
89fdcedb | 292 | it('testDateValueFormatter', function() { |
6d6c60b6 RK |
293 | var opts = { |
294 | width: 480, | |
295 | height: 320, | |
296 | axes : { | |
297 | x : { | |
e0269a3d | 298 | pixelsPerLabel: 60, |
be7543ac | 299 | valueFormatter: function(x, opts, series_name, dg, row, col) { |
89fdcedb DV |
300 | assert.equal('number', typeof(x)); |
301 | assert.equal('function', typeof(opts)); | |
302 | assert.equal('string', typeof(series_name)); | |
303 | assert.equal('[Dygraph graph]', dg.toString()); | |
304 | assert.equal('number', typeof(row)); | |
305 | assert.equal('number', typeof(col)); | |
306 | assert.equal(dg, this); | |
464b5f50 | 307 | return 'x' + Util.formatDate(x); |
6d6c60b6 RK |
308 | } |
309 | }, | |
310 | y : { | |
be7543ac | 311 | valueFormatter: function(y, opts, series_name, dg, row, col) { |
89fdcedb DV |
312 | assert.equal('number', typeof(y)); |
313 | assert.equal('function', typeof(opts)); | |
314 | assert.equal('string', typeof(series_name)); | |
315 | assert.equal('[Dygraph graph]', dg.toString()); | |
316 | assert.equal('number', typeof(row)); | |
317 | assert.equal('number', typeof(col)); | |
318 | assert.equal(dg, this); | |
6d6c60b6 RK |
319 | return 'y' + y; |
320 | } | |
321 | } | |
322 | }, | |
323 | labels: ['x', 'y'] | |
324 | }; | |
325 | ||
326 | var data = []; | |
327 | for (var i = 1; i < 10; i++) { | |
328 | data.push([new Date("2011/01/0" + i), 2 * i]); | |
329 | } | |
330 | var graph = document.getElementById("graph"); | |
331 | var g = new Dygraph(graph, data, opts); | |
332 | ||
333 | // valueFormatters do not affect ticks. | |
89fdcedb DV |
334 | assert.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util.getXLabels()); |
335 | assert.deepEqual(["5","10","15"], Util.getYLabels()); | |
6d6c60b6 RK |
336 | |
337 | // the valueFormatter options also affect the legend. | |
338 | g.setSelection(2); | |
89fdcedb DV |
339 | assert.equal('x2011/01/03: y: y6', Util.getLegend()); |
340 | }); | |
6d6c60b6 RK |
341 | |
342 | // This test verifies that when both a valueFormatter and an axisLabelFormatter | |
343 | // are specified, the axisLabelFormatter takes precedence. | |
89fdcedb | 344 | it('testAxisLabelFormatterPrecedence', function() { |
6d6c60b6 RK |
345 | var opts = { |
346 | width: 480, | |
347 | height: 320, | |
348 | axes : { | |
349 | x : { | |
350 | valueFormatter: function(x) { | |
89fdcedb | 351 | assert.equal('[Dygraph graph]', this.toString()); |
6d6c60b6 RK |
352 | return 'xvf' + x; |
353 | }, | |
354 | axisLabelFormatter: function(x, granularity) { | |
89fdcedb | 355 | assert.equal('[Dygraph graph]', this.toString()); |
6d6c60b6 RK |
356 | return 'x' + x; |
357 | } | |
358 | }, | |
359 | y : { | |
360 | valueFormatter: function(y) { | |
89fdcedb | 361 | assert.equal('[Dygraph graph]', this.toString()); |
6d6c60b6 RK |
362 | return 'yvf' + y; |
363 | }, | |
364 | axisLabelFormatter: function(y) { | |
89fdcedb | 365 | assert.equal('[Dygraph graph]', this.toString()); |
6d6c60b6 RK |
366 | return 'y' + y; |
367 | } | |
368 | } | |
369 | }, | |
370 | labels: ['x', 'y'] | |
371 | }; | |
372 | var data = []; | |
373 | for (var i = 0; i < 10; i++) { | |
374 | data.push([i, 2 * i]); | |
375 | } | |
376 | var graph = document.getElementById("graph"); | |
377 | var g = new Dygraph(graph, data, opts); | |
378 | ||
89fdcedb DV |
379 | assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels()); |
380 | assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); | |
6d6c60b6 RK |
381 | |
382 | g.setSelection(9); | |
89fdcedb DV |
383 | assert.equal("xvf9: y: yvf18", Util.getLegend()); |
384 | }); | |
6d6c60b6 RK |
385 | |
386 | // This is the same as the previous test, except that options are added | |
387 | // one-by-one. | |
89fdcedb | 388 | it('testAxisLabelFormatterIncremental', function() { |
6d6c60b6 RK |
389 | var opts = { |
390 | width: 480, | |
391 | height: 320, | |
392 | labels: ['x', 'y'] | |
393 | }; | |
394 | var data = []; | |
395 | for (var i = 0; i < 10; i++) { | |
396 | data.push([i, 2 * i]); | |
397 | } | |
398 | var graph = document.getElementById("graph"); | |
399 | var g = new Dygraph(graph, data, opts); | |
400 | g.updateOptions({ | |
401 | axes : { | |
402 | x : { | |
403 | valueFormatter: function(x) { | |
404 | return 'xvf' + x; | |
405 | } | |
406 | } | |
407 | } | |
408 | }); | |
409 | g.updateOptions({ | |
410 | axes : { | |
411 | y : { | |
412 | valueFormatter: function(y) { | |
413 | return 'yvf' + y; | |
414 | } | |
415 | } | |
416 | } | |
417 | }); | |
418 | g.updateOptions({ | |
419 | axes : { | |
420 | x : { | |
421 | axisLabelFormatter: function(x, granularity) { | |
422 | return 'x' + x; | |
423 | } | |
424 | } | |
425 | } | |
426 | }); | |
427 | g.updateOptions({ | |
428 | axes : { | |
429 | y : { | |
430 | axisLabelFormatter: function(y) { | |
431 | return 'y' + y; | |
432 | } | |
433 | } | |
434 | } | |
435 | }); | |
436 | ||
89fdcedb DV |
437 | assert.deepEqual(["x0","x2","x4","x6","x8"], Util.getXLabels()); |
438 | assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); | |
6d6c60b6 RK |
439 | |
440 | g.setSelection(9); | |
89fdcedb DV |
441 | assert.equal("xvf9: y: yvf18", Util.getLegend()); |
442 | }); | |
6d6c60b6 | 443 | |
89fdcedb | 444 | it('testGlobalFormatters', function() { |
6d6c60b6 RK |
445 | var opts = { |
446 | width: 480, | |
447 | height: 320, | |
448 | labels: ['x', 'y'], | |
449 | valueFormatter: function(x) { | |
89fdcedb | 450 | assert.equal('[Dygraph graph]', this); |
6d6c60b6 RK |
451 | return 'vf' + x; |
452 | }, | |
453 | axisLabelFormatter: function(x) { | |
89fdcedb | 454 | assert.equal('[Dygraph graph]', this); |
6d6c60b6 RK |
455 | return 'alf' + x; |
456 | } | |
457 | }; | |
458 | var data = []; | |
459 | for (var i = 0; i < 10; i++) { | |
460 | data.push([i, 2 * i]); | |
461 | } | |
462 | var graph = document.getElementById("graph"); | |
463 | var g = new Dygraph(graph, data, opts); | |
464 | ||
89fdcedb DV |
465 | assert.deepEqual(['alf0','alf2','alf4','alf6','alf8'], Util.getXLabels()); |
466 | assert.deepEqual(["alf0","alf5","alf10","alf15"], Util.getYLabels()); | |
6d6c60b6 RK |
467 | |
468 | g.setSelection(9); | |
89fdcedb DV |
469 | assert.equal("vf9: y: vf18", Util.getLegend()); |
470 | }); | |
6d6c60b6 | 471 | |
89fdcedb | 472 | it('testValueFormatterParameters', function() { |
be7543ac DV |
473 | var calls = []; |
474 | // change any functions in list to 'fn' -- functions can't be asserted. | |
475 | var killFunctions = function(list) { | |
476 | var out = []; | |
477 | for (var i = 0; i < list.length; i++) { | |
478 | if (typeof(list[i]) == 'function') { | |
479 | out[i] = 'fn'; | |
480 | } else { | |
481 | out[i] = list[i]; | |
482 | } | |
483 | } | |
484 | return out; | |
485 | }; | |
486 | var taggedRecorder = function(tag) { | |
487 | return function() { | |
6addd5b7 | 488 | calls.push([tag].concat([this], killFunctions(arguments))); |
be7543ac DV |
489 | return ''; |
490 | } | |
491 | }; | |
492 | var opts = { | |
493 | axes: { | |
494 | x: { valueFormatter: taggedRecorder('x') }, | |
495 | y: { valueFormatter: taggedRecorder('y') }, | |
496 | y2: { valueFormatter: taggedRecorder('y2') } | |
497 | }, | |
498 | series: { | |
499 | 'y1': { axis: 'y1'}, | |
500 | 'y2': { axis: 'y2'} | |
501 | }, | |
502 | labels: ['x', 'y1', 'y2'] | |
503 | }; | |
504 | var data = [ | |
505 | [0, 1, 2], | |
506 | [1, 3, 4] | |
507 | ]; | |
508 | var graph = document.getElementById('graph'); | |
509 | var g = new Dygraph(graph, data, opts); | |
510 | ||
89fdcedb | 511 | assert.deepEqual([], calls); |
be7543ac | 512 | g.setSelection(0); |
89fdcedb | 513 | assert.deepEqual([ |
be7543ac | 514 | // num or millis, opts, series, dygraph, row, col |
6addd5b7 DV |
515 | [ 'x', g, 0, 'fn', 'x', g, 0, 0], |
516 | [ 'y', g, 1, 'fn', 'y1', g, 0, 1], | |
517 | ['y2', g, 2, 'fn', 'y2', g, 0, 2] | |
54f4c379 DV |
518 | ], calls); |
519 | ||
520 | calls = []; | |
521 | g.setSelection(1); | |
89fdcedb | 522 | assert.deepEqual([ |
6addd5b7 DV |
523 | [ 'x', g, 1, 'fn', 'x', g, 1, 0], |
524 | [ 'y', g, 3, 'fn', 'y1', g, 1, 1], | |
525 | ['y2', g, 4, 'fn', 'y2', g, 1, 2] | |
be7543ac | 526 | ], calls); |
89fdcedb | 527 | }); |
be7543ac | 528 | |
89fdcedb | 529 | it('testSeriesOrder', function() { |
6d6c60b6 RK |
530 | var opts = { |
531 | width: 480, | |
532 | height: 320 | |
533 | }; | |
534 | var data = "x,00,01,10,11\n" + | |
535 | "0,101,201,301,401\n" + | |
536 | "1,102,202,302,402\n" + | |
537 | "2,103,203,303,403\n" + | |
538 | "3,104,204,304,404\n" | |
539 | ; | |
540 | ||
541 | var graph = document.getElementById("graph"); | |
542 | var g = new Dygraph(graph, data, opts); | |
543 | ||
544 | g.setSelection(2); | |
89fdcedb | 545 | assert.equal('2: 00: 103 01: 203 10: 303 11: 403', Util.getLegend()); |
6d6c60b6 RK |
546 | |
547 | // Sanity checks for indexFromSetName | |
89fdcedb DV |
548 | assert.equal(0, g.indexFromSetName("x")); |
549 | assert.equal(1, g.indexFromSetName("00")); | |
550 | assert.equal(null, g.indexFromSetName("abcde")); | |
6d6c60b6 RK |
551 | |
552 | // Verify that we get the label list back in the right order | |
89fdcedb DV |
553 | assert.deepEqual(["x", "00", "01", "10", "11"], g.getLabels()); |
554 | }); | |
de624512 | 555 | |
89fdcedb | 556 | it('testLabelKMB', function() { |
de624512 RK |
557 | var data = []; |
558 | data.push([0,0]); | |
559 | data.push([1,2000]); | |
560 | data.push([2,1000]); | |
561 | ||
562 | var g = new Dygraph( | |
563 | document.getElementById("graph"), | |
564 | data, | |
565 | { | |
566 | labels: [ 'X', 'bar' ], | |
567 | axes : { | |
568 | y: { | |
569 | labelsKMB: true | |
570 | } | |
571 | } | |
572 | } | |
573 | ); | |
574 | ||
89fdcedb DV |
575 | assert.deepEqual(["0", "500", "1K", "1.5K", "2K"], Util.getYLabels()); |
576 | }); | |
de624512 | 577 | |
89fdcedb | 578 | it('testLabelKMG2', function() { |
de624512 RK |
579 | var data = []; |
580 | data.push([0,0]); | |
581 | data.push([1,2000]); | |
582 | data.push([2,1000]); | |
583 | ||
584 | var g = new Dygraph( | |
585 | document.getElementById("graph"), | |
586 | data, | |
587 | { | |
588 | labels: [ 'X', 'bar' ], | |
589 | axes : { | |
590 | y: { | |
591 | labelsKMG2: true | |
592 | } | |
593 | } | |
594 | } | |
595 | ); | |
596 | ||
dc910fce DV |
597 | assert.deepEqual(["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"], |
598 | Util.getYLabels()); | |
89fdcedb | 599 | }); |
b0d3471d | 600 | |
94647da2 | 601 | // Same as testLabelKMG2 but specifies the option at the |
a716aff2 | 602 | // top of the option dictionary. |
89fdcedb | 603 | it('testLabelKMG2_top', function() { |
a716aff2 RK |
604 | var data = []; |
605 | data.push([0,0]); | |
606 | data.push([1,2000]); | |
607 | data.push([2,1000]); | |
608 | ||
609 | var g = new Dygraph( | |
610 | document.getElementById("graph"), | |
611 | data, | |
612 | { | |
613 | labels: [ 'X', 'bar' ], | |
614 | labelsKMG2: true | |
615 | } | |
616 | ); | |
617 | ||
dc910fce | 618 | assert.deepEqual( |
a716aff2 RK |
619 | ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"], |
620 | Util.getYLabels()); | |
89fdcedb | 621 | }); |
a716aff2 | 622 | |
a681c4cb DV |
623 | it('testSmallLabelKMB', function() { |
624 | var data = []; | |
625 | data.push([0, 0]); | |
626 | data.push([1, 1e-6]); | |
627 | data.push([2, 2e-6]); | |
628 | ||
629 | var g = new Dygraph( | |
630 | document.getElementById("graph"), | |
631 | data, | |
632 | { | |
633 | labels: [ 'X', 'bar' ], | |
634 | axes : { | |
635 | y: { | |
636 | labelsKMB: true | |
637 | } | |
638 | } | |
639 | } | |
640 | ); | |
641 | ||
642 | // TODO(danvk): use prefixes here (e.g. m, µ, n) | |
643 | assert.deepEqual(['0', '5.00e-7', '1.00e-6', '1.50e-6', '2.00e-6'], | |
644 | Util.getYLabels()); | |
645 | }); | |
646 | ||
647 | it('testSmallLabelKMG2', function() { | |
648 | var data = []; | |
649 | data.push([0, 0]); | |
650 | data.push([1, 1e-6]); | |
651 | data.push([2, 2e-6]); | |
652 | ||
653 | var g = new Dygraph( | |
654 | document.getElementById("graph"), | |
655 | data, | |
656 | { | |
657 | labels: [ 'X', 'bar' ], | |
658 | axes : { | |
659 | y: { | |
660 | labelsKMG2: true | |
661 | } | |
662 | } | |
663 | } | |
664 | ); | |
665 | ||
666 | // TODO(danvk): this is strange--the values aren't on powers of two, and are | |
4f6b80ba | 667 | // these units really used for powers of two in <1? See issue #571. |
a681c4cb DV |
668 | assert.deepEqual(['0', '0.48u', '0.95u', '1.43u', '1.91u'], |
669 | Util.getYLabels()); | |
670 | }); | |
671 | ||
b0d3471d RK |
672 | /** |
673 | * Verify that log scale axis range is properly specified. | |
674 | */ | |
89fdcedb | 675 | it('testLogScale', function() { |
65129ba8 DV |
676 | var g = new Dygraph("graph", |
677 | [[0, 5], [1, 1000]], { | |
678 | logscale: true, | |
679 | labels: ['X', 'Y'] | |
680 | }); | |
b0d3471d | 681 | var nonEmptyLabels = Util.getYLabels().filter(function(x) { return x.length > 0; }); |
89fdcedb | 682 | assert.deepEqual(["5","10","20","50","100","200","500","1000"], nonEmptyLabels); |
b0d3471d RK |
683 | |
684 | g.updateOptions({ logscale : false }); | |
89fdcedb DV |
685 | assert.deepEqual(['0','200','400','600','800','1000'], Util.getYLabels()); |
686 | }); | |
478b866b RK |
687 | |
688 | /** | |
689 | * Verify that include zero range is properly specified. | |
690 | */ | |
89fdcedb | 691 | it('testIncludeZero', function() { |
65129ba8 DV |
692 | var g = new Dygraph("graph", |
693 | [[0, 500], [1, 1000]], { | |
694 | includeZero: true, | |
695 | labels: ['X', 'Y1'] | |
696 | }); | |
89fdcedb | 697 | assert.deepEqual(['0','200','400','600','800','1000'], Util.getYLabels()); |
478b866b RK |
698 | |
699 | g.updateOptions({ includeZero : false }); | |
89fdcedb DV |
700 | assert.deepEqual(['500','600','700','800','900','1000'], Util.getYLabels()); |
701 | }); | |
d574a45e | 702 | |
89fdcedb | 703 | it('testAxisLabelFontSize', function() { |
48dc3815 | 704 | var graph = document.getElementById("graph"); |
319d0361 | 705 | var g = new Dygraph(graph, simpleData, {}); |
48dc3815 RK |
706 | |
707 | // Be sure we're dealing with a 14-point default. | |
89fdcedb | 708 | assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); |
48dc3815 | 709 | |
3a84670d RK |
710 | var assertFontSize = function(selector, expected) { |
711 | Util.assertStyleOfChildren(selector, "font-size", expected); | |
712 | } | |
65129ba8 | 713 | |
dc910fce | 714 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "14px"); |
65129ba8 | 715 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "14px"); |
48dc3815 | 716 | |
65129ba8 | 717 | g.updateOptions({axisLabelFontSize : 8}); |
dc910fce DV |
718 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "8px"); |
719 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "8px"); | |
48dc3815 RK |
720 | |
721 | g.updateOptions({ | |
722 | axisLabelFontSize : null, | |
65129ba8 DV |
723 | axes: { |
724 | x: { axisLabelFontSize : 5 }, | |
48dc3815 RK |
725 | } |
726 | }); | |
727 | ||
dc910fce DV |
728 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "5px"); |
729 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "14px"); | |
48dc3815 RK |
730 | |
731 | g.updateOptions({ | |
65129ba8 DV |
732 | axes: { |
733 | y: { axisLabelFontSize : 20 }, | |
48dc3815 RK |
734 | } |
735 | }); | |
736 | ||
dc910fce DV |
737 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "5px"); |
738 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "20px"); | |
48dc3815 RK |
739 | |
740 | g.updateOptions({ | |
65129ba8 DV |
741 | series: { |
742 | Y2: { axis : "y2" } // copy y2 series to y2 axis. | |
48dc3815 | 743 | }, |
65129ba8 DV |
744 | axes: { |
745 | y2: { axisLabelFontSize : 12 }, | |
48dc3815 RK |
746 | } |
747 | }); | |
748 | ||
dc910fce DV |
749 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "5px"); |
750 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y1"), "20px"); | |
751 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y2"), "12px"); | |
89fdcedb | 752 | }); |
48dc3815 | 753 | |
89fdcedb | 754 | it('testAxisLabelFontSizeNull', function() { |
d574a45e | 755 | var graph = document.getElementById("graph"); |
319d0361 | 756 | var g = new Dygraph(graph, simpleData, |
d574a45e RK |
757 | { |
758 | axisLabelFontSize: null | |
759 | }); | |
760 | ||
3a84670d RK |
761 | var assertFontSize = function(selector, expected) { |
762 | Util.assertStyleOfChildren(selector, "font-size", expected); | |
5ab63877 | 763 | }; |
3a84670d RK |
764 | |
765 | // Be sure we're dealing with a 14-point default. | |
89fdcedb | 766 | assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); |
3a84670d | 767 | |
dc910fce DV |
768 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "14px"); |
769 | assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "14px"); | |
89fdcedb | 770 | }); |
3a84670d | 771 | |
89fdcedb | 772 | it('testAxisLabelColor', function() { |
3a84670d | 773 | var graph = document.getElementById("graph"); |
319d0361 | 774 | var g = new Dygraph(graph, simpleData, {}); |
3a84670d RK |
775 | |
776 | // Be sure we're dealing with a black default. | |
89fdcedb | 777 | assert.equal("black", Dygraph.DEFAULT_ATTRS.axisLabelColor); |
3a84670d RK |
778 | |
779 | var assertColor = function(selector, expected) { | |
780 | Util.assertStyleOfChildren(selector, "color", expected); | |
781 | } | |
782 | ||
dc910fce DV |
783 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)"); |
784 | assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)"); | |
3a84670d RK |
785 | |
786 | g.updateOptions({ axisLabelColor : "red"}); | |
dc910fce DV |
787 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(255, 0, 0)"); |
788 | assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(255, 0, 0)"); | |
3a84670d RK |
789 | |
790 | g.updateOptions({ | |
791 | axisLabelColor : null, | |
792 | axes : { | |
793 | x : { axisLabelColor : "blue" }, | |
794 | } | |
795 | }); | |
796 | ||
dc910fce DV |
797 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)"); |
798 | assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)"); | |
3a84670d RK |
799 | |
800 | g.updateOptions({ | |
801 | axes : { | |
802 | y : { axisLabelColor : "green" }, | |
803 | } | |
804 | }); | |
805 | ||
dc910fce DV |
806 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)"); |
807 | assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 128, 0)"); | |
3a84670d RK |
808 | |
809 | g.updateOptions({ | |
810 | series : { | |
811 | Y2 : { axis : "y2" } // copy y2 series to y2 axis. | |
812 | }, | |
813 | axes : { | |
814 | y2 : { axisLabelColor : "yellow" }, | |
815 | } | |
816 | }); | |
817 | ||
dc910fce DV |
818 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)"); |
819 | assertColor(document.querySelectorAll(".dygraph-axis-label-y1"), "rgb(0, 128, 0)"); | |
820 | assertColor(document.querySelectorAll(".dygraph-axis-label-y2"), "rgb(255, 255, 0)"); | |
89fdcedb | 821 | }); |
3a84670d | 822 | |
89fdcedb | 823 | it('testAxisLabelColorNull', function() { |
3a84670d | 824 | var graph = document.getElementById("graph"); |
319d0361 | 825 | var g = new Dygraph(graph, simpleData, |
3a84670d RK |
826 | { |
827 | axisLabelColor: null | |
828 | }); | |
829 | ||
830 | var assertColor = function(selector, expected) { | |
831 | Util.assertStyleOfChildren(selector, "color", expected); | |
832 | } | |
833 | ||
d574a45e | 834 | // Be sure we're dealing with a 14-point default. |
89fdcedb | 835 | assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); |
d574a45e | 836 | |
dc910fce DV |
837 | assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)"); |
838 | assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)"); | |
89fdcedb | 839 | }); |
2fd143d3 DV |
840 | |
841 | /* | |
842 | * This test shows that the label formatter overrides labelsKMB for all values. | |
843 | */ | |
89fdcedb | 844 | it('testLabelFormatterOverridesLabelsKMB', function() { |
2fd143d3 DV |
845 | var g = new Dygraph( |
846 | document.getElementById("graph"), | |
847 | "X,a,b\n" + | |
848 | "1,0,2000\n" + | |
849 | "2,500,1500\n" + | |
850 | "3,1000,1000\n" + | |
851 | "4,2000,0\n", { | |
852 | labelsKMB: true, | |
853 | axisLabelFormatter: function (v) { | |
854 | return v + ":X"; | |
855 | } | |
856 | }); | |
89fdcedb DV |
857 | assert.deepEqual(["0:X","500:X","1000:X","1500:X","2000:X"], Util.getYLabels()); |
858 | assert.deepEqual(["1:X","2:X","3:X"], Util.getXLabels()); | |
859 | }); | |
2fd143d3 DV |
860 | |
861 | /* | |
862 | * This test shows that you can override labelsKMB on the axis level. | |
863 | */ | |
89fdcedb | 864 | it('testLabelsKMBPerAxis', function() { |
c6975231 | 865 | var g = new Dygraph( |
2fd143d3 DV |
866 | document.getElementById("graph"), |
867 | "x,a,b\n" + | |
94647da2 RK |
868 | "1000,0,2000\n" + |
869 | "2000,500,1500\n" + | |
870 | "3000,1000,1000\n" + | |
871 | "4000,2000,0\n", { | |
872 | labelsKMB: false, | |
2fd143d3 | 873 | axes: { |
94647da2 RK |
874 | y2: { labelsKMB: true }, |
875 | x: { labelsKMB: true } | |
2fd143d3 DV |
876 | }, |
877 | series: { | |
d4cddcca | 878 | b: { axis: "y2" } |
2fd143d3 DV |
879 | } |
880 | }); | |
94647da2 RK |
881 | |
882 | // labelsKMB doesn't apply to the x axis. This value should be different. | |
bc262c40 | 883 | // BUG : https://code.google.com/p/dygraphs/issues/detail?id=488 |
89fdcedb | 884 | assert.deepEqual(["1000","2000","3000"], Util.getXLabels()); |
dc910fce | 885 | assert.deepEqual(["0","500","1000","1500","2000"], Util.getYLabels(1)); |
89fdcedb DV |
886 | assert.deepEqual(["0","500","1K","1.5K","2K"], Util.getYLabels(2)); |
887 | }); | |
c6975231 | 888 | |
ace524b5 | 889 | /* |
94647da2 RK |
890 | * This test shows that you can override labelsKMG2 on the axis level. |
891 | */ | |
89fdcedb | 892 | it('testLabelsKMBG2IPerAxis', function() { |
94647da2 RK |
893 | var g = new Dygraph( |
894 | document.getElementById("graph"), | |
895 | "x,a,b\n" + | |
896 | "1000,0,2000\n" + | |
897 | "2000,500,1500\n" + | |
898 | "3000,1000,1000\n" + | |
899 | "4000,2000,0\n", { | |
900 | labelsKMG2: false, | |
901 | axes: { | |
902 | y2: { labelsKMG2: true }, | |
e0269a3d | 903 | x: { labelsKMG2: true, pixelsPerLabel: 60 } |
94647da2 RK |
904 | }, |
905 | series: { | |
d4cddcca | 906 | b: { axis: "y2" } |
94647da2 RK |
907 | } |
908 | }); | |
909 | ||
910 | // It is weird that labelsKMG2 does something on the x axis but KMB does not. | |
911 | // Plus I can't be sure they're doing the same thing as they're done in different | |
912 | // bits of code. | |
bc262c40 | 913 | // BUG : https://code.google.com/p/dygraphs/issues/detail?id=488 |
89fdcedb | 914 | assert.deepEqual(["1024","2048","3072"], Util.getXLabels()); |
dc910fce | 915 | assert.deepEqual(["0","500","1000","1500","2000"], Util.getYLabels(1)); |
89fdcedb DV |
916 | assert.deepEqual(["0","500","1000","1.46k","1.95k"], Util.getYLabels(2)); |
917 | }); | |
94647da2 RK |
918 | |
919 | /** | |
920 | * This test shows you can override sigFigs on the axis level. | |
921 | */ | |
89fdcedb | 922 | it('testSigFigsPerAxis', function() { |
94647da2 RK |
923 | var g = new Dygraph( |
924 | document.getElementById("graph"), | |
925 | "x,a,b\n" + | |
926 | "1000,0,2000\n" + | |
927 | "2000,500,1500\n" + | |
928 | "3000,1000,1000\n" + | |
929 | "4000,2000,0\n", { | |
930 | sigFigs: 2, | |
931 | axes: { | |
932 | y2: { sigFigs: 6 }, | |
933 | x: { sigFigs: 8 } | |
934 | }, | |
935 | series: { | |
d4cddcca | 936 | b: { axis: "y2" } |
94647da2 RK |
937 | } |
938 | ||
939 | }); | |
940 | // sigFigs doesn't apply to the x axis. This value should be different. | |
bc262c40 | 941 | // BUG : https://code.google.com/p/dygraphs/issues/detail?id=488 |
89fdcedb DV |
942 | assert.deepEqual(["1000","2000","3000"], Util.getXLabels()); |
943 | assert.deepEqual(["0.0","5.0e+2","1.0e+3","1.5e+3","2.0e+3"], Util.getYLabels(1)); | |
944 | assert.deepEqual(["0.00000","500.000","1000.00","1500.00","2000.00"], Util.getYLabels(2)); | |
945 | }); | |
94647da2 RK |
946 | |
947 | /** | |
948 | * This test shows you can override digitsAfterDecimal on the axis level. | |
949 | */ | |
89fdcedb | 950 | it('testDigitsAfterDecimalPerAxis', function() { |
94647da2 RK |
951 | var g = new Dygraph( |
952 | document.getElementById("graph"), | |
953 | "x,a,b\n" + | |
954 | "0.006,0.001,0.008\n" + | |
955 | "0.007,0.002,0.007\n" + | |
956 | "0.008,0.003,0.006\n" + | |
957 | "0.009,0.004,0.005\n", { | |
958 | digitsAfterDecimal: 1, | |
959 | series: { | |
d4cddcca | 960 | b: { axis: "y2" } |
94647da2 RK |
961 | } |
962 | ||
963 | }); | |
964 | ||
965 | g.updateOptions({ axes: { y: { digitsAfterDecimal: 3 }}}); | |
89fdcedb | 966 | assert.deepEqual(["0.001","0.002","0.002","0.003","0.003","0.004","0.004"], Util.getYLabels(1)); |
94647da2 | 967 | g.updateOptions({ axes: { y: { digitsAfterDecimal: 4 }}}); |
89fdcedb | 968 | assert.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util.getYLabels(1)); |
94647da2 | 969 | g.updateOptions({ axes: { y: { digitsAfterDecimal: 5 }}}); |
89fdcedb | 970 | assert.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util.getYLabels(1)); |
94647da2 | 971 | g.updateOptions({ axes: { y: { digitsAfterDecimal: null }}}); |
89fdcedb | 972 | assert.deepEqual(["1e-3","2e-3","2e-3","3e-3","3e-3","4e-3","4e-3"], Util.getYLabels(1)); |
94647da2 RK |
973 | |
974 | g.updateOptions({ axes: { y2: { digitsAfterDecimal: 3 }}}); | |
89fdcedb | 975 | assert.deepEqual(["0.005","0.006","0.006","0.007","0.007","0.008","0.008"], Util.getYLabels(2)); |
94647da2 | 976 | g.updateOptions({ axes: { y2: { digitsAfterDecimal: 4 }}}); |
89fdcedb | 977 | assert.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util.getYLabels(2)); |
94647da2 | 978 | g.updateOptions({ axes: { y2: { digitsAfterDecimal: 5 }}}); |
89fdcedb | 979 | assert.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util.getYLabels(2)); |
94647da2 | 980 | g.updateOptions({ axes: { y2: { digitsAfterDecimal: null }}}); |
89fdcedb | 981 | assert.deepEqual(["5e-3","6e-3","6e-3","7e-3","7e-3","7e-3","8e-3"], Util.getYLabels(2)); |
94647da2 RK |
982 | |
983 | ||
984 | // digitsAfterDecimal is ignored for the x-axis. | |
bc262c40 | 985 | // BUG : https://code.google.com/p/dygraphs/issues/detail?id=488 |
94647da2 | 986 | g.updateOptions({ axes: { x: { digitsAfterDecimal: 3 }}}); |
89fdcedb | 987 | assert.deepEqual(["0.006","0.007","0.008"], Util.getXLabels()); |
94647da2 | 988 | g.updateOptions({ axes: { x: { digitsAfterDecimal: 4 }}}); |
89fdcedb | 989 | assert.deepEqual(["0.006","0.007","0.008"], Util.getXLabels()); |
94647da2 | 990 | g.updateOptions({ axes: { x: { digitsAfterDecimal: 5 }}}); |
89fdcedb | 991 | assert.deepEqual(["0.006","0.007","0.008"], Util.getXLabels()); |
94647da2 | 992 | g.updateOptions({ axes: { x: { digitsAfterDecimal: null }}}); |
89fdcedb DV |
993 | assert.deepEqual(["0.006","0.007","0.008"], Util.getXLabels()); |
994 | }); | |
94647da2 RK |
995 | |
996 | /** | |
997 | * This test shows you can override digitsAfterDecimal on the axis level. | |
998 | */ | |
89fdcedb | 999 | it('testMaxNumberWidthPerAxis', function() { |
94647da2 RK |
1000 | var g = new Dygraph( |
1001 | document.getElementById("graph"), | |
1002 | "x,a,b\n" + | |
1003 | "12401,12601,12804\n" + | |
1004 | "12402,12602,12803\n" + | |
1005 | "12403,12603,12802\n" + | |
1006 | "12404,12604,12801\n", { | |
1007 | maxNumberWidth: 1, | |
1008 | series: { | |
d4cddcca | 1009 | b: { axis: "y2" } |
94647da2 RK |
1010 | } |
1011 | }); | |
d4cddcca | 1012 | |
94647da2 | 1013 | g.updateOptions({ axes: { y: { maxNumberWidth: 4 }}}); |
89fdcedb | 1014 | assert.deepEqual(["1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4"] , Util.getYLabels(1)); |
94647da2 | 1015 | g.updateOptions({ axes: { y: { maxNumberWidth: 5 }}}); |
89fdcedb | 1016 | assert.deepEqual(["12601","12601.5","12602","12602.5","12603","12603.5","12604"] , Util.getYLabels(1)); |
94647da2 | 1017 | g.updateOptions({ axes: { y: { maxNumberWidth: null }}}); |
89fdcedb | 1018 | assert.deepEqual(["1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4"] , Util.getYLabels(1)); |
94647da2 RK |
1019 | |
1020 | g.updateOptions({ axes: { y2: { maxNumberWidth: 4 }}}); | |
89fdcedb | 1021 | assert.deepEqual(["1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4"], Util.getYLabels(2)); |
94647da2 | 1022 | g.updateOptions({ axes: { y2: { maxNumberWidth: 5 }}}); |
89fdcedb | 1023 | assert.deepEqual(["12801","12801.5","12802","12802.5","12803","12803.5","12804"], Util.getYLabels(2)); |
94647da2 | 1024 | g.updateOptions({ axes: { y2: { maxNumberWidth: null }}}); |
89fdcedb | 1025 | assert.deepEqual(["1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4"], Util.getYLabels(2)); |
94647da2 RK |
1026 | |
1027 | // maxNumberWidth is ignored for the x-axis. | |
bc262c40 | 1028 | // BUG : https://code.google.com/p/dygraphs/issues/detail?id=488 |
94647da2 | 1029 | g.updateOptions({ axes: { x: { maxNumberWidth: 4 }}}); |
89fdcedb | 1030 | assert.deepEqual(["12401","12402","12403"], Util.getXLabels()); |
94647da2 | 1031 | g.updateOptions({ axes: { x: { maxNumberWidth: 5 }}}); |
89fdcedb | 1032 | assert.deepEqual(["12401","12402","12403"], Util.getXLabels()); |
94647da2 | 1033 | g.updateOptions({ axes: { x: { maxNumberWidth: null }}}); |
89fdcedb DV |
1034 | assert.deepEqual(["12401","12402","12403"], Util.getXLabels()); |
1035 | }); | |
94647da2 RK |
1036 | |
1037 | /* | |
c6975231 DV |
1038 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=147 |
1039 | // Checks that axis labels stay sane across a DST change. | |
89fdcedb | 1040 | it('testLabelsCrossDstChange', function() { |
c6975231 DV |
1041 | // (From tests/daylight-savings.html) |
1042 | var g = new Dygraph( | |
1043 | document.getElementById("graph"), | |
1044 | "Date/Time,Purchases\n" + | |
1045 | "2010-11-05 00:00:00,167082\n" + | |
1046 | "2010-11-06 00:00:00,168571\n" + | |
1047 | "2010-11-07 00:00:00,177796\n" + | |
1048 | "2010-11-08 00:00:00,165587\n" + | |
1049 | "2010-11-09 00:00:00,164380\n", | |
1050 | { width: 1024 } | |
1051 | ); | |
1052 | ||
1053 | // Dates and "nice" hours: 6AM/PM and noon, not 5AM/11AM/... | |
1054 | var okLabels = { | |
1055 | '05Nov': true, | |
1056 | '06Nov': true, | |
1057 | '07Nov': true, | |
1058 | '08Nov': true, | |
1059 | '09Nov': true, | |
1060 | '06:00': true, | |
1061 | '12:00': true, | |
1062 | '18:00': true | |
1063 | }; | |
1064 | ||
1065 | var xLabels = Util.getXLabels(); | |
1066 | for (var i = 0; i < xLabels.length; i++) { | |
89fdcedb | 1067 | assert.isTrue(okLabels[xLabels[i]]); |
c6975231 | 1068 | } |
0f9bf369 DV |
1069 | |
1070 | // This range had issues of its own on tests/daylight-savings.html. | |
1071 | g.updateOptions({ | |
1072 | dateWindow: [1289109997722.8127, 1289261208937.7659] | |
1073 | }); | |
1074 | xLabels = Util.getXLabels(); | |
1075 | for (var i = 0; i < xLabels.length; i++) { | |
89fdcedb | 1076 | assert.isTrue(okLabels[xLabels[i]]); |
0f9bf369 | 1077 | } |
89fdcedb | 1078 | }); |
0f9bf369 DV |
1079 | |
1080 | ||
1081 | // Tests data which crosses a "fall back" at a high enough frequency that you | |
1082 | // can see both 1:00 A.M.s. | |
89fdcedb | 1083 | it('testLabelsCrossDstChangeHighFreq', function() { |
0f9bf369 DV |
1084 | // Generate data which crosses the EST/EDT boundary. |
1085 | var dst_data = []; | |
1086 | var base_ms = 1383454200000; | |
1087 | for (var x = base_ms; x < base_ms + 1000 * 60 * 80; x += 1000) { | |
1088 | dst_data.push([new Date(x), x]); | |
1089 | } | |
1090 | ||
1091 | var g = new Dygraph( | |
1092 | document.getElementById("graph"), | |
1093 | dst_data, | |
1094 | { width: 1024, labels: ['Date', 'Value'] } | |
1095 | ); | |
1096 | ||
89fdcedb | 1097 | assert.deepEqual([ |
0f9bf369 DV |
1098 | '00:50', '00:55', |
1099 | '01:00', '01:05', '01:10', '01:15', '01:20', '01:25', | |
1100 | '01:30', '01:35', '01:40', '01:45', '01:50', '01:55', | |
1101 | '01:00', '01:05' // 1 AM number two! | |
1102 | ], Util.getXLabels()); | |
1103 | ||
1104 | // Now zoom past the initial 1 AM. This used to cause trouble. | |
1105 | g.updateOptions({ | |
1106 | dateWindow: [1383454200000 + 15*60*1000, g.xAxisExtremes()[1]]} | |
1107 | ); | |
89fdcedb | 1108 | assert.deepEqual([ |
0f9bf369 DV |
1109 | '01:05', '01:10', '01:15', '01:20', '01:25', |
1110 | '01:30', '01:35', '01:40', '01:45', '01:50', '01:55', | |
1111 | '01:00', '01:05' // 1 AM number two! | |
1112 | ], Util.getXLabels()); | |
89fdcedb | 1113 | }); |
fb0c64b0 DV |
1114 | |
1115 | ||
1116 | // Tests data which crosses a "spring forward" at a low frequency. | |
1117 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=433 | |
89fdcedb | 1118 | it('testLabelsCrossSpringForward', function() { |
fb0c64b0 DV |
1119 | var g = new Dygraph( |
1120 | document.getElementById("graph"), | |
1121 | "Date/Time,Purchases\n" + | |
1122 | "2011-03-11 00:00:00,167082\n" + | |
1123 | "2011-03-12 00:00:00,168571\n" + | |
1124 | "2011-03-13 00:00:00,177796\n" + | |
1125 | "2011-03-14 00:00:00,165587\n" + | |
1126 | "2011-03-15 00:00:00,164380\n", | |
1127 | { | |
1128 | width: 1024, | |
1129 | dateWindow: [1299989043119.4365, 1300080693627.4866] | |
1130 | }); | |
1131 | ||
1132 | var okLabels = { | |
1133 | '13Mar': true, | |
1134 | // '02:00': true, // not a real time! | |
1135 | '04:00': true, | |
1136 | '06:00': true, | |
1137 | '08:00': true, | |
1138 | '10:00': true, | |
1139 | '12:00': true, | |
1140 | '14:00': true, | |
1141 | '16:00': true, | |
1142 | '18:00': true, | |
1143 | '20:00': true, | |
1144 | '22:00': true, | |
1145 | '14Mar': true | |
1146 | }; | |
1147 | ||
1148 | var xLabels = Util.getXLabels(); | |
1149 | for (var i = 0; i < xLabels.length; i++) { | |
89fdcedb | 1150 | assert.isTrue(okLabels[xLabels[i]]); |
fb0c64b0 | 1151 | } |
89fdcedb | 1152 | }); |
fb0c64b0 | 1153 | |
89fdcedb | 1154 | it('testLabelsCrossSpringForwardHighFreq', function() { |
fb0c64b0 DV |
1155 | var base_ms_spring = 1299999000000; |
1156 | var dst_data_spring = []; | |
1157 | for (var x = base_ms_spring; x < base_ms_spring + 1000 * 60 * 80; x += 1000) { | |
1158 | dst_data_spring.push([new Date(x), x]); | |
1159 | } | |
1160 | ||
1161 | var g = new Dygraph( | |
1162 | document.getElementById("graph"), | |
1163 | dst_data_spring, | |
1164 | { width: 1024, labels: ['Date', 'Value'] } | |
1165 | ); | |
1166 | ||
89fdcedb | 1167 | assert.deepEqual([ |
fb0c64b0 DV |
1168 | '01:50', '01:55', |
1169 | '03:00', '03:05', '03:10', '03:15', '03:20', '03:25', | |
1170 | '03:30', '03:35', '03:40', '03:45', '03:50', '03:55', | |
1171 | '04:00', '04:05' | |
1172 | ], Util.getXLabels()); | |
89fdcedb | 1173 | }); |
ace524b5 | 1174 | */ |
89fdcedb DV |
1175 | |
1176 | }); |