2 * @fileoverview Test cases for how axis labels are chosen and formatted.
4 * @author dan@dygraphs.com (Dan Vanderkam)
6 describe("axis-labels", function() {
8 beforeEach(function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 afterEach(function() {
22 var kCloseFloat
= 1.0e-10;
24 it('testMinusOneToOne', function() {
36 var graph
= document
.getElementById("graph");
37 var g
= new Dygraph(graph
, data
, opts
);
39 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
40 assert
.deepEqual(['-1','-0.5','0','0.5','1'], Util
.getYLabels());
44 g
.updateOptions({file
: data
});
45 assert
.deepEqual(['-1','-0.5','0','0.5','1','1.5','2'], Util
.getYLabels());
49 g
.updateOptions({file
: data
});
50 assert
.deepEqual(['-2','0','2','4','6','8','10'], Util
.getYLabels());
54 g
.updateOptions({file
: data
});
55 assert
.deepEqual(['0','20','40','60','80','100'], Util
.getYLabels());
58 assert
.equal('0: Y: -1', Util
.getLegend());
61 it('testSmallRangeNearZero', function() {
73 opts
.valueRange
= [-0.1, 0.1];
75 var graph
= document
.getElementById("graph");
76 var g
= new Dygraph(graph
, data
, opts
);
77 assertDeepCloseTo([-0.1,-0.05,0,0.05],
78 Util
.makeNumbers(Util
.getYLabels()), kCloseFloat
);
80 opts
.valueRange
= [-0.05, 0.05];
81 g
.updateOptions(opts
);
82 assert
.deepEqual([-0.04,-0.02,0,0.02,0.04],
83 Util
.makeNumbers(Util
.getYLabels()));
85 opts
.valueRange
= [-0.01, 0.01];
86 g
.updateOptions(opts
);
87 assert
.deepEqual([-0.01,-0.005,0,0.005],
88 Util
.makeNumbers(Util
.getYLabels()));
91 assert
.equal('1: Y: 0', Util
.getLegend());
94 it('testSmallRangeAwayFromZero', function() {
105 var graph
= document
.getElementById("graph");
107 opts
.valueRange
= [9.9, 10.1];
108 var g
= new Dygraph(graph
, data
, opts
);
109 assert
.deepEqual(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], Util
.getYLabels());
111 opts
.valueRange
= [9.99, 10.01];
112 g
.updateOptions(opts
);
113 // TODO(danvk): this is bad
114 assert
.deepEqual(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], Util
.getYLabels());
116 opts
.valueRange
= [9.999, 10.001];
117 g
.updateOptions(opts
);
118 // TODO(danvk): this is even worse!
119 assert
.deepEqual(["10","10","10","10"], Util
.getYLabels());
122 assert
.equal('1: Y: 0', Util
.getLegend());
125 it('testXAxisTimeLabelFormatter', function() {
130 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]];
131 var graph
= document
.getElementById("graph");
132 var g
= new Dygraph(graph
, data
, opts
);
136 axisLabelFormatter
: function (totalMinutes
) {
137 var hours
= Math
.floor( totalMinutes
/ 60);
138 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
139 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
141 if (hours
< 10) hours
= "0" + hours
;
142 if (minutes
< 10) minutes
= "0" + minutes
;
143 if (seconds
< 10) seconds
= "0" + seconds
;
145 return hours
+ ':' + minutes
+ ':' + seconds
;
151 assert
.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
153 // The legend does not use the axisLabelFormatter:
155 assert
.equal('5.1: Y1: 1', Util
.getLegend());
158 it('testAxisLabelFormatter', function() {
164 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
165 assert
.equal('number', typeof(x
));
166 assert
.equal('number', typeof(granularity
));
167 assert
.equal('function', typeof(opts
));
168 assert
.equal('[Dygraph graph]', dg
.toString());
173 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
174 assert
.equal('number', typeof(y
));
175 assert
.equal('number', typeof(granularity
));
176 assert
.equal('function', typeof(opts
));
177 assert
.equal('[Dygraph graph]', dg
.toString());
185 for (var i
= 0; i
< 10; i
++) {
186 data
.push([i
, 2 * i
]);
188 var graph
= document
.getElementById("graph");
189 var g
= new Dygraph(graph
, data
, opts
);
191 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
192 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
195 assert
.equal("2: y: 4", Util
.getLegend());
198 it('testDateAxisLabelFormatter', function() {
205 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
206 assert
.isTrue(Dygraph
.isDateLike(x
));
207 assert
.equal('number', typeof(granularity
));
208 assert
.equal('function', typeof(opts
));
209 assert
.equal('[Dygraph graph]', dg
.toString());
210 return 'x' + Util
.formatDate(x
);
214 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
215 assert
.equal('number', typeof(y
));
216 assert
.equal('number', typeof(granularity
));
217 assert
.equal('function', typeof(opts
));
218 assert
.equal('[Dygraph graph]', dg
.toString());
226 for (var i
= 1; i
< 10; i
++) {
227 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
229 var graph
= document
.getElementById("graph");
230 var g
= new Dygraph(graph
, data
, opts
);
232 assert
.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util
.getXLabels());
233 assert
.deepEqual(["y5","y10","y15"], Util
.getYLabels());
236 assert
.equal("2011/01/01: y: 2", Util
.getLegend());
239 // This test verifies that when a valueFormatter is set (but not an
240 // axisLabelFormatter), then the valueFormatter is used to format the axis
242 it('testValueFormatter', function() {
248 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
249 assert
.equal('number', typeof(x
));
250 assert
.equal('function', typeof(opts
));
251 assert
.equal('string', typeof(series_name
));
252 assert
.equal('[Dygraph graph]', dg
.toString());
253 assert
.equal('number', typeof(row
));
254 assert
.equal('number', typeof(col
));
255 assert
.equal(dg
, this);
260 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
261 assert
.equal('number', typeof(y
));
262 assert
.equal('function', typeof(opts
));
263 assert
.equal('string', typeof(series_name
));
264 assert
.equal('[Dygraph graph]', dg
.toString());
265 assert
.equal('number', typeof(row
));
266 assert
.equal('number', typeof(col
));
267 assert
.equal(dg
, this);
275 for (var i
= 0; i
< 10; i
++) {
276 data
.push([i
, 2 * i
]);
278 var graph
= document
.getElementById("graph");
279 var g
= new Dygraph(graph
, data
, opts
);
281 // the valueFormatter options do not affect the ticks.
282 assert
.deepEqual(['0','2','4','6','8'], Util
.getXLabels());
283 assert
.deepEqual(["0","5","10","15"],
286 // they do affect the legend, however.
288 assert
.equal("x2: y: y4", Util
.getLegend());
291 it('testDateValueFormatter', function() {
298 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
299 assert
.equal('number', typeof(x
));
300 assert
.equal('function', typeof(opts
));
301 assert
.equal('string', typeof(series_name
));
302 assert
.equal('[Dygraph graph]', dg
.toString());
303 assert
.equal('number', typeof(row
));
304 assert
.equal('number', typeof(col
));
305 assert
.equal(dg
, this);
306 return 'x' + Util
.formatDate(x
);
310 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
311 assert
.equal('number', typeof(y
));
312 assert
.equal('function', typeof(opts
));
313 assert
.equal('string', typeof(series_name
));
314 assert
.equal('[Dygraph graph]', dg
.toString());
315 assert
.equal('number', typeof(row
));
316 assert
.equal('number', typeof(col
));
317 assert
.equal(dg
, this);
326 for (var i
= 1; i
< 10; i
++) {
327 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
329 var graph
= document
.getElementById("graph");
330 var g
= new Dygraph(graph
, data
, opts
);
332 // valueFormatters do not affect ticks.
333 assert
.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
334 assert
.deepEqual(["5","10","15"], Util
.getYLabels());
336 // the valueFormatter options also affect the legend.
338 assert
.equal('x2011/01/03: y: y6', Util
.getLegend());
341 // This test verifies that when both a valueFormatter and an axisLabelFormatter
342 // are specified, the axisLabelFormatter takes precedence.
343 it('testAxisLabelFormatterPrecedence', function() {
349 valueFormatter
: function(x
) {
350 assert
.equal('[Dygraph graph]', this.toString());
353 axisLabelFormatter
: function(x
, granularity
) {
354 assert
.equal('[Dygraph graph]', this.toString());
359 valueFormatter
: function(y
) {
360 assert
.equal('[Dygraph graph]', this.toString());
363 axisLabelFormatter
: function(y
) {
364 assert
.equal('[Dygraph graph]', this.toString());
372 for (var i
= 0; i
< 10; i
++) {
373 data
.push([i
, 2 * i
]);
375 var graph
= document
.getElementById("graph");
376 var g
= new Dygraph(graph
, data
, opts
);
378 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
379 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
382 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
385 // This is the same as the previous test, except that options are added
387 it('testAxisLabelFormatterIncremental', function() {
394 for (var i
= 0; i
< 10; i
++) {
395 data
.push([i
, 2 * i
]);
397 var graph
= document
.getElementById("graph");
398 var g
= new Dygraph(graph
, data
, opts
);
402 valueFormatter
: function(x
) {
411 valueFormatter
: function(y
) {
420 axisLabelFormatter
: function(x
, granularity
) {
429 axisLabelFormatter
: function(y
) {
436 assert
.deepEqual(["x0","x2","x4","x6","x8"], Util
.getXLabels());
437 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
440 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
443 it('testGlobalFormatters', function() {
448 valueFormatter
: function(x
) {
449 assert
.equal('[Dygraph graph]', this);
452 axisLabelFormatter
: function(x
) {
453 assert
.equal('[Dygraph graph]', this);
458 for (var i
= 0; i
< 10; i
++) {
459 data
.push([i
, 2 * i
]);
461 var graph
= document
.getElementById("graph");
462 var g
= new Dygraph(graph
, data
, opts
);
464 assert
.deepEqual(['alf0','alf2','alf4','alf6','alf8'], Util
.getXLabels());
465 assert
.deepEqual(["alf0","alf5","alf10","alf15"], Util
.getYLabels());
468 assert
.equal("vf9: y: vf18", Util
.getLegend());
471 it('testValueFormatterParameters', function() {
473 // change any functions in list to 'fn' -- functions can't be asserted.
474 var killFunctions
= function(list
) {
476 for (var i
= 0; i
< list
.length
; i
++) {
477 if (typeof(list
[i
]) == 'function') {
485 var taggedRecorder
= function(tag
) {
487 calls
.push([tag
].concat([this], killFunctions(arguments
)));
493 x
: { valueFormatter
: taggedRecorder('x') },
494 y
: { valueFormatter
: taggedRecorder('y') },
495 y2
: { valueFormatter
: taggedRecorder('y2') }
501 labels
: ['x', 'y1', 'y2']
507 var graph
= document
.getElementById('graph');
508 var g
= new Dygraph(graph
, data
, opts
);
510 assert
.deepEqual([], calls
);
513 // num or millis, opts, series, dygraph, row, col
514 [ 'x', g
, 0, 'fn', 'x', g
, 0, 0],
515 [ 'y', g
, 1, 'fn', 'y1', g
, 0, 1],
516 ['y2', g
, 2, 'fn', 'y2', g
, 0, 2]
522 [ 'x', g
, 1, 'fn', 'x', g
, 1, 0],
523 [ 'y', g
, 3, 'fn', 'y1', g
, 1, 1],
524 ['y2', g
, 4, 'fn', 'y2', g
, 1, 2]
528 it('testSeriesOrder', function() {
533 var data
= "x,00,01,10,11\n" +
534 "0,101,201,301,401\n" +
535 "1,102,202,302,402\n" +
536 "2,103,203,303,403\n" +
537 "3,104,204,304,404\n"
540 var graph
= document
.getElementById("graph");
541 var g
= new Dygraph(graph
, data
, opts
);
544 assert
.equal('2: 00: 103 01: 203 10: 303 11: 403', Util
.getLegend());
546 // Sanity checks for indexFromSetName
547 assert
.equal(0, g
.indexFromSetName("x"));
548 assert
.equal(1, g
.indexFromSetName("00"));
549 assert
.equal(null, g
.indexFromSetName("abcde"));
551 // Verify that we get the label list back in the right order
552 assert
.deepEqual(["x", "00", "01", "10", "11"], g
.getLabels());
555 it('testLabelKMB', function() {
562 document
.getElementById("graph"),
565 labels
: [ 'X', 'bar' ],
574 assert
.deepEqual(["0", "500", "1K", "1.5K", "2K"], Util
.getYLabels());
577 it('testLabelKMG2', function() {
584 document
.getElementById("graph"),
587 labels
: [ 'X', 'bar' ],
596 assert
.deepEqual(["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
600 // Same as testLabelKMG2 but specifies the option at the
601 // top of the option dictionary.
602 it('testLabelKMG2_top', function() {
609 document
.getElementById("graph"),
612 labels
: [ 'X', 'bar' ],
618 ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
623 * Verify that log scale axis range is properly specified.
625 it('testLogScale', function() {
626 var g
= new Dygraph("graph", [[0, 5], [1, 1000]], { logscale
: true });
627 var nonEmptyLabels
= Util
.getYLabels().filter(function(x
) { return x
.length
> 0; });
628 assert
.deepEqual(["5","10","20","50","100","200","500","1000"], nonEmptyLabels
);
630 g
.updateOptions({ logscale
: false });
631 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
635 * Verify that include zero range is properly specified.
637 it('testIncludeZero', function() {
638 var g
= new Dygraph("graph", [[0, 500], [1, 1000]], { includeZero
: true });
639 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
641 g
.updateOptions({ includeZero
: false });
642 assert
.deepEqual(['500','600','700','800','900','1000'], Util
.getYLabels());
645 it('testAxisLabelFontSize', function() {
646 var graph
= document
.getElementById("graph");
647 var g
= new Dygraph(graph
, simpleData
, {});
649 // Be sure we're dealing with a 14-point default.
650 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
652 var assertFontSize
= function(selector
, expected
) {
653 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
656 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
657 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y") , "14px");
659 g
.updateOptions({ axisLabelFontSize
: 8});
660 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "8px");
661 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "8px");
664 axisLabelFontSize
: null,
666 x
: { axisLabelFontSize
: 5 },
670 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
671 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
675 y
: { axisLabelFontSize
: 20 },
679 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
680 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "20px");
684 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
687 y2
: { axisLabelFontSize
: 12 },
691 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
692 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y1"), "20px");
693 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y2"), "12px");
696 it('testAxisLabelFontSizeNull', function() {
697 var graph
= document
.getElementById("graph");
698 var g
= new Dygraph(graph
, simpleData
,
700 axisLabelFontSize
: null
703 var assertFontSize
= function(selector
, expected
) {
704 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
707 // Be sure we're dealing with a 14-point default.
708 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
710 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
711 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
714 it('testAxisLabelColor', function() {
715 var graph
= document
.getElementById("graph");
716 var g
= new Dygraph(graph
, simpleData
, {});
718 // Be sure we're dealing with a black default.
719 assert
.equal("black", Dygraph
.DEFAULT_ATTRS
.axisLabelColor
);
721 var assertColor
= function(selector
, expected
) {
722 Util
.assertStyleOfChildren(selector
, "color", expected
);
725 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
726 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
728 g
.updateOptions({ axisLabelColor
: "red"});
729 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(255, 0, 0)");
730 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(255, 0, 0)");
733 axisLabelColor
: null,
735 x
: { axisLabelColor
: "blue" },
739 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
740 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
744 y
: { axisLabelColor
: "green" },
748 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
749 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 128, 0)");
753 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
756 y2
: { axisLabelColor
: "yellow" },
760 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
761 assertColor(document
.querySelectorAll(".dygraph-axis-label-y1"), "rgb(0, 128, 0)");
762 assertColor(document
.querySelectorAll(".dygraph-axis-label-y2"), "rgb(255, 255, 0)");
765 it('testAxisLabelColorNull', function() {
766 var graph
= document
.getElementById("graph");
767 var g
= new Dygraph(graph
, simpleData
,
772 var assertColor
= function(selector
, expected
) {
773 Util
.assertStyleOfChildren(selector
, "color", expected
);
776 // Be sure we're dealing with a 14-point default.
777 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
779 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
780 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
784 * This test shows that the label formatter overrides labelsKMB for all values.
786 it('testLabelFormatterOverridesLabelsKMB', function() {
788 document
.getElementById("graph"),
795 axisLabelFormatter
: function (v
) {
799 assert
.deepEqual(["0:X","500:X","1000:X","1500:X","2000:X"], Util
.getYLabels());
800 assert
.deepEqual(["1:X","2:X","3:X"], Util
.getXLabels());
804 * This test shows that you can override labelsKMB on the axis level.
806 it('testLabelsKMBPerAxis', function() {
808 document
.getElementById("graph"),
816 y2
: { labelsKMB
: true },
817 x
: { labelsKMB
: true }
824 // labelsKMB doesn't apply to the x axis. This value should be different.
825 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
826 assert
.deepEqual(["1000","2000","3000"], Util
.getXLabels());
827 assert
.deepEqual(["0","500","1000","1500","2000"], Util
.getYLabels(1));
828 assert
.deepEqual(["0","500","1K","1.5K","2K"], Util
.getYLabels(2));
832 * This test shows that you can override labelsKMG2 on the axis level.
834 it('testLabelsKMBG2IPerAxis', function() {
836 document
.getElementById("graph"),
844 y2
: { labelsKMG2
: true },
845 x
: { labelsKMG2
: true, pixelsPerLabel
: 60 }
852 // It is weird that labelsKMG2 does something on the x axis but KMB does not.
853 // Plus I can't be sure they're doing the same thing as they're done in different
855 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
856 assert
.deepEqual(["1024","2048","3072"], Util
.getXLabels());
857 assert
.deepEqual(["0","500","1000","1500","2000"], Util
.getYLabels(1));
858 assert
.deepEqual(["0","500","1000","1.46k","1.95k"], Util
.getYLabels(2));
862 * This test shows you can override sigFigs on the axis level.
864 it('testSigFigsPerAxis', function() {
866 document
.getElementById("graph"),
882 // sigFigs doesn't apply to the x axis. This value should be different.
883 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
884 assert
.deepEqual(["1000","2000","3000"], Util
.getXLabels());
885 assert
.deepEqual(["0.0","5.0e+2","1.0e+3","1.5e+3","2.0e+3"], Util
.getYLabels(1));
886 assert
.deepEqual(["0.00000","500.000","1000.00","1500.00","2000.00"], Util
.getYLabels(2));
890 * This test shows you can override digitsAfterDecimal on the axis level.
892 it('testDigitsAfterDecimalPerAxis', function() {
894 document
.getElementById("graph"),
896 "0.006,0.001,0.008\n" +
897 "0.007,0.002,0.007\n" +
898 "0.008,0.003,0.006\n" +
899 "0.009,0.004,0.005\n", {
900 digitsAfterDecimal
: 1,
907 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 3 }}});
908 assert
.deepEqual(["0.001","0.002","0.002","0.003","0.003","0.004","0.004"], Util
.getYLabels(1));
909 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 4 }}});
910 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
911 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 5 }}});
912 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
913 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: null }}});
914 assert
.deepEqual(["1e-3","2e-3","2e-3","3e-3","3e-3","4e-3","4e-3"], Util
.getYLabels(1));
916 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 3 }}});
917 assert
.deepEqual(["0.005","0.006","0.006","0.007","0.007","0.008","0.008"], Util
.getYLabels(2));
918 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 4 }}});
919 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
920 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 5 }}});
921 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
922 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: null }}});
923 assert
.deepEqual(["5e-3","6e-3","6e-3","7e-3","7e-3","7e-3","8e-3"], Util
.getYLabels(2));
926 // digitsAfterDecimal is ignored for the x-axis.
927 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
928 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 3 }}});
929 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
930 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 4 }}});
931 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
932 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 5 }}});
933 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
934 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: null }}});
935 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
939 * This test shows you can override digitsAfterDecimal on the axis level.
941 it('testMaxNumberWidthPerAxis', function() {
943 document
.getElementById("graph"),
945 "12401,12601,12804\n" +
946 "12402,12602,12803\n" +
947 "12403,12603,12802\n" +
948 "12404,12604,12801\n", {
955 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 4 }}});
956 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));
957 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 5 }}});
958 assert
.deepEqual(["12601","12601.5","12602","12602.5","12603","12603.5","12604"] , Util
.getYLabels(1));
959 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: null }}});
960 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));
962 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 4 }}});
963 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));
964 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 5 }}});
965 assert
.deepEqual(["12801","12801.5","12802","12802.5","12803","12803.5","12804"], Util
.getYLabels(2));
966 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: null }}});
967 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));
969 // maxNumberWidth is ignored for the x-axis.
970 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
971 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 4 }}});
972 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
973 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 5 }}});
974 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
975 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: null }}});
976 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
980 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=147
981 // Checks that axis labels stay sane across a DST change.
982 it('testLabelsCrossDstChange', function() {
983 // (From tests/daylight-savings.html)
985 document.getElementById("graph"),
986 "Date/Time,Purchases\n" +
987 "2010-11-05 00:00:00,167082\n" +
988 "2010-11-06 00:00:00,168571\n" +
989 "2010-11-07 00:00:00,177796\n" +
990 "2010-11-08 00:00:00,165587\n" +
991 "2010-11-09 00:00:00,164380\n",
995 // Dates and "nice" hours: 6AM/PM and noon, not 5AM/11AM/...
1007 var xLabels = Util.getXLabels();
1008 for (var i = 0; i < xLabels.length; i++) {
1009 assert.isTrue(okLabels[xLabels[i]]);
1012 // This range had issues of its own on tests/daylight-savings.html.
1014 dateWindow: [1289109997722.8127, 1289261208937.7659]
1016 xLabels = Util.getXLabels();
1017 for (var i = 0; i < xLabels.length; i++) {
1018 assert.isTrue(okLabels[xLabels[i]]);
1023 // Tests data which crosses a "fall back" at a high enough frequency that you
1024 // can see both 1:00 A.M.s.
1025 it('testLabelsCrossDstChangeHighFreq', function() {
1026 // Generate data which crosses the EST/EDT boundary.
1028 var base_ms = 1383454200000;
1029 for (var x = base_ms; x < base_ms + 1000 * 60 * 80; x += 1000) {
1030 dst_data.push([new Date(x), x]);
1033 var g = new Dygraph(
1034 document.getElementById("graph"),
1036 { width: 1024, labels: ['Date', 'Value'] }
1041 '01:00', '01:05', '01:10', '01:15', '01:20', '01:25',
1042 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
1043 '01:00', '01:05' // 1 AM number two!
1044 ], Util.getXLabels());
1046 // Now zoom past the initial 1 AM. This used to cause trouble.
1048 dateWindow: [1383454200000 + 15*60*1000, g.xAxisExtremes()[1]]}
1051 '01:05', '01:10', '01:15', '01:20', '01:25',
1052 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
1053 '01:00', '01:05' // 1 AM number two!
1054 ], Util.getXLabels());
1058 // Tests data which crosses a "spring forward" at a low frequency.
1059 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=433
1060 it('testLabelsCrossSpringForward', function() {
1061 var g = new Dygraph(
1062 document.getElementById("graph"),
1063 "Date/Time,Purchases\n" +
1064 "2011-03-11 00:00:00,167082\n" +
1065 "2011-03-12 00:00:00,168571\n" +
1066 "2011-03-13 00:00:00,177796\n" +
1067 "2011-03-14 00:00:00,165587\n" +
1068 "2011-03-15 00:00:00,164380\n",
1071 dateWindow: [1299989043119.4365, 1300080693627.4866]
1076 // '02:00': true, // not a real time!
1090 var xLabels = Util.getXLabels();
1091 for (var i = 0; i < xLabels.length; i++) {
1092 assert.isTrue(okLabels[xLabels[i]]);
1096 it('testLabelsCrossSpringForwardHighFreq', function() {
1097 var base_ms_spring = 1299999000000;
1098 var dst_data_spring = [];
1099 for (var x = base_ms_spring; x < base_ms_spring + 1000 * 60 * 80; x += 1000) {
1100 dst_data_spring.push([new Date(x), x]);
1103 var g = new Dygraph(
1104 document.getElementById("graph"),
1106 { width: 1024, labels: ['Date', 'Value'] }
1111 '03:00', '03:05', '03:10', '03:15', '03:20', '03:25',
1112 '03:30', '03:35', '03:40', '03:45', '03:50', '03:55',
1114 ], Util.getXLabels());