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() {
16 * Takes in an array of strings and returns an array of floats.
18 function makeNumbers(ary
) {
20 for (var i
= 0; i
< ary
.length
; i
++) {
21 ret
.push(parseFloat(ary
[i
]));
26 AxisLabelsTestCase
.prototype.kCloseFloat
= 1.0e-10;
28 AxisLabelsTestCase
.prototype.testMinusOneToOne
= function() {
40 var graph
= document
.getElementById("graph");
41 var g
= new Dygraph(graph
, data
, opts
);
43 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
44 assertEquals(['-1','-0.5','0','0.5','1'], Util
.getYLabels());
48 g
.updateOptions({file
: data
});
49 assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], Util
.getYLabels());
53 g
.updateOptions({file
: data
});
54 assertEquals(['-2','0','2','4','6','8','10'], Util
.getYLabels());
58 g
.updateOptions({file
: data
});
59 assertEquals(['0','20','40','60','80','100'], Util
.getYLabels());
62 assertEquals('0: Y:-1', Util
.getLegend());
65 AxisLabelsTestCase
.prototype.testSmallRangeNearZero
= function() {
77 opts
.valueRange
= [-0.1, 0.1];
79 var graph
= document
.getElementById("graph");
80 var g
= new Dygraph(graph
, data
, opts
);
81 assertEqualsDelta(makeNumbers(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"]),
82 makeNumbers(Util
.getYLabels()), this.kCloseFloat
);
84 opts
.valueRange
= [-0.05, 0.05];
85 g
.updateOptions(opts
);
86 // TODO(danvk): why '1.00e-2' and not '0.01'?
87 assertEquals(makeNumbers(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"]),
88 makeNumbers(Util
.getYLabels()));
90 opts
.valueRange
= [-0.01, 0.01];
91 g
.updateOptions(opts
);
92 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(Util
.getYLabels()));
95 assertEquals('1: Y:0', Util
.getLegend());
98 AxisLabelsTestCase
.prototype.testSmallRangeAwayFromZero
= function() {
109 var graph
= document
.getElementById("graph");
111 opts
.valueRange
= [9.9, 10.1];
112 var g
= new Dygraph(graph
, data
, opts
);
113 assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], Util
.getYLabels());
115 opts
.valueRange
= [9.99, 10.01];
116 g
.updateOptions(opts
);
117 // TODO(danvk): this is bad
118 assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], Util
.getYLabels());
120 opts
.valueRange
= [9.999, 10.001];
121 g
.updateOptions(opts
);
122 // TODO(danvk): this is even worse!
123 assertEquals(["10","10","10","10","10","10","10","10","10","10"], Util
.getYLabels());
126 assertEquals('1: Y:0', Util
.getLegend());
129 AxisLabelsTestCase
.prototype.testXAxisTimeLabelFormatter
= function() {
134 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]];
135 var graph
= document
.getElementById("graph");
136 var g
= new Dygraph(graph
, data
, opts
);
140 axisLabelFormatter
: function (totalMinutes
) {
141 var hours
= Math
.floor( totalMinutes
/ 60);
142 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
143 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
145 if (hours
< 10) hours
= "0" + hours
;
146 if (minutes
< 10) minutes
= "0" + minutes
;
147 if (seconds
< 10) seconds
= "0" + seconds
;
149 return hours
+ ':' + minutes
+ ':' + seconds
;
155 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
157 // The legend does not use the axisLabelFormatter:
159 assertEquals('5.1: Y1:1', Util
.getLegend());
162 AxisLabelsTestCase
.prototype.testAxisLabelFormatter
= function () {
168 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
169 assertEquals('number', typeof(x
));
170 assertEquals('number', typeof(granularity
));
171 assertEquals('function', typeof(opts
));
172 assertEquals('[Dygraph graph]', dg
.toString());
177 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
178 assertEquals('number', typeof(y
));
179 assertEquals('number', typeof(granularity
));
180 assertEquals('function', typeof(opts
));
181 assertEquals('[Dygraph graph]', dg
.toString());
189 for (var i
= 0; i
< 10; i
++) {
190 data
.push([i
, 2 * i
]);
192 var graph
= document
.getElementById("graph");
193 var g
= new Dygraph(graph
, data
, opts
);
195 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
196 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
199 assertEquals("2: y:4", Util
.getLegend());
202 AxisLabelsTestCase
.prototype.testDateAxisLabelFormatter
= function () {
208 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
209 assertTrue(Dygraph
.isDateLike(x
));
210 assertEquals('number', typeof(granularity
));
211 assertEquals('function', typeof(opts
));
212 assertEquals('[Dygraph graph]', dg
.toString());
213 return 'x' + x
.strftime('%Y/%m/%d');
217 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
218 assertEquals('number', typeof(y
));
219 assertEquals('number', typeof(granularity
));
220 assertEquals('function', typeof(opts
));
221 assertEquals('[Dygraph graph]', dg
.toString());
229 for (var i
= 1; i
< 10; i
++) {
230 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
232 var graph
= document
.getElementById("graph");
233 var g
= new Dygraph(graph
, data
, opts
);
235 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());
236 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
239 assertEquals("2011/01/01: y:2", Util
.getLegend());
242 // This test verifies that when a valueFormatter is set (but not an
243 // axisLabelFormatter), then the valueFormatter is used to format the axis
245 AxisLabelsTestCase
.prototype.testValueFormatter
= function () {
251 valueFormatter
: function(x
, opts
, series_name
, dg
) {
252 assertEquals('number', typeof(x
));
253 assertEquals('function', typeof(opts
));
254 assertEquals('string', typeof(series_name
));
255 assertEquals('[Dygraph graph]', dg
.toString());
260 valueFormatter
: 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());
272 for (var i
= 0; i
< 10; i
++) {
273 data
.push([i
, 2 * i
]);
275 var graph
= document
.getElementById("graph");
276 var g
= new Dygraph(graph
, data
, opts
);
278 // the valueFormatter options do not affect the ticks.
279 assertEquals(['0','2','4','6','8'], Util
.getXLabels());
280 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
283 // they do affect the legend, however.
285 assertEquals("x2: y:y4", Util
.getLegend());
288 AxisLabelsTestCase
.prototype.testDateValueFormatter
= function () {
294 valueFormatter
: function(x
, opts
, series_name
, dg
) {
295 assertEquals('number', typeof(x
));
296 assertEquals('function', typeof(opts
));
297 assertEquals('string', typeof(series_name
));
298 assertEquals('[Dygraph graph]', dg
.toString());
299 return 'x' + new Date(x
).strftime('%Y/%m/%d');
303 valueFormatter
: function(y
, opts
, series_name
, dg
) {
304 assertEquals('number', typeof(y
));
305 assertEquals('function', typeof(opts
));
306 assertEquals('string', typeof(series_name
));
307 assertEquals('[Dygraph graph]', dg
.toString());
316 for (var i
= 1; i
< 10; i
++) {
317 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
319 var graph
= document
.getElementById("graph");
320 var g
= new Dygraph(graph
, data
, opts
);
322 // valueFormatters do not affect ticks.
323 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], Util
.getXLabels());
324 assertEquals(['2','4','6','8','10','12','14','16','18'], Util
.getYLabels());
326 // the valueFormatter options also affect the legend.
328 assertEquals('x2011/01/03: y:y6', Util
.getLegend());
331 // This test verifies that when both a valueFormatter and an axisLabelFormatter
332 // are specified, the axisLabelFormatter takes precedence.
333 AxisLabelsTestCase
.prototype.testAxisLabelFormatterPrecedence
= function () {
339 valueFormatter
: function(x
) {
342 axisLabelFormatter
: function(x
, granularity
) {
347 valueFormatter
: function(y
) {
350 axisLabelFormatter
: function(y
) {
358 for (var i
= 0; i
< 10; i
++) {
359 data
.push([i
, 2 * i
]);
361 var graph
= document
.getElementById("graph");
362 var g
= new Dygraph(graph
, data
, opts
);
364 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
365 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
368 assertEquals("xvf9: y:yvf18", Util
.getLegend());
371 // This is the same as the previous test, except that options are added
373 AxisLabelsTestCase
.prototype.testAxisLabelFormatterIncremental
= function () {
380 for (var i
= 0; i
< 10; i
++) {
381 data
.push([i
, 2 * i
]);
383 var graph
= document
.getElementById("graph");
384 var g
= new Dygraph(graph
, data
, opts
);
388 valueFormatter
: function(x
) {
397 valueFormatter
: function(y
) {
406 axisLabelFormatter
: function(x
, granularity
) {
415 axisLabelFormatter
: function(y
) {
422 assertEquals(["x0","x2","x4","x6","x8"], Util
.getXLabels());
423 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util
.getYLabels());
426 assertEquals("xvf9: y:yvf18", Util
.getLegend());
429 AxisLabelsTestCase
.prototype.testGlobalFormatters
= function() {
434 valueFormatter
: function(x
) {
437 axisLabelFormatter
: function(x
) {
442 for (var i
= 0; i
< 10; i
++) {
443 data
.push([i
, 2 * i
]);
445 var graph
= document
.getElementById("graph");
446 var g
= new Dygraph(graph
, data
, opts
);
448 assertEquals(['alf0','alf2','alf4','alf6','alf8'], Util
.getXLabels());
449 assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], Util
.getYLabels());
452 assertEquals("vf9: y:vf18", Util
.getLegend());
455 AxisLabelsTestCase
.prototype.testSeriesOrder
= function() {
460 var data
= "x,00,01,10,11\n" +
461 "0,101,201,301,401\n" +
462 "1,102,202,302,402\n" +
463 "2,103,203,303,403\n" +
464 "3,104,204,304,404\n"
467 var graph
= document
.getElementById("graph");
468 var g
= new Dygraph(graph
, data
, opts
);
471 assertEquals('2: 00:103 01:203 10:303 11:403', Util
.getLegend());
473 // Sanity checks for indexFromSetName
474 assertEquals(0, g
.indexFromSetName("x"));
475 assertEquals(1, g
.indexFromSetName("00"));
476 assertEquals(null, g
.indexFromSetName("abcde"));
478 // Verify that we get the label list back in the right order
479 assertEquals(["x", "00", "01", "10", "11"], g
.getLabels());