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 function makeNumbers(ary
) {
35 for (var i
= 0; i
< ary
.length
; i
++) {
36 ret
.push(parseFloat(ary
[i
]));
41 function getLegend() {
42 var legend
= document
.getElementsByClassName("dygraph-legend")[0];
43 return legend
.textContent
;
46 AxisLabelsTestCase
.prototype.kCloseFloat
= 1.0e-10;
48 AxisLabelsTestCase
.prototype.testMinusOneToOne
= function() {
60 var graph
= document
.getElementById("graph");
61 var g
= new Dygraph(graph
, data
, opts
);
63 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
64 assertEquals(['-1','-0.5','0','0.5','1'], getYLabels());
68 g
.updateOptions({file
: data
});
69 assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels());
73 g
.updateOptions({file
: data
});
74 assertEquals(['-2','0','2','4','6','8','10'], getYLabels());
78 g
.updateOptions({file
: data
});
79 assertEquals(['0','20','40','60','80','100'], getYLabels());
82 assertEquals('0: Y:-1', getLegend());
85 AxisLabelsTestCase
.prototype.testSmallRangeNearZero
= function() {
96 opts
.valueRange
= [-0.1, 0.1];
98 var graph
= document
.getElementById("graph");
99 var g
= new Dygraph(graph
, data
, opts
);
100 assertEqualsDelta(makeNumbers(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"]),
101 makeNumbers(getYLabels()), this.kCloseFloat
);
103 opts
.valueRange
= [-0.05, 0.05];
104 g
.updateOptions(opts
);
105 // TODO(danvk): why '1.00e-2' and not '0.01'?
106 assertEquals(makeNumbers(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"]),
107 makeNumbers(getYLabels()));
109 opts
.valueRange
= [-0.01, 0.01];
110 g
.updateOptions(opts
);
111 assertEquals(makeNumbers(["-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"]), makeNumbers(getYLabels()));
114 assertEquals('1: Y:0', getLegend());
117 AxisLabelsTestCase
.prototype.testSmallRangeAwayFromZero
= function() {
128 var graph
= document
.getElementById("graph");
130 opts
.valueRange
= [9.9, 10.1];
131 var g
= new Dygraph(graph
, data
, opts
);
132 assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels());
134 opts
.valueRange
= [9.99, 10.01];
135 g
.updateOptions(opts
);
136 // TODO(danvk): this is bad
137 assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels());
139 opts
.valueRange
= [9.999, 10.001];
140 g
.updateOptions(opts
);
141 // TODO(danvk): this is even worse!
142 assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels());
145 assertEquals('1: Y:0', getLegend());
148 AxisLabelsTestCase
.prototype.testXAxisTimeLabelFormatter
= function() {
153 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]];
154 var graph
= document
.getElementById("graph");
155 var g
= new Dygraph(graph
, data
, opts
);
157 xAxisLabelFormatter
: function (totalMinutes
) {
158 var hours
= Math
.floor( totalMinutes
/ 60);
159 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
160 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
162 if (hours
< 10) hours
= "0" + hours
;
163 if (minutes
< 10) minutes
= "0" + minutes
;
164 if (seconds
< 10) seconds
= "0" + seconds
;
166 return hours
+ ':' + minutes
+ ':' + seconds
;
170 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], getXLabels());
172 // The legend does not use the xAxisLabelFormatter:
174 assertEquals('5.1: Y1:1', getLegend());
177 AxisLabelsTestCase
.prototype.testAxisLabelFormatter
= function () {
181 xAxisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
182 assertEquals('number', typeof(x
));
183 assertEquals('number', typeof(granularity
));
184 assertEquals('function', typeof(opts
));
185 assertEquals('[Dygraph graph]', dg
.toString());
188 yAxisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
189 assertEquals('number', typeof(y
));
190 assertEquals('number', typeof(granularity
));
191 assertEquals('function', typeof(opts
));
192 assertEquals('[Dygraph graph]', dg
.toString());
198 for (var i
= 0; i
< 10; i
++) {
199 data
.push([i
, 2 * i
]);
201 var graph
= document
.getElementById("graph");
202 var g
= new Dygraph(graph
, data
, opts
);
204 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
205 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
208 assertEquals("2: y:4", getLegend());
211 AxisLabelsTestCase
.prototype.testDateAxisLabelFormatter
= function () {
215 xAxisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
216 assertTrue(Dygraph
.isDateLike(x
));
217 assertEquals('number', typeof(granularity
));
218 assertEquals('function', typeof(opts
));
219 assertEquals('[Dygraph graph]', dg
.toString());
220 return 'x' + x
.strftime('%Y/%m/%d');
222 yAxisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
223 assertEquals('number', typeof(y
));
224 assertEquals('number', typeof(granularity
));
225 assertEquals('function', typeof(opts
));
226 assertEquals('[Dygraph graph]', dg
.toString());
232 for (var i
= 1; i
< 10; i
++) {
233 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
235 var graph
= document
.getElementById("graph");
236 var g
= new Dygraph(graph
, data
, opts
);
238 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"], getXLabels());
239 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
242 assertEquals("2011/01/01: y:2", getLegend());
245 // This test verifies that when a valueFormatter is set (but not an
246 // axisLabelFormatter), then the valueFormatter is used to format the axis
248 AxisLabelsTestCase
.prototype.testValueFormatter
= function () {
252 xValueFormatter
: function(x
, opts
, series_name
, dg
) {
253 assertEquals('number', typeof(x
));
254 assertEquals('function', typeof(opts
));
255 assertEquals('string', typeof(series_name
));
256 assertEquals('[Dygraph graph]', dg
.toString());
259 yValueFormatter
: function(y
, opts
, series_name
, dg
) {
260 assertEquals('number', typeof(y
));
261 assertEquals('function', typeof(opts
));
262 assertEquals('string', typeof(series_name
));
263 assertEquals('[Dygraph graph]', dg
.toString());
269 for (var i
= 0; i
< 10; i
++) {
270 data
.push([i
, 2 * i
]);
272 var graph
= document
.getElementById("graph");
273 var g
= new Dygraph(graph
, data
, opts
);
275 // the valueFormatter options do not affect the ticks.
276 assertEquals(['0','2','4','6','8'], getXLabels());
277 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
280 // they do affect the legend, however.
282 assertEquals("x2: y:y4", getLegend());
285 AxisLabelsTestCase
.prototype.testDateValueFormatter
= function () {
289 xValueFormatter
: function(x
, opts
, series_name
, dg
) {
290 assertEquals('number', typeof(x
));
291 assertEquals('function', typeof(opts
));
292 assertEquals('string', typeof(series_name
));
293 assertEquals('[Dygraph graph]', dg
.toString());
294 return 'x' + new Date(x
).strftime('%Y/%m/%d');
296 yValueFormatter
: function(y
, opts
, series_name
, dg
) {
297 assertEquals('number', typeof(y
));
298 assertEquals('function', typeof(opts
));
299 assertEquals('string', typeof(series_name
));
300 assertEquals('[Dygraph graph]', dg
.toString());
307 for (var i
= 1; i
< 10; i
++) {
308 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
310 var graph
= document
.getElementById("graph");
311 var g
= new Dygraph(graph
, data
, opts
);
313 // valueFormatters do not affect ticks.
314 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], getXLabels());
315 assertEquals(['2','4','6','8','10','12','14','16','18'], getYLabels());
317 // the valueFormatter options also affect the legend.
319 assertEquals('x2011/01/03: y:y6', getLegend());
322 // This test verifies that when both a valueFormatter and an axisLabelFormatter
323 // are specified, the axisLabelFormatter takes precedence.
324 AxisLabelsTestCase
.prototype.testAxisLabelFormatterPrecedence
= function () {
328 xValueFormatter
: function(x
) {
331 yValueFormatter
: function(y
) {
334 xAxisLabelFormatter
: function(x
, granularity
) {
337 yAxisLabelFormatter
: function(y
) {
343 for (var i
= 0; i
< 10; i
++) {
344 data
.push([i
, 2 * i
]);
346 var graph
= document
.getElementById("graph");
347 var g
= new Dygraph(graph
, data
, opts
);
349 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
350 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
353 assertEquals("xvf9: y:yvf18", getLegend());
356 // This is the same as the previous test, except that options are added
358 AxisLabelsTestCase
.prototype.testAxisLabelFormatterIncremental
= function () {
365 for (var i
= 0; i
< 10; i
++) {
366 data
.push([i
, 2 * i
]);
368 var graph
= document
.getElementById("graph");
369 var g
= new Dygraph(graph
, data
, opts
);
371 xValueFormatter
: function(x
) {
376 yValueFormatter
: function(y
) {
381 xAxisLabelFormatter
: function(x
, granularity
) {
386 yAxisLabelFormatter
: function(y
) {
391 assertEquals(["x0","x2","x4","x6","x8"], getXLabels());
392 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
395 assertEquals("xvf9: y:yvf18", getLegend());
398 AxisLabelsTestCase
.prototype.testGlobalFormatters
= function() {
403 valueFormatter
: function(x
) {
406 axisLabelFormatter
: function(x
) {
411 for (var i
= 0; i
< 10; i
++) {
412 data
.push([i
, 2 * i
]);
414 var graph
= document
.getElementById("graph");
415 var g
= new Dygraph(graph
, data
, opts
);
417 assertEquals(['alf0','alf2','alf4','alf6','alf8'], getXLabels());
418 assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], getYLabels());
421 assertEquals("vf9: y:vf18", getLegend());