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() {
97 opts
.valueRange
= [-0.1, 0.1];
99 var graph
= document
.getElementById("graph");
100 var g
= new Dygraph(graph
, data
, opts
);
101 assertEqualsDelta(makeNumbers(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"]),
102 makeNumbers(getYLabels()), this.kCloseFloat
);
104 opts
.valueRange
= [-0.05, 0.05];
105 g
.updateOptions(opts
);
106 // TODO(danvk): why '1.00e-2' and not '0.01'?
107 assertEquals(makeNumbers(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"]),
108 makeNumbers(getYLabels()));
110 opts
.valueRange
= [-0.01, 0.01];
111 g
.updateOptions(opts
);
112 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()));
115 assertEquals('1: Y:0', getLegend());
118 AxisLabelsTestCase
.prototype.testSmallRangeAwayFromZero
= function() {
129 var graph
= document
.getElementById("graph");
131 opts
.valueRange
= [9.9, 10.1];
132 var g
= new Dygraph(graph
, data
, opts
);
133 assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels());
135 opts
.valueRange
= [9.99, 10.01];
136 g
.updateOptions(opts
);
137 // TODO(danvk): this is bad
138 assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels());
140 opts
.valueRange
= [9.999, 10.001];
141 g
.updateOptions(opts
);
142 // TODO(danvk): this is even worse!
143 assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels());
146 assertEquals('1: Y:0', getLegend());
149 AxisLabelsTestCase
.prototype.testXAxisTimeLabelFormatter
= function() {
154 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]];
155 var graph
= document
.getElementById("graph");
156 var g
= new Dygraph(graph
, data
, opts
);
158 xAxisLabelFormatter
: function (totalMinutes
) {
159 var hours
= Math
.floor( totalMinutes
/ 60);
160 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
161 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
163 if (hours
< 10) hours
= "0" + hours
;
164 if (minutes
< 10) minutes
= "0" + minutes
;
165 if (seconds
< 10) seconds
= "0" + seconds
;
167 return hours
+ ':' + minutes
+ ':' + seconds
;
171 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], getXLabels());
173 // The legend does not use the xAxisLabelFormatter:
175 assertEquals('5.1: Y1:1', getLegend());
178 AxisLabelsTestCase
.prototype.testAxisLabelFormatter
= function () {
182 xAxisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
183 assertEquals('number', typeof(x
));
184 assertEquals('number', typeof(granularity
));
185 assertEquals('function', typeof(opts
));
186 assertEquals('[Dygraph graph]', dg
.toString());
189 yAxisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
190 assertEquals('number', typeof(y
));
191 assertEquals('number', typeof(granularity
));
192 assertEquals('function', typeof(opts
));
193 assertEquals('[Dygraph graph]', dg
.toString());
199 for (var i
= 0; i
< 10; i
++) {
200 data
.push([i
, 2 * i
]);
202 var graph
= document
.getElementById("graph");
203 var g
= new Dygraph(graph
, data
, opts
);
205 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
206 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
209 assertEquals("2: y:4", getLegend());
212 AxisLabelsTestCase
.prototype.testDateAxisLabelFormatter
= function () {
216 xAxisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
217 assertTrue(Dygraph
.isDateLike(x
));
218 assertEquals('number', typeof(granularity
));
219 assertEquals('function', typeof(opts
));
220 assertEquals('[Dygraph graph]', dg
.toString());
221 return 'x' + x
.strftime('%Y/%m/%d');
223 yAxisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
224 assertEquals('number', typeof(y
));
225 assertEquals('number', typeof(granularity
));
226 assertEquals('function', typeof(opts
));
227 assertEquals('[Dygraph graph]', dg
.toString());
233 for (var i
= 1; i
< 10; i
++) {
234 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
236 var graph
= document
.getElementById("graph");
237 var g
= new Dygraph(graph
, data
, opts
);
239 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());
240 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
243 assertEquals("2011/01/01: y:2", getLegend());
246 // This test verifies that when a valueFormatter is set (but not an
247 // axisLabelFormatter), then the valueFormatter is used to format the axis
249 AxisLabelsTestCase
.prototype.testValueFormatter
= function () {
253 xValueFormatter
: function(x
, opts
, series_name
, dg
) {
254 assertEquals('number', typeof(x
));
255 assertEquals('function', typeof(opts
));
256 assertEquals('string', typeof(series_name
));
257 assertEquals('[Dygraph graph]', dg
.toString());
260 yValueFormatter
: function(y
, opts
, series_name
, dg
) {
261 assertEquals('number', typeof(y
));
262 assertEquals('function', typeof(opts
));
263 assertEquals('string', typeof(series_name
));
264 assertEquals('[Dygraph graph]', dg
.toString());
270 for (var i
= 0; i
< 10; i
++) {
271 data
.push([i
, 2 * i
]);
273 var graph
= document
.getElementById("graph");
274 var g
= new Dygraph(graph
, data
, opts
);
276 // the valueFormatter options do not affect the ticks.
277 assertEquals(['0','2','4','6','8'], getXLabels());
278 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
281 // they do affect the legend, however.
283 assertEquals("x2: y:y4", getLegend());
286 AxisLabelsTestCase
.prototype.testDateValueFormatter
= function () {
290 xValueFormatter
: 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');
297 yValueFormatter
: function(y
, opts
, series_name
, dg
) {
298 assertEquals('number', typeof(y
));
299 assertEquals('function', typeof(opts
));
300 assertEquals('string', typeof(series_name
));
301 assertEquals('[Dygraph graph]', dg
.toString());
308 for (var i
= 1; i
< 10; i
++) {
309 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
311 var graph
= document
.getElementById("graph");
312 var g
= new Dygraph(graph
, data
, opts
);
314 // valueFormatters do not affect ticks.
315 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], getXLabels());
316 assertEquals(['2','4','6','8','10','12','14','16','18'], getYLabels());
318 // the valueFormatter options also affect the legend.
320 assertEquals('x2011/01/03: y:y6', getLegend());
323 // This test verifies that when both a valueFormatter and an axisLabelFormatter
324 // are specified, the axisLabelFormatter takes precedence.
325 AxisLabelsTestCase
.prototype.testAxisLabelFormatterPrecedence
= function () {
329 xValueFormatter
: function(x
) {
332 yValueFormatter
: function(y
) {
335 xAxisLabelFormatter
: function(x
, granularity
) {
338 yAxisLabelFormatter
: function(y
) {
344 for (var i
= 0; i
< 10; i
++) {
345 data
.push([i
, 2 * i
]);
347 var graph
= document
.getElementById("graph");
348 var g
= new Dygraph(graph
, data
, opts
);
350 assertEquals(['x0','x2','x4','x6','x8'], getXLabels());
351 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
354 assertEquals("xvf9: y:yvf18", getLegend());
357 // This is the same as the previous test, except that options are added
359 AxisLabelsTestCase
.prototype.testAxisLabelFormatterIncremental
= function () {
366 for (var i
= 0; i
< 10; i
++) {
367 data
.push([i
, 2 * i
]);
369 var graph
= document
.getElementById("graph");
370 var g
= new Dygraph(graph
, data
, opts
);
372 xValueFormatter
: function(x
) {
377 yValueFormatter
: function(y
) {
382 xAxisLabelFormatter
: function(x
, granularity
) {
387 yAxisLabelFormatter
: function(y
) {
392 assertEquals(["x0","x2","x4","x6","x8"], getXLabels());
393 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels());
396 assertEquals("xvf9: y:yvf18", getLegend());
399 AxisLabelsTestCase
.prototype.testGlobalFormatters
= function() {
404 valueFormatter
: function(x
) {
407 axisLabelFormatter
: function(x
) {
412 for (var i
= 0; i
< 10; i
++) {
413 data
.push([i
, 2 * i
]);
415 var graph
= document
.getElementById("graph");
416 var g
= new Dygraph(graph
, data
, opts
);
418 assertEquals(['alf0','alf2','alf4','alf6','alf8'], getXLabels());
419 assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], getYLabels());
422 assertEquals("vf9: y:vf18", getLegend());
425 AxisLabelsTestCase
.prototype.testSeriesOrder
= function() {
430 var data
= "x,00,01,10,11\n" +
431 "0,101,201,301,401\n" +
432 "1,102,202,302,402\n" +
433 "2,103,203,303,403\n" +
434 "3,104,204,304,404\n"
437 var graph
= document
.getElementById("graph");
438 var g
= new Dygraph(graph
, data
, opts
);
441 assertEquals('2: 00:103 01:203 10:303 11:403', getLegend());
443 // Sanity checks for indexFromSetName
444 assertEquals(0, g
.indexFromSetName("x"));
445 assertEquals(1, g
.indexFromSetName("00"));
446 assertEquals(null, g
.indexFromSetName("abcde"));
448 // Verify that we get the label list back in the right order
449 assertEquals(["x", "00", "01", "10", "11"], g
.getLabels());