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 function getYLabels() {
16 var y_labels
= document
.getElementsByClassName("dygraph-axis-label-y");
18 for (var i
= 0; i
< y_labels
.length
; i
++) {
19 ary
.push(y_labels
[i
].innerHTML
);
24 function getXLabels() {
25 var x_labels
= document
.getElementsByClassName("dygraph-axis-label-x");
27 for (var i
= 0; i
< x_labels
.length
; i
++) {
28 ary
.push(x_labels
[i
].innerHTML
);
33 AxisLabelsTestCase
.prototype.testMinusOneToOne
= function() {
45 var graph
= document
.getElementById("graph");
46 var g
= new Dygraph(graph
, data
, opts
);
48 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
49 assertEquals(['-1','-0.5','0','0.5','1'], getYLabels());
53 g
.updateOptions({file
: data
});
54 assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels());
58 g
.updateOptions({file
: data
});
59 assertEquals(['-2','0','2','4','6','8','10'], getYLabels());
63 g
.updateOptions({file
: data
});
64 assertEquals(['0','20','40','60','80','100'], getYLabels());
67 AxisLabelsTestCase
.prototype.testSmallRangeNearZero
= function() {
78 opts
.valueRange
= [-0.1, 0.1];
80 var graph
= document
.getElementById("graph");
81 var g
= new Dygraph(graph
, data
, opts
);
82 assertEquals(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"], getYLabels());
84 opts
.valueRange
= [-0.05, 0.05];
85 g
.updateOptions(opts
);
86 // TODO(danvk): why '1.00e-2' and not '0.01'?
87 assertEquals(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"], getYLabels());
89 opts
.valueRange
= [-0.01, 0.01];
90 g
.updateOptions(opts
);
91 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"], getYLabels());
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"], 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"], 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"], getYLabels());
122 AxisLabelsTestCase
.prototype.testXAxisTimeLabelFormatter
= function() {
127 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]];
128 var graph
= document
.getElementById("graph");
129 var g
= new Dygraph(graph
, data
, opts
);
131 xAxisLabelFormatter
: function (totalMinutes
) {
132 var hours
= Math
.floor( totalMinutes
/ 60);
133 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
134 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
140 minutes
= "0"+minutes
;
143 seconds
= "0"+seconds
;
146 var time
= hours
+':'+minutes
+':'+seconds
;
151 assertEquals(["00:05:00", "00:05:06", "00:05:12", "00:05:18", "00:05:24", "00:05:30", "00:05:36", "00:05:42", "00:05:48", "00:05:54"], getXLabels());
152 // assertEquals(['5','5.1','5.2','5.3','5.4','5.5', '5.6', '5.7', '5.8', '5.9'], getXLabels());