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