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 describe("axis-labels-deprecated", function() {
9 beforeEach(function() {
10 document
.body
.innerHTML
= "<div id='graph'></div>";
13 afterEach(function() {
16 it('testDeprecatedDeprecatedXAxisTimeLabelFormatter', function() {
22 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]];
23 var graph
= document
.getElementById("graph");
24 var g
= new Dygraph(graph
, data
, opts
);
28 axisLabelFormatter
: function (totalMinutes
) {
29 var hours
= Math
.floor( totalMinutes
/ 60);
30 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
31 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
33 if (hours
< 10) hours
= "0" + hours
;
34 if (minutes
< 10) minutes
= "0" + minutes
;
35 if (seconds
< 10) seconds
= "0" + seconds
;
37 return hours
+ ':' + minutes
+ ':' + seconds
;
43 assert
.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
45 // The legend does not use the xAxisLabelFormatter:
47 assert
.equal('5.1: Y1: 1', Util
.getLegend());
50 it('testDeprecatedAxisLabelFormatter', function() {
56 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
57 assert
.equal('number', typeof(x
));
58 assert
.equal('number', typeof(granularity
));
59 assert
.equal('function', typeof(opts
));
60 assert
.equal('[Dygraph graph]', dg
.toString());
65 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
66 assert
.equal('number', typeof(y
));
67 assert
.equal('number', typeof(granularity
));
68 assert
.equal('function', typeof(opts
));
69 assert
.equal('[Dygraph graph]', dg
.toString());
77 for (var i
= 0; i
< 10; i
++) {
78 data
.push([i
, 2 * i
]);
80 var graph
= document
.getElementById("graph");
81 var g
= new Dygraph(graph
, data
, opts
);
83 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
84 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
87 assert
.equal("2: y: 4", Util
.getLegend());
90 it('testDeprecatedDateAxisLabelFormatter', function() {
96 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
97 assert
.isTrue(Dygraph
.isDateLike(x
));
98 assert
.equal('number', typeof(granularity
));
99 assert
.equal('function', typeof(opts
));
100 assert
.equal('[Dygraph graph]', dg
.toString());
101 return 'x' + Util
.formatDate(x
);
106 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
107 assert
.equal('number', typeof(y
));
108 assert
.equal('number', typeof(granularity
));
109 assert
.equal('function', typeof(opts
));
110 assert
.equal('[Dygraph graph]', dg
.toString());
118 for (var i
= 1; i
< 10; i
++) {
119 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
121 var graph
= document
.getElementById("graph");
122 var g
= new Dygraph(graph
, data
, opts
);
124 assert
.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util
.getXLabels());
125 assert
.deepEqual(["y5","y10","y15"], Util
.getYLabels());
128 assert
.equal("2011/01/01: y: 2", Util
.getLegend());
131 // This test verifies that when a valueFormatter is set (but not an
132 // axisLabelFormatter), then the valueFormatter is used to format the axis
134 it('testDeprecatedValueFormatter', function() {
140 valueFormatter
: function(x
, opts
, series_name
, dg
) {
141 assert
.equal('number', typeof(x
));
142 assert
.equal('function', typeof(opts
));
143 assert
.equal('string', typeof(series_name
));
144 assert
.equal('[Dygraph graph]', dg
.toString());
149 valueFormatter
: function(y
, opts
, series_name
, dg
) {
150 assert
.equal('number', typeof(y
));
151 assert
.equal('function', typeof(opts
));
152 assert
.equal('string', typeof(series_name
));
153 assert
.equal('[Dygraph graph]', dg
.toString());
161 for (var i
= 0; i
< 10; i
++) {
162 data
.push([i
, 2 * i
]);
164 var graph
= document
.getElementById("graph");
165 var g
= new Dygraph(graph
, data
, opts
);
167 // the valueFormatter options do not affect the ticks.
168 assert
.deepEqual(['0','2','4','6','8'], Util
.getXLabels());
169 assert
.deepEqual(["0","5","10","15"], Util
.getYLabels());
171 // they do affect the legend, however.
173 assert
.equal("x2: y: y4", Util
.getLegend());
176 it('testDeprecatedDateValueFormatter', function() {
182 valueFormatter
: function(x
, opts
, series_name
, dg
) {
183 assert
.equal('number', typeof(x
));
184 assert
.equal('function', typeof(opts
));
185 assert
.equal('string', typeof(series_name
));
186 assert
.equal('[Dygraph graph]', dg
.toString());
187 return 'x' + Util
.formatDate(x
);
192 valueFormatter
: function(y
, opts
, series_name
, dg
) {
193 assert
.equal('number', typeof(y
));
194 assert
.equal('function', typeof(opts
));
195 assert
.equal('string', typeof(series_name
));
196 assert
.equal('[Dygraph graph]', dg
.toString());
205 for (var i
= 1; i
< 10; i
++) {
206 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
208 var graph
= document
.getElementById("graph");
209 var g
= new Dygraph(graph
, data
, opts
);
211 // valueFormatters do not affect ticks.
212 assert
.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
213 assert
.deepEqual(["5","10","15"], Util
.getYLabels());
215 // the valueFormatter options also affect the legend.
217 assert
.equal('x2011/01/03: y: y6', Util
.getLegend());
220 // This test verifies that when both a valueFormatter and an axisLabelFormatter
221 // are specified, the axisLabelFormatter takes precedence.
222 it('testDeprecatedAxisLabelFormatterPrecedence', function() {
228 valueFormatter
: function(x
) {
231 axisLabelFormatter
: function(x
, granularity
) {
236 valueFormatter
: function(y
) {
239 axisLabelFormatter
: function(y
) {
247 for (var i
= 0; i
< 10; i
++) {
248 data
.push([i
, 2 * i
]);
250 var graph
= document
.getElementById("graph");
251 var g
= new Dygraph(graph
, data
, opts
);
253 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
254 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
257 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
260 // This is the same as the previous test, except that options are added
262 it('testDeprecatedAxisLabelFormatterIncremental', function() {
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
);
277 valueFormatter
: function(x
) {
286 valueFormatter
: function(y
) {
295 axisLabelFormatter
: function(x
) {
304 axisLabelFormatter
: function(y
) {
311 assert
.deepEqual(["x0","x2","x4","x6","x8"], Util
.getXLabels());
312 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
315 assert
.equal("xvf9: y: yvf18", Util
.getLegend());