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() {
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 assert
.deepEqual(["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 assert
.equal('5.1: Y1: 1', Util
.getLegend());
49 it('testDeprecatedAxisLabelFormatter', function() {
55 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
56 assert
.equal('number', typeof(x
));
57 assert
.equal('number', typeof(granularity
));
58 assert
.equal('function', typeof(opts
));
59 assert
.equal('[Dygraph graph]', dg
.toString());
64 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
65 assert
.equal('number', typeof(y
));
66 assert
.equal('number', typeof(granularity
));
67 assert
.equal('function', typeof(opts
));
68 assert
.equal('[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 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
83 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
86 assert
.equal("2: y: 4", Util
.getLegend());
89 it('testDeprecatedDateAxisLabelFormatter', function() {
95 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
96 assert
.isTrue(Dygraph
.isDateLike(x
));
97 assert
.equal('number', typeof(granularity
));
98 assert
.equal('function', typeof(opts
));
99 assert
.equal('[Dygraph graph]', dg
.toString());
100 return 'x' + Util
.formatDate(x
);
105 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
106 assert
.equal('number', typeof(y
));
107 assert
.equal('number', typeof(granularity
));
108 assert
.equal('function', typeof(opts
));
109 assert
.equal('[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 assert
.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util
.getXLabels());
124 assert
.deepEqual(["y5","y10","y15"], Util
.getYLabels());
127 assert
.equal("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 it('testDeprecatedValueFormatter', function() {
139 valueFormatter
: function(x
, opts
, series_name
, dg
) {
140 assert
.equal('number', typeof(x
));
141 assert
.equal('function', typeof(opts
));
142 assert
.equal('string', typeof(series_name
));
143 assert
.equal('[Dygraph graph]', dg
.toString());
148 valueFormatter
: function(y
, opts
, series_name
, dg
) {
149 assert
.equal('number', typeof(y
));
150 assert
.equal('function', typeof(opts
));
151 assert
.equal('string', typeof(series_name
));
152 assert
.equal('[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 assert
.deepEqual(['0','2','4','6','8'], Util
.getXLabels());
168 assert
.deepEqual(["0","5","10","15"], Util
.getYLabels());
170 // they do affect the legend, however.
172 assert
.equal("x2: y: y4", Util
.getLegend());
175 it('testDeprecatedDateValueFormatter', function() {
181 valueFormatter
: function(x
, opts
, series_name
, dg
) {
182 assert
.equal('number', typeof(x
));
183 assert
.equal('function', typeof(opts
));
184 assert
.equal('string', typeof(series_name
));
185 assert
.equal('[Dygraph graph]', dg
.toString());
186 return 'x' + Util
.formatDate(x
);
191 valueFormatter
: function(y
, opts
, series_name
, dg
) {
192 assert
.equal('number', typeof(y
));
193 assert
.equal('function', typeof(opts
));
194 assert
.equal('string', typeof(series_name
));
195 assert
.equal('[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 assert
.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
212 assert
.deepEqual(["5","10","15"], Util
.getYLabels());
214 // the valueFormatter options also affect the legend.
216 assert
.equal('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 it('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 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
253 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
256 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
259 // This is the same as the previous test, except that options are added
261 it('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 assert
.deepEqual(["x0","x2","x4","x6","x8"], Util
.getXLabels());
311 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
314 assert
.equal("xvf9: y: yvf18", Util
.getLegend());