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() {
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
);
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));
142 if (hours
< 10) hours
= "0" + hours
;
143 if (minutes
< 10) minutes
= "0" + minutes
;
144 if (seconds
< 10) seconds
= "0" + seconds
;
146 return hours
+ ':' + minutes
+ ':' + seconds
;
152 assert
.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
154 // The legend does not use the axisLabelFormatter:
156 assert
.equal('5.1: Y1: 1', Util
.getLegend());
159 it('testAxisLabelFormatter', function() {
165 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
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());
174 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
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());
186 for (var i
= 0; i
< 10; i
++) {
187 data
.push([i
, 2 * i
]);
189 var graph
= document
.getElementById("graph");
190 var g
= new Dygraph(graph
, data
, opts
);
192 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
193 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
196 assert
.equal("2: y: 4", Util
.getLegend());
199 it('testDateAxisLabelFormatter', function() {
206 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
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());
211 return 'x' + Util
.formatDate(x
);
215 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
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());
227 for (var i
= 1; i
< 10; i
++) {
228 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
230 var graph
= document
.getElementById("graph");
231 var g
= new Dygraph(graph
, data
, opts
);
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());
237 assert
.equal("2011/01/01: y: 2", Util
.getLegend());
240 // This test verifies that when a valueFormatter is set (but not an
241 // axisLabelFormatter), then the valueFormatter is used to format the axis
243 it('testValueFormatter', function() {
249 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
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);
261 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
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);
276 for (var i
= 0; i
< 10; i
++) {
277 data
.push([i
, 2 * i
]);
279 var graph
= document
.getElementById("graph");
280 var g
= new Dygraph(graph
, data
, opts
);
282 // the valueFormatter options do not affect the ticks.
283 assert
.deepEqual(['0','2','4','6','8'], Util
.getXLabels());
284 assert
.deepEqual(["0","5","10","15"],
287 // they do affect the legend, however.
289 assert
.equal("x2: y: y4", Util
.getLegend());
292 it('testDateValueFormatter', function() {
299 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
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);
307 return 'x' + Util
.formatDate(x
);
311 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
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);
327 for (var i
= 1; i
< 10; i
++) {
328 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
330 var graph
= document
.getElementById("graph");
331 var g
= new Dygraph(graph
, data
, opts
);
333 // valueFormatters do not affect ticks.
334 assert
.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
335 assert
.deepEqual(["5","10","15"], Util
.getYLabels());
337 // the valueFormatter options also affect the legend.
339 assert
.equal('x2011/01/03: y: y6', Util
.getLegend());
342 // This test verifies that when both a valueFormatter and an axisLabelFormatter
343 // are specified, the axisLabelFormatter takes precedence.
344 it('testAxisLabelFormatterPrecedence', function() {
350 valueFormatter
: function(x
) {
351 assert
.equal('[Dygraph graph]', this.toString());
354 axisLabelFormatter
: function(x
, granularity
) {
355 assert
.equal('[Dygraph graph]', this.toString());
360 valueFormatter
: function(y
) {
361 assert
.equal('[Dygraph graph]', this.toString());
364 axisLabelFormatter
: function(y
) {
365 assert
.equal('[Dygraph graph]', this.toString());
373 for (var i
= 0; i
< 10; i
++) {
374 data
.push([i
, 2 * i
]);
376 var graph
= document
.getElementById("graph");
377 var g
= new Dygraph(graph
, data
, opts
);
379 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
380 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
383 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
386 // This is the same as the previous test, except that options are added
388 it('testAxisLabelFormatterIncremental', function() {
395 for (var i
= 0; i
< 10; i
++) {
396 data
.push([i
, 2 * i
]);
398 var graph
= document
.getElementById("graph");
399 var g
= new Dygraph(graph
, data
, opts
);
403 valueFormatter
: function(x
) {
412 valueFormatter
: function(y
) {
421 axisLabelFormatter
: function(x
, granularity
) {
430 axisLabelFormatter
: function(y
) {
437 assert
.deepEqual(["x0","x2","x4","x6","x8"], Util
.getXLabels());
438 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
441 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
444 it('testGlobalFormatters', function() {
449 valueFormatter
: function(x
) {
450 assert
.equal('[Dygraph graph]', this);
453 axisLabelFormatter
: function(x
) {
454 assert
.equal('[Dygraph graph]', this);
459 for (var i
= 0; i
< 10; i
++) {
460 data
.push([i
, 2 * i
]);
462 var graph
= document
.getElementById("graph");
463 var g
= new Dygraph(graph
, data
, opts
);
465 assert
.deepEqual(['alf0','alf2','alf4','alf6','alf8'], Util
.getXLabels());
466 assert
.deepEqual(["alf0","alf5","alf10","alf15"], Util
.getYLabels());
469 assert
.equal("vf9: y: vf18", Util
.getLegend());
472 it('testValueFormatterParameters', function() {
474 // change any functions in list to 'fn' -- functions can't be asserted.
475 var killFunctions
= function(list
) {
477 for (var i
= 0; i
< list
.length
; i
++) {
478 if (typeof(list
[i
]) == 'function') {
486 var taggedRecorder
= function(tag
) {
488 calls
.push([tag
].concat([this], killFunctions(arguments
)));
494 x
: { valueFormatter
: taggedRecorder('x') },
495 y
: { valueFormatter
: taggedRecorder('y') },
496 y2
: { valueFormatter
: taggedRecorder('y2') }
502 labels
: ['x', 'y1', 'y2']
508 var graph
= document
.getElementById('graph');
509 var g
= new Dygraph(graph
, data
, opts
);
511 assert
.deepEqual([], calls
);
514 // num or millis, opts, series, dygraph, row, col
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]
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]
529 it('testSeriesOrder', function() {
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"
541 var graph
= document
.getElementById("graph");
542 var g
= new Dygraph(graph
, data
, opts
);
545 assert
.equal('2: 00: 103 01: 203 10: 303 11: 403', Util
.getLegend());
547 // Sanity checks for indexFromSetName
548 assert
.equal(0, g
.indexFromSetName("x"));
549 assert
.equal(1, g
.indexFromSetName("00"));
550 assert
.equal(null, g
.indexFromSetName("abcde"));
552 // Verify that we get the label list back in the right order
553 assert
.deepEqual(["x", "00", "01", "10", "11"], g
.getLabels());
556 it('testLabelKMB', function() {
563 document
.getElementById("graph"),
566 labels
: [ 'X', 'bar' ],
575 assert
.deepEqual(["0", "500", "1K", "1.5K", "2K"], Util
.getYLabels());
578 it('testLabelKMG2', function() {
585 document
.getElementById("graph"),
588 labels
: [ 'X', 'bar' ],
597 assert
.deepEqual(["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
601 // Same as testLabelKMG2 but specifies the option at the
602 // top of the option dictionary.
603 it('testLabelKMG2_top', function() {
610 document
.getElementById("graph"),
613 labels
: [ 'X', 'bar' ],
619 ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
623 it('testSmallLabelKMB', function() {
626 data
.push([1, 1e-6]);
627 data
.push([2, 2e-6]);
630 document
.getElementById("graph"),
633 labels
: [ 'X', 'bar' ],
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'],
647 it('testSmallLabelKMG2', function() {
650 data
.push([1, 1e-6]);
651 data
.push([2, 2e-6]);
654 document
.getElementById("graph"),
657 labels
: [ 'X', 'bar' ],
666 // TODO(danvk): this is strange--the values aren't on powers of two, and are
667 // these units really used for powers of two in <1? See issue #571.
668 assert
.deepEqual(['0', '0.48u', '0.95u', '1.43u', '1.91u'],
673 * Verify that log scale axis range is properly specified.
675 it('testLogScale', function() {
676 var g
= new Dygraph("graph",
677 [[0, 5], [1, 1000]], {
681 var nonEmptyLabels
= Util
.getYLabels().filter(function(x
) { return x
.length
> 0; });
682 assert
.deepEqual(["5","10","20","50","100","200","500","1000"], nonEmptyLabels
);
684 g
.updateOptions({ logscale
: false });
685 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
689 * Verify that include zero range is properly specified.
691 it('testIncludeZero', function() {
692 var g
= new Dygraph("graph",
693 [[0, 500], [1, 1000]], {
697 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
699 g
.updateOptions({ includeZero
: false });
700 assert
.deepEqual(['500','600','700','800','900','1000'], Util
.getYLabels());
703 it('testAxisLabelFontSize', function() {
704 var graph
= document
.getElementById("graph");
705 var g
= new Dygraph(graph
, simpleData
, {});
707 // Be sure we're dealing with a 14-point default.
708 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
710 var assertFontSize
= function(selector
, expected
) {
711 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
714 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
715 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
717 g
.updateOptions({axisLabelFontSize
: 8});
718 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "8px");
719 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "8px");
722 axisLabelFontSize
: null,
724 x
: { axisLabelFontSize
: 5 },
728 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
729 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
733 y
: { axisLabelFontSize
: 20 },
737 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
738 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "20px");
742 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
745 y2
: { axisLabelFontSize
: 12 },
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");
754 it('testAxisLabelFontSizeNull', function() {
755 var graph
= document
.getElementById("graph");
756 var g
= new Dygraph(graph
, simpleData
,
758 axisLabelFontSize
: null
761 var assertFontSize
= function(selector
, expected
) {
762 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
765 // Be sure we're dealing with a 14-point default.
766 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
768 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
769 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
772 it('testAxisLabelColor', function() {
773 var graph
= document
.getElementById("graph");
774 var g
= new Dygraph(graph
, simpleData
, {});
776 // Be sure we're dealing with a black default.
777 assert
.equal("black", Dygraph
.DEFAULT_ATTRS
.axisLabelColor
);
779 var assertColor
= function(selector
, expected
) {
780 Util
.assertStyleOfChildren(selector
, "color", expected
);
783 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
784 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
786 g
.updateOptions({ axisLabelColor
: "red"});
787 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(255, 0, 0)");
788 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(255, 0, 0)");
791 axisLabelColor
: null,
793 x
: { axisLabelColor
: "blue" },
797 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
798 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
802 y
: { axisLabelColor
: "green" },
806 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
807 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 128, 0)");
811 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
814 y2
: { axisLabelColor
: "yellow" },
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)");
823 it('testAxisLabelColorNull', function() {
824 var graph
= document
.getElementById("graph");
825 var g
= new Dygraph(graph
, simpleData
,
830 var assertColor
= function(selector
, expected
) {
831 Util
.assertStyleOfChildren(selector
, "color", expected
);
834 // Be sure we're dealing with a 14-point default.
835 assert
.equal(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
837 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
838 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
842 * This test shows that the label formatter overrides labelsKMB for all values.
844 it('testLabelFormatterOverridesLabelsKMB', function() {
846 document
.getElementById("graph"),
853 axisLabelFormatter
: function (v
) {
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());
862 * This test shows that you can override labelsKMB on the axis level.
864 it('testLabelsKMBPerAxis', function() {
866 document
.getElementById("graph"),
874 y2
: { labelsKMB
: true },
875 x
: { labelsKMB
: true }
882 // labelsKMB 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","500","1000","1500","2000"], Util
.getYLabels(1));
886 assert
.deepEqual(["0","500","1K","1.5K","2K"], Util
.getYLabels(2));
890 * This test shows that you can override labelsKMG2 on the axis level.
892 it('testLabelsKMBG2IPerAxis', function() {
894 document
.getElementById("graph"),
902 y2
: { labelsKMG2
: true },
903 x
: { labelsKMG2
: true, pixelsPerLabel
: 60 }
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
913 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
914 assert
.deepEqual(["1024","2048","3072"], Util
.getXLabels());
915 assert
.deepEqual(["0","500","1000","1500","2000"], Util
.getYLabels(1));
916 assert
.deepEqual(["0","500","1000","1.46k","1.95k"], Util
.getYLabels(2));
920 * This test shows you can override sigFigs on the axis level.
922 it('testSigFigsPerAxis', function() {
924 document
.getElementById("graph"),
940 // sigFigs doesn't apply to the x axis. This value should be different.
941 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
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));
948 * This test shows you can override digitsAfterDecimal on the axis level.
950 it('testDigitsAfterDecimalPerAxis', function() {
952 document
.getElementById("graph"),
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,
965 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 3 }}});
966 assert
.deepEqual(["0.001","0.002","0.002","0.003","0.003","0.004","0.004"], Util
.getYLabels(1));
967 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 4 }}});
968 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
969 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 5 }}});
970 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
971 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: null }}});
972 assert
.deepEqual(["1e-3","2e-3","2e-3","3e-3","3e-3","4e-3","4e-3"], Util
.getYLabels(1));
974 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 3 }}});
975 assert
.deepEqual(["0.005","0.006","0.006","0.007","0.007","0.008","0.008"], Util
.getYLabels(2));
976 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 4 }}});
977 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
978 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 5 }}});
979 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
980 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: null }}});
981 assert
.deepEqual(["5e-3","6e-3","6e-3","7e-3","7e-3","7e-3","8e-3"], Util
.getYLabels(2));
984 // digitsAfterDecimal is ignored for the x-axis.
985 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
986 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 3 }}});
987 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
988 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 4 }}});
989 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
990 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 5 }}});
991 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
992 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: null }}});
993 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
997 * This test shows you can override digitsAfterDecimal on the axis level.
999 it('testMaxNumberWidthPerAxis', function() {
1000 var g
= new Dygraph(
1001 document
.getElementById("graph"),
1003 "12401,12601,12804\n" +
1004 "12402,12602,12803\n" +
1005 "12403,12603,12802\n" +
1006 "12404,12604,12801\n", {
1013 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 4 }}});
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));
1015 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 5 }}});
1016 assert
.deepEqual(["12601","12601.5","12602","12602.5","12603","12603.5","12604"] , Util
.getYLabels(1));
1017 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: null }}});
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));
1020 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 4 }}});
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));
1022 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 5 }}});
1023 assert
.deepEqual(["12801","12801.5","12802","12802.5","12803","12803.5","12804"], Util
.getYLabels(2));
1024 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: null }}});
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));
1027 // maxNumberWidth is ignored for the x-axis.
1028 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
1029 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 4 }}});
1030 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
1031 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 5 }}});
1032 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
1033 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: null }}});
1034 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
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.
1040 it('testLabelsCrossDstChange', function() {
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",
1053 // Dates and "nice" hours: 6AM/PM and noon, not 5AM/11AM/...
1065 var xLabels = Util.getXLabels();
1066 for (var i = 0; i < xLabels.length; i++) {
1067 assert.isTrue(okLabels[xLabels[i]]);
1070 // This range had issues of its own on tests/daylight-savings.html.
1072 dateWindow: [1289109997722.8127, 1289261208937.7659]
1074 xLabels = Util.getXLabels();
1075 for (var i = 0; i < xLabels.length; i++) {
1076 assert.isTrue(okLabels[xLabels[i]]);
1081 // Tests data which crosses a "fall back" at a high enough frequency that you
1082 // can see both 1:00 A.M.s.
1083 it('testLabelsCrossDstChangeHighFreq', function() {
1084 // Generate data which crosses the EST/EDT boundary.
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]);
1091 var g = new Dygraph(
1092 document.getElementById("graph"),
1094 { width: 1024, labels: ['Date', 'Value'] }
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());
1104 // Now zoom past the initial 1 AM. This used to cause trouble.
1106 dateWindow: [1383454200000 + 15*60*1000, g.xAxisExtremes()[1]]}
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());
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
1118 it('testLabelsCrossSpringForward', function() {
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",
1129 dateWindow: [1299989043119.4365, 1300080693627.4866]
1134 // '02:00': true, // not a real time!
1148 var xLabels = Util.getXLabels();
1149 for (var i = 0; i < xLabels.length; i++) {
1150 assert.isTrue(okLabels[xLabels[i]]);
1154 it('testLabelsCrossSpringForwardHighFreq', function() {
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]);
1161 var g = new Dygraph(
1162 document.getElementById("graph"),
1164 { width: 1024, labels: ['Date', 'Value'] }
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',
1172 ], Util.getXLabels());