2 * @fileoverview Test cases for how axis labels are chosen and formatted,
3 * specializing on the deprecated xLabelFormatter, etc.
5 * @author dan@dygraphs.com (Dan Vanderkam)
7 var DeprecatedAxisLabelsTestCase
= TestCase("axis-labels-deprecated");
9 DeprecatedAxisLabelsTestCase
.prototype.setUp
= function() {
10 document
.body
.innerHTML
= "<div id='graph'></div>";
13 DeprecatedAxisLabelsTestCase
.prototype.tearDown
= function() {
16 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedDeprecatedXAxisTimeLabelFormatter
= function() {
21 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]];
22 var graph
= document
.getElementById("graph");
23 var g
= new Dygraph(graph
, data
, opts
);
27 axisLabelFormatter
: function (totalMinutes
) {
28 var hours
= Math
.floor( totalMinutes
/ 60);
29 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
30 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
32 if (hours
< 10) hours
= "0" + hours
;
33 if (minutes
< 10) minutes
= "0" + minutes
;
34 if (seconds
< 10) seconds
= "0" + seconds
;
36 return hours
+ ':' + minutes
+ ':' + seconds
;
42 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
44 // The legend does not use the xAxisLabelFormatter:
46 assertEquals('5.1: Y1: 1', Util
.getLegend());
49 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedAxisLabelFormatter
= function () {
55 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
56 assertEquals('number', typeof(x
));
57 assertEquals('number', typeof(granularity
));
58 assertEquals('function', typeof(opts
));
59 assertEquals('[Dygraph graph]', dg
.toString());
64 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
65 assertEquals('number', typeof(y
));
66 assertEquals('number', typeof(granularity
));
67 assertEquals('function', typeof(opts
));
68 assertEquals('[Dygraph graph]', dg
.toString());
76 for (var i
= 0; i
< 10; i
++) {
77 data
.push([i
, 2 * i
]);
79 var graph
= document
.getElementById("graph");
80 var g
= new Dygraph(graph
, data
, opts
);
82 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
83 assertEquals(["y0","y5","y10","y15"], Util
.getYLabels());
86 assertEquals("2: y: 4", Util
.getLegend());
89 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedDateAxisLabelFormatter
= function () {
95 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
96 assertTrue(Dygraph
.isDateLike(x
));
97 assertEquals('number', typeof(granularity
));
98 assertEquals('function', typeof(opts
));
99 assertEquals('[Dygraph graph]', dg
.toString());
100 return 'x' + Util
.formatDate(x
);
105 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
106 assertEquals('number', typeof(y
));
107 assertEquals('number', typeof(granularity
));
108 assertEquals('function', typeof(opts
));
109 assertEquals('[Dygraph graph]', dg
.toString());
117 for (var i
= 1; i
< 10; i
++) {
118 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
120 var graph
= document
.getElementById("graph");
121 var g
= new Dygraph(graph
, data
, opts
);
123 assertEquals(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util
.getXLabels());
124 assertEquals(["y5","y10","y15"], Util
.getYLabels());
127 assertEquals("2011/01/01: y: 2", Util
.getLegend());
130 // This test verifies that when a valueFormatter is set (but not an
131 // axisLabelFormatter), then the valueFormatter is used to format the axis
133 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedValueFormatter
= function () {
139 valueFormatter
: function(x
, opts
, series_name
, dg
) {
140 assertEquals('number', typeof(x
));
141 assertEquals('function', typeof(opts
));
142 assertEquals('string', typeof(series_name
));
143 assertEquals('[Dygraph graph]', dg
.toString());
148 valueFormatter
: function(y
, opts
, series_name
, dg
) {
149 assertEquals('number', typeof(y
));
150 assertEquals('function', typeof(opts
));
151 assertEquals('string', typeof(series_name
));
152 assertEquals('[Dygraph graph]', dg
.toString());
160 for (var i
= 0; i
< 10; i
++) {
161 data
.push([i
, 2 * i
]);
163 var graph
= document
.getElementById("graph");
164 var g
= new Dygraph(graph
, data
, opts
);
166 // the valueFormatter options do not affect the ticks.
167 assertEquals(['0','2','4','6','8'], Util
.getXLabels());
168 assertEquals(["0","5","10","15"], Util
.getYLabels());
170 // they do affect the legend, however.
172 assertEquals("x2: y: y4", Util
.getLegend());
175 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedDateValueFormatter
= function () {
181 valueFormatter
: function(x
, opts
, series_name
, dg
) {
182 assertEquals('number', typeof(x
));
183 assertEquals('function', typeof(opts
));
184 assertEquals('string', typeof(series_name
));
185 assertEquals('[Dygraph graph]', dg
.toString());
186 return 'x' + Util
.formatDate(x
);
191 valueFormatter
: function(y
, opts
, series_name
, dg
) {
192 assertEquals('number', typeof(y
));
193 assertEquals('function', typeof(opts
));
194 assertEquals('string', typeof(series_name
));
195 assertEquals('[Dygraph graph]', dg
.toString());
204 for (var i
= 1; i
< 10; i
++) {
205 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
207 var graph
= document
.getElementById("graph");
208 var g
= new Dygraph(graph
, data
, opts
);
210 // valueFormatters do not affect ticks.
211 assertEquals(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
212 assertEquals(["5","10","15"], Util
.getYLabels());
214 // the valueFormatter options also affect the legend.
216 assertEquals('x2011/01/03: y: y6', Util
.getLegend());
219 // This test verifies that when both a valueFormatter and an axisLabelFormatter
220 // are specified, the axisLabelFormatter takes precedence.
221 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedAxisLabelFormatterPrecedence
= function () {
227 valueFormatter
: function(x
) {
230 axisLabelFormatter
: function(x
, granularity
) {
235 valueFormatter
: function(y
) {
238 axisLabelFormatter
: function(y
) {
246 for (var i
= 0; i
< 10; i
++) {
247 data
.push([i
, 2 * i
]);
249 var graph
= document
.getElementById("graph");
250 var g
= new Dygraph(graph
, data
, opts
);
252 assertEquals(['x0','x2','x4','x6','x8'], Util
.getXLabels());
253 assertEquals(["y0","y5","y10","y15"], Util
.getYLabels());
256 assertEquals("xvf9: y: yvf18", Util
.getLegend());
259 // This is the same as the previous test, except that options are added
261 DeprecatedAxisLabelsTestCase
.prototype.testDeprecatedAxisLabelFormatterIncremental
= function () {
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
);
276 valueFormatter
: function(x
) {
285 valueFormatter
: function(y
) {
294 axisLabelFormatter
: function(x
) {
303 axisLabelFormatter
: function(y
) {
310 assertEquals(["x0","x2","x4","x6","x8"], Util
.getXLabels());
311 assertEquals(["y0","y5","y10","y15"], Util
.getYLabels());
314 assertEquals("xvf9: y: yvf18", Util
.getLegend());