2 * @fileoverview Test cases for how axis labels are chosen and formatted.
4 * @author dan@dygraphs.com (Dan Vanderkam)
6 var AxisLabelsTestCase
= TestCase("axis-labels");
8 AxisLabelsTestCase
.prototype.setUp
= function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 AxisLabelsTestCase
.prototype.tearDown
= function() {
15 AxisLabelsTestCase
.simpleData
=
22 AxisLabelsTestCase
.prototype.kCloseFloat
= 1.0e-10;
24 AxisLabelsTestCase
.prototype.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 assertEquals(['-1','-0.5','0','0.5','1'], Util
.getYLabels());
44 g
.updateOptions({file
: data
});
45 assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], Util
.getYLabels());
49 g
.updateOptions({file
: data
});
50 assertEquals(['-2','0','2','4','6','8','10'], Util
.getYLabels());
54 g
.updateOptions({file
: data
});
55 assertEquals(['0','20','40','60','80','100'], Util
.getYLabels());
58 assertEquals('0: Y: -1', Util
.getLegend());
61 AxisLabelsTestCase
.prototype.testSmallRangeNearZero
= function() {
73 opts
.valueRange
= [-0.1, 0.1];
75 var graph
= document
.getElementById("graph");
76 var g
= new Dygraph(graph
, data
, opts
);
77 assertEqualsDelta([-0.1, -0.08, -0.06, -0.04, -0.02, 0, 0.02, 0.04, 0.06, 0.08],
78 Util
.makeNumbers(Util
.getYLabels()), this.kCloseFloat
);
80 opts
.valueRange
= [-0.05, 0.05];
81 g
.updateOptions(opts
);
82 assertEquals([-0.05, -0.04, -0.03, -0.02, -0.01, 0, 0.01, 0.02, 0.03, 0.04],
83 Util
.makeNumbers(Util
.getYLabels()));
85 opts
.valueRange
= [-0.01, 0.01];
86 g
.updateOptions(opts
);
87 assertEquals([-0.01, -8.00e-3, -6.00e-3, -4.00e-3, -2.00e-3, 0, 2.00e-3, 4.00e-3, 6.00e-3, 8.00e-3],
88 Util
.makeNumbers(Util
.getYLabels()));
91 assertEquals('1: Y: 0', Util
.getLegend());
94 AxisLabelsTestCase
.prototype.testSmallRangeAwayFromZero
= function() {
105 var graph
= document
.getElementById("graph");
107 opts
.valueRange
= [9.9, 10.1];
108 var g
= new Dygraph(graph
, data
, opts
);
109 assertEquals(["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 assertEquals(["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 assertEquals(["10","10","10","10","10","10","10","10","10","10"], Util
.getYLabels());
122 assertEquals('1: Y: 0', Util
.getLegend());
125 AxisLabelsTestCase
.prototype.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 assertEquals(["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 assertEquals('5.1: Y1: 1', Util
.getLegend());
158 AxisLabelsTestCase
.prototype.testAxisLabelFormatter
= function () {
164 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
165 assertEquals('number', typeof(x
));
166 assertEquals('number', typeof(granularity
));
167 assertEquals('function', typeof(opts
));
168 assertEquals('[Dygraph graph]', dg
.toString());
173 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
174 assertEquals('number', typeof(y
));
175 assertEquals('number', typeof(granularity
));
176 assertEquals('function', typeof(opts
));
177 assertEquals('[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 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
192 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
195 assertEquals("2: y: 4", Util
.getLegend());
198 AxisLabelsTestCase
.prototype.testDateAxisLabelFormatter
= function () {
204 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
205 assertTrue(Dygraph
.isDateLike(x
));
206 assertEquals('number', typeof(granularity
));
207 assertEquals('function', typeof(opts
));
208 assertEquals('[Dygraph graph]', dg
.toString());
209 return 'x' + x
.strftime('%Y/%m/%d');
213 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
214 assertEquals('number', typeof(y
));
215 assertEquals('number', typeof(granularity
));
216 assertEquals('function', typeof(opts
));
217 assertEquals('[Dygraph graph]', dg
.toString());
225 for (var i
= 1; i
< 10; i
++) {
226 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
228 var graph
= document
.getElementById("graph");
229 var g
= new Dygraph(graph
, data
, opts
);
231 assertEquals(["x2011/01/01", "x2011/01/02", "x2011/01/03", "x2011/01/04", "x2011/01/05", "x2011/01/06", "x2011/01/07", "x2011/01/08", "x2011/01/09"], Util
.getXLabels());
232 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
235 assertEquals("2011/01/01: y: 2", Util
.getLegend());
238 // This test verifies that when a valueFormatter is set (but not an
239 // axisLabelFormatter), then the valueFormatter is used to format the axis
241 AxisLabelsTestCase
.prototype.testValueFormatter
= function () {
247 valueFormatter
: function(x
, opts
, series_name
, dg
) {
248 assertEquals('number', typeof(x
));
249 assertEquals('function', typeof(opts
));
250 assertEquals('string', typeof(series_name
));
251 assertEquals('[Dygraph graph]', dg
.toString());
256 valueFormatter
: function(y
, opts
, series_name
, dg
) {
257 assertEquals('number', typeof(y
));
258 assertEquals('function', typeof(opts
));
259 assertEquals('string', typeof(series_name
));
260 assertEquals('[Dygraph graph]', dg
.toString());
268 for (var i
= 0; i
< 10; i
++) {
269 data
.push([i
, 2 * i
]);
271 var graph
= document
.getElementById("graph");
272 var g
= new Dygraph(graph
, data
, opts
);
274 // the valueFormatter options do not affect the ticks.
275 assertEquals(['0','2','4','6','8'], Util
.getXLabels());
276 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
279 // they do affect the legend, however.
281 assertEquals("x2: y: y4", Util
.getLegend());
284 AxisLabelsTestCase
.prototype.testDateValueFormatter
= function () {
290 valueFormatter
: function(x
, opts
, series_name
, dg
) {
291 assertEquals('number', typeof(x
));
292 assertEquals('function', typeof(opts
));
293 assertEquals('string', typeof(series_name
));
294 assertEquals('[Dygraph graph]', dg
.toString());
295 return 'x' + new Date(x
).strftime('%Y/%m/%d');
299 valueFormatter
: function(y
, opts
, series_name
, dg
) {
300 assertEquals('number', typeof(y
));
301 assertEquals('function', typeof(opts
));
302 assertEquals('string', typeof(series_name
));
303 assertEquals('[Dygraph graph]', dg
.toString());
312 for (var i
= 1; i
< 10; i
++) {
313 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
315 var graph
= document
.getElementById("graph");
316 var g
= new Dygraph(graph
, data
, opts
);
318 // valueFormatters do not affect ticks.
319 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], Util
.getXLabels());
320 assertEquals(['2','4','6','8','10','12','14','16','18'], Util
.getYLabels());
322 // the valueFormatter options also affect the legend.
324 assertEquals('x2011/01/03: y: y6', Util
.getLegend());
327 // This test verifies that when both a valueFormatter and an axisLabelFormatter
328 // are specified, the axisLabelFormatter takes precedence.
329 AxisLabelsTestCase
.prototype.testAxisLabelFormatterPrecedence
= function () {
335 valueFormatter
: function(x
) {
338 axisLabelFormatter
: function(x
, granularity
) {
343 valueFormatter
: function(y
) {
346 axisLabelFormatter
: function(y
) {
354 for (var i
= 0; i
< 10; i
++) {
355 data
.push([i
, 2 * i
]);
357 var graph
= document
.getElementById("graph");
358 var g
= new Dygraph(graph
, data
, opts
);
360 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
361 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
364 assertEquals("xvf9: y: yvf18", Util
.getLegend());
367 // This is the same as the previous test, except that options are added
369 AxisLabelsTestCase
.prototype.testAxisLabelFormatterIncremental
= function () {
376 for (var i
= 0; i
< 10; i
++) {
377 data
.push([i
, 2 * i
]);
379 var graph
= document
.getElementById("graph");
380 var g
= new Dygraph(graph
, data
, opts
);
384 valueFormatter
: function(x
) {
393 valueFormatter
: function(y
) {
402 axisLabelFormatter
: function(x
, granularity
) {
411 axisLabelFormatter
: function(y
) {
418 assertEquals(["x0","x2","x4","x6","x8"], Util
.getXLabels());
419 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
422 assertEquals("xvf9: y: yvf18", Util
.getLegend());
425 AxisLabelsTestCase
.prototype.testGlobalFormatters
= function() {
430 valueFormatter
: function(x
) {
433 axisLabelFormatter
: function(x
) {
438 for (var i
= 0; i
< 10; i
++) {
439 data
.push([i
, 2 * i
]);
441 var graph
= document
.getElementById("graph");
442 var g
= new Dygraph(graph
, data
, opts
);
444 assertEquals(['alf0','alf2','alf4','alf6','alf8'], Util
.getXLabels());
445 assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], Util
.getYLabels());
448 assertEquals("vf9: y: vf18", Util
.getLegend());
451 AxisLabelsTestCase
.prototype.testSeriesOrder
= function() {
456 var data
= "x,00,01,10,11\n" +
457 "0,101,201,301,401\n" +
458 "1,102,202,302,402\n" +
459 "2,103,203,303,403\n" +
460 "3,104,204,304,404\n"
463 var graph
= document
.getElementById("graph");
464 var g
= new Dygraph(graph
, data
, opts
);
467 assertEquals('2: 00: 103 01: 203 10: 303 11: 403', Util
.getLegend());
469 // Sanity checks for indexFromSetName
470 assertEquals(0, g
.indexFromSetName("x"));
471 assertEquals(1, g
.indexFromSetName("00"));
472 assertEquals(null, g
.indexFromSetName("abcde"));
474 // Verify that we get the label list back in the right order
475 assertEquals(["x", "00", "01", "10", "11"], g
.getLabels());
478 AxisLabelsTestCase
.prototype.testLabelKMB
= function() {
485 document
.getElementById("graph"),
488 labels
: [ 'X', 'bar' ],
497 assertEquals(["0", "500", "1K", "1.5K", "2K"], Util
.getYLabels());
500 AxisLabelsTestCase
.prototype.testLabelKMG2
= function() {
507 document
.getElementById("graph"),
510 labels
: [ 'X', 'bar' ],
520 ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
524 // Same sa testLabelKMG2 but specifies the option at the
525 // top of the option dictionary.
526 AxisLabelsTestCase
.prototype.testLabelKMG2_top
= function() {
533 document
.getElementById("graph"),
536 labels
: [ 'X', 'bar' ],
542 ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
547 * Verify that log scale axis range is properly specified.
549 AxisLabelsTestCase
.prototype.testLogScale
= function() {
550 var g
= new Dygraph("graph", [[0, 5], [1, 1000]], { logscale
: true });
551 var nonEmptyLabels
= Util
.getYLabels().filter(function(x
) { return x
.length
> 0; });
552 assertEquals(["6","10","30","60","100","300","600","1000"], nonEmptyLabels
);
554 g
.updateOptions({ logscale
: false });
555 assertEquals(['0','200','400','600','800','1000'], Util
.getYLabels());
559 * Verify that include zero range is properly specified.
561 AxisLabelsTestCase
.prototype.testIncludeZero
= function() {
562 var g
= new Dygraph("graph", [[0, 500], [1, 1000]], { includeZero
: true });
563 assertEquals(['0','200','400','600','800','1000'], Util
.getYLabels());
565 g
.updateOptions({ includeZero
: false });
566 assertEquals(['500','600','700','800','900','1000'], Util
.getYLabels());
569 AxisLabelsTestCase
.prototype.testAxisLabelFontSize
= function() {
570 var graph
= document
.getElementById("graph");
571 var g
= new Dygraph(graph
, AxisLabelsTestCase
.simpleData
, {});
573 // Be sure we're dealing with a 14-point default.
574 assertEquals(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
576 var assertFontSize
= function(selector
, expected
) {
577 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
580 assertFontSize($(".dygraph-axis-label-x"), "14px");
581 assertFontSize($(".dygraph-axis-label-y") , "14px");
583 g
.updateOptions({ axisLabelFontSize
: 8});
584 assertFontSize($(".dygraph-axis-label-x"), "8px");
585 assertFontSize($(".dygraph-axis-label-y"), "8px");
588 axisLabelFontSize
: null,
590 x
: { axisLabelFontSize
: 5 },
594 assertFontSize($(".dygraph-axis-label-x"), "5px");
595 assertFontSize($(".dygraph-axis-label-y"), "14px");
599 y
: { axisLabelFontSize
: 20 },
603 assertFontSize($(".dygraph-axis-label-x"), "5px");
604 assertFontSize($(".dygraph-axis-label-y"), "20px");
608 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
611 y2
: { axisLabelFontSize
: 12 },
615 assertFontSize($(".dygraph-axis-label-x"), "5px");
616 assertFontSize($(".dygraph-axis-label-y1"), "20px");
617 assertFontSize($(".dygraph-axis-label-y2"), "12px");
620 AxisLabelsTestCase
.prototype.testAxisLabelFontSizeNull
= function() {
621 var graph
= document
.getElementById("graph");
622 var g
= new Dygraph(graph
, AxisLabelsTestCase
.simpleData
,
624 axisLabelFontSize
: null
627 var assertFontSize
= function(selector
, expected
) {
628 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
631 // Be sure we're dealing with a 14-point default.
632 assertEquals(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
634 assertFontSize($(".dygraph-axis-label-x"), "14px");
635 assertFontSize($(".dygraph-axis-label-y"), "14px");
638 AxisLabelsTestCase
.prototype.testAxisLabelColor
= function() {
639 var graph
= document
.getElementById("graph");
640 var g
= new Dygraph(graph
, AxisLabelsTestCase
.simpleData
, {});
642 // Be sure we're dealing with a black default.
643 assertEquals("black", Dygraph
.DEFAULT_ATTRS
.axisLabelColor
);
645 var assertColor
= function(selector
, expected
) {
646 Util
.assertStyleOfChildren(selector
, "color", expected
);
649 assertColor($(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
650 assertColor($(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
652 g
.updateOptions({ axisLabelColor
: "red"});
653 assertColor($(".dygraph-axis-label-x"), "rgb(255, 0, 0)");
654 assertColor($(".dygraph-axis-label-y"), "rgb(255, 0, 0)");
657 axisLabelColor
: null,
659 x
: { axisLabelColor
: "blue" },
663 assertColor($(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
664 assertColor($(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
668 y
: { axisLabelColor
: "green" },
672 assertColor($(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
673 assertColor($(".dygraph-axis-label-y"), "rgb(0, 128, 0)");
677 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
680 y2
: { axisLabelColor
: "yellow" },
684 assertColor($(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
685 assertColor($(".dygraph-axis-label-y1"), "rgb(0, 128, 0)");
686 assertColor($(".dygraph-axis-label-y2"), "rgb(255, 255, 0)");
689 AxisLabelsTestCase
.prototype.testAxisLabelColorNull
= function() {
690 var graph
= document
.getElementById("graph");
691 var g
= new Dygraph(graph
, AxisLabelsTestCase
.simpleData
,
696 var assertColor
= function(selector
, expected
) {
697 Util
.assertStyleOfChildren(selector
, "color", expected
);
700 // Be sure we're dealing with a 14-point default.
701 assertEquals(14, Dygraph
.DEFAULT_ATTRS
.axisLabelFontSize
);
703 assertColor($(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
704 assertColor($(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
708 * This test shows that the label formatter overrides labelsKMB for all values.
710 AxisLabelsTestCase
.prototype.testLabelFormatterOverridesLabelsKMB
= function() {
712 document
.getElementById("graph"),
719 axisLabelFormatter
: function (v
) {
723 assertEquals(["0:X","500:X","1000:X","1500:X","2000:X"], Util
.getYLabels());
724 assertEquals(["1:X","1.5:X","2:X","2.5:X","3:X","3.5:X"], Util
.getXLabels());
728 * This test shows that you can override labelsKMB on the axis level.
730 AxisLabelsTestCase
.prototype.testLabelsKMBIgnoredWhenOverridden
= function() {
732 document
.getElementById("graph"),
750 assertEquals(["0","500","1K","1.5K","2K"], Util
.getYLabels(1));
751 assertEquals(["0","500","1000","1500","2000"], Util
.getYLabels(2));
754 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=147
755 // Checks that axis labels stay sane across a DST change.
756 AxisLabelsTestCase
.prototype.testLabelsCrossDstChange
= function() {
757 // (From tests/daylight-savings
.html
)
759 document
.getElementById("graph"),
760 "Date/Time,Purchases\n" +
761 "2010-11-05 00:00:00,167082\n" +
762 "2010-11-06 00:00:00,168571\n" +
763 "2010-11-07 00:00:00,177796\n" +
764 "2010-11-08 00:00:00,165587\n" +
765 "2010-11-09 00:00:00,164380\n",
769 // Dates and "nice" hours: 6AM/PM and noon
, not
5AM
/11AM/...
781 var xLabels
= Util
.getXLabels();
782 for (var i
= 0; i
< xLabels
.length
; i
++) {
783 assertTrue(okLabels
[xLabels
[i
]]);
786 // This range had issues of its own on tests/daylight-savings
.html
.
788 dateWindow
: [1289109997722.8127, 1289261208937.7659]
790 xLabels
= Util
.getXLabels();
791 for (var i
= 0; i
< xLabels
.length
; i
++) {
792 assertTrue(okLabels
[xLabels
[i
]]);
797 // Tests data which crosses a "fall back" at a high enough frequency that you
798 // can see both 1:00 A.M.s.
799 AxisLabelsTestCase
.prototype.testLabelsCrossDstChangeHighFreq
= function() {
800 // Generate data which crosses the EST/EDT boundary
.
802 var base_ms
= 1383454200000;
803 for (var x
= base_ms
; x
< base_ms
+ 1000 * 60 * 80; x
+= 1000) {
804 dst_data
.push([new Date(x
), x
]);
808 document
.getElementById("graph"),
810 { width
: 1024, labels
: ['Date', 'Value'] }
815 '01:00', '01:05', '01:10', '01:15', '01:20', '01:25',
816 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
817 '01:00', '01:05' // 1 AM number two!
818 ], Util
.getXLabels());
820 // Now zoom past the initial 1 AM. This used to cause trouble.
822 dateWindow
: [1383454200000 + 15*60*1000, g
.xAxisExtremes()[1]]}
825 '01:05', '01:10', '01:15', '01:20', '01:25',
826 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
827 '01:00', '01:05' // 1 AM number two!
828 ], Util
.getXLabels());
832 // Tests data which crosses a "spring forward" at a low frequency.
833 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=433
834 AxisLabelsTestCase
.prototype.testLabelsCrossSpringForward
= function() {
836 document
.getElementById("graph"),
837 "Date/Time,Purchases\n" +
838 "2011-03-11 00:00:00,167082\n" +
839 "2011-03-12 00:00:00,168571\n" +
840 "2011-03-13 00:00:00,177796\n" +
841 "2011-03-14 00:00:00,165587\n" +
842 "2011-03-15 00:00:00,164380\n",
845 dateWindow
: [1299989043119.4365, 1300080693627.4866]
850 // '02:00': true, // not a real time
!
864 var xLabels
= Util
.getXLabels();
865 for (var i
= 0; i
< xLabels
.length
; i
++) {
866 assertTrue(okLabels
[xLabels
[i
]]);
870 AxisLabelsTestCase
.prototype.testLabelsCrossSpringForwardHighFreq
= function() {
871 var base_ms_spring
= 1299999000000;
872 var dst_data_spring
= [];
873 for (var x
= base_ms_spring
; x
< base_ms_spring
+ 1000 * 60 * 80; x
+= 1000) {
874 dst_data_spring
.push([new Date(x
), x
]);
878 document
.getElementById("graph"),
880 { width
: 1024, labels
: ['Date', 'Value'] }
885 '03:00', '03:05', '03:10', '03:15', '03:20', '03:25',
886 '03:30', '03:35', '03:40', '03:45', '03:50', '03:55',
888 ], Util
.getXLabels());