Merge branch 'master' of github.com:kberg/dygraphs
[dygraphs.git] / auto_tests / tests / axis_labels-deprecated.js
CommitLineData
48e614ac 1/**
6d6c60b6
RK
2 * @fileoverview Test cases for how axis labels are chosen and formatted,
3 * specializing on the deprecated xLabelFormatter, etc.
f3cbe61e
DV
4 *
5 * @author dan@dygraphs.com (Dan Vanderkam)
6 */
6d6c60b6 7var DeprecatedAxisLabelsTestCase = TestCase("axis-labels-deprecated");
f3cbe61e 8
6d6c60b6 9DeprecatedAxisLabelsTestCase.prototype.setUp = function() {
f3cbe61e
DV
10 document.body.innerHTML = "<div id='graph'></div>";
11};
12
6d6c60b6 13DeprecatedAxisLabelsTestCase.prototype.tearDown = function() {
f3cbe61e
DV
14};
15
8fcf1b16 16DeprecatedAxisLabelsTestCase.prototype.testDeprecatedDeprecatedXAxisTimeLabelFormatter = function() {
ad69cb8a
DV
17 var opts = {
18 width: 480,
19 height: 320
20 };
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);
24 g.updateOptions({
25 xAxisLabelFormatter: function (totalMinutes) {
26 var hours = Math.floor( totalMinutes / 60);
27 var minutes = Math.floor((totalMinutes - (hours * 60)));
28 var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60));
29
30 if (hours < 10) hours = "0" + hours;
31 if (minutes < 10) minutes = "0" + minutes;
32 if (seconds < 10) seconds = "0" + seconds;
33
34 return hours + ':' + minutes + ':' + seconds;
35 }
36 });
97889da4 37
846038aa 38 assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util.getXLabels());
48e614ac
DV
39
40 // The legend does not use the xAxisLabelFormatter:
41 g.setSelection(1);
846038aa 42 assertEquals('5.1: Y1:1', Util.getLegend());
48e614ac
DV
43};
44
8fcf1b16 45DeprecatedAxisLabelsTestCase.prototype.testDeprecatedAxisLabelFormatter = function () {
48e614ac
DV
46 var opts = {
47 width: 480,
48 height: 320,
49 xAxisLabelFormatter: function(x, granularity, opts, dg) {
50 assertEquals('number', typeof(x));
51 assertEquals('number', typeof(granularity));
52 assertEquals('function', typeof(opts));
53 assertEquals('[Dygraph graph]', dg.toString());
54 return 'x' + x;
55 },
56 yAxisLabelFormatter: function(y, granularity, opts, dg) {
57 assertEquals('number', typeof(y));
58 assertEquals('number', typeof(granularity));
59 assertEquals('function', typeof(opts));
60 assertEquals('[Dygraph graph]', dg.toString());
61 return 'y' + y;
62 },
63 labels: ['x', 'y']
64 };
65 var data = [];
66 for (var i = 0; i < 10; i++) {
67 data.push([i, 2 * i]);
68 }
69 var graph = document.getElementById("graph");
70 var g = new Dygraph(graph, data, opts);
71
846038aa
RK
72 assertEquals(['x0','x2','x4','x6','x8'], Util.getXLabels());
73 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util.getYLabels());
48e614ac
DV
74
75 g.setSelection(2);
846038aa 76 assertEquals("2: y:4", Util.getLegend());
48e614ac
DV
77};
78
8fcf1b16 79DeprecatedAxisLabelsTestCase.prototype.testDeprecatedDateAxisLabelFormatter = function () {
48e614ac
DV
80 var opts = {
81 width: 480,
82 height: 320,
83 xAxisLabelFormatter: function(x, granularity, opts, dg) {
84 assertTrue(Dygraph.isDateLike(x));
85 assertEquals('number', typeof(granularity));
86 assertEquals('function', typeof(opts));
87 assertEquals('[Dygraph graph]', dg.toString());
88 return 'x' + x.strftime('%Y/%m/%d');
89 },
90 yAxisLabelFormatter: function(y, granularity, opts, dg) {
91 assertEquals('number', typeof(y));
92 assertEquals('number', typeof(granularity));
93 assertEquals('function', typeof(opts));
94 assertEquals('[Dygraph graph]', dg.toString());
95 return 'y' + y;
96 },
97 labels: ['x', 'y']
98 };
99 var data = [];
100 for (var i = 1; i < 10; i++) {
101 data.push([new Date("2011/01/0" + i), 2 * i]);
102 }
103 var graph = document.getElementById("graph");
104 var g = new Dygraph(graph, data, opts);
105
846038aa
RK
106 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());
107 assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util.getYLabels());
48e614ac
DV
108
109 g.setSelection(0);
846038aa 110 assertEquals("2011/01/01: y:2", Util.getLegend());
48e614ac
DV
111};
112
113// This test verifies that when a valueFormatter is set (but not an
114// axisLabelFormatter), then the valueFormatter is used to format the axis
115// labels.
8fcf1b16 116DeprecatedAxisLabelsTestCase.prototype.testDeprecatedValueFormatter = function () {
48e614ac
DV
117 var opts = {
118 width: 480,
119 height: 320,
120 xValueFormatter: function(x, opts, series_name, dg) {
121 assertEquals('number', typeof(x));
122 assertEquals('function', typeof(opts));
123 assertEquals('string', typeof(series_name));
124 assertEquals('[Dygraph graph]', dg.toString());
125 return 'x' + x;
126 },
127 yValueFormatter: function(y, opts, series_name, dg) {
128 assertEquals('number', typeof(y));
129 assertEquals('function', typeof(opts));
130 assertEquals('string', typeof(series_name));
131 assertEquals('[Dygraph graph]', dg.toString());
132 return 'y' + y;
133 },
134 labels: ['x', 'y']
135 };
136 var data = [];
137 for (var i = 0; i < 10; i++) {
138 data.push([i, 2 * i]);
139 }
140 var graph = document.getElementById("graph");
141 var g = new Dygraph(graph, data, opts);
142
143 // the valueFormatter options do not affect the ticks.
846038aa 144 assertEquals(['0','2','4','6','8'], Util.getXLabels());
48e614ac 145 assertEquals(['0','2','4','6','8','10','12','14','16','18'],
846038aa 146 Util.getYLabels());
48e614ac
DV
147
148 // they do affect the legend, however.
149 g.setSelection(2);
846038aa 150 assertEquals("x2: y:y4", Util.getLegend());
48e614ac
DV
151};
152
8fcf1b16 153DeprecatedAxisLabelsTestCase.prototype.testDeprecatedDateValueFormatter = function () {
48e614ac
DV
154 var opts = {
155 width: 480,
156 height: 320,
157 xValueFormatter: function(x, opts, series_name, dg) {
158 assertEquals('number', typeof(x));
159 assertEquals('function', typeof(opts));
160 assertEquals('string', typeof(series_name));
161 assertEquals('[Dygraph graph]', dg.toString());
162 return 'x' + new Date(x).strftime('%Y/%m/%d');
163 },
164 yValueFormatter: function(y, opts, series_name, dg) {
165 assertEquals('number', typeof(y));
166 assertEquals('function', typeof(opts));
167 assertEquals('string', typeof(series_name));
168 assertEquals('[Dygraph graph]', dg.toString());
169 return 'y' + y;
170 },
171 labels: ['x', 'y']
172 };
173
174 var data = [];
175 for (var i = 1; i < 10; i++) {
176 data.push([new Date("2011/01/0" + i), 2 * i]);
177 }
178 var graph = document.getElementById("graph");
179 var g = new Dygraph(graph, data, opts);
180
181 // valueFormatters do not affect ticks.
846038aa
RK
182 assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], Util.getXLabels());
183 assertEquals(['2','4','6','8','10','12','14','16','18'], Util.getYLabels());
48e614ac
DV
184
185 // the valueFormatter options also affect the legend.
186 g.setSelection(2);
846038aa 187 assertEquals('x2011/01/03: y:y6', Util.getLegend());
48e614ac
DV
188};
189
190// This test verifies that when both a valueFormatter and an axisLabelFormatter
191// are specified, the axisLabelFormatter takes precedence.
8fcf1b16 192DeprecatedAxisLabelsTestCase.prototype.testDeprecatedAxisLabelFormatterPrecedence = function () {
48e614ac
DV
193 var opts = {
194 width: 480,
195 height: 320,
196 xValueFormatter: function(x) {
197 return 'xvf' + x;
198 },
199 yValueFormatter: function(y) {
200 return 'yvf' + y;
201 },
202 xAxisLabelFormatter: function(x, granularity) {
203 return 'x' + x;
204 },
205 yAxisLabelFormatter: function(y) {
206 return 'y' + y;
207 },
208 labels: ['x', 'y']
209 };
210 var data = [];
211 for (var i = 0; i < 10; i++) {
212 data.push([i, 2 * i]);
213 }
214 var graph = document.getElementById("graph");
215 var g = new Dygraph(graph, data, opts);
216
846038aa
RK
217 assertEquals(['x0','x2','x4','x6','x8'], Util.getXLabels());
218 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util.getYLabels());
48e614ac
DV
219
220 g.setSelection(9);
846038aa 221 assertEquals("xvf9: y:yvf18", Util.getLegend());
48e614ac
DV
222};
223
224// This is the same as the previous test, except that options are added
225// one-by-one.
8fcf1b16 226DeprecatedAxisLabelsTestCase.prototype.testDeprecatedAxisLabelFormatterIncremental = function () {
48e614ac
DV
227 var opts = {
228 width: 480,
229 height: 320,
230 labels: ['x', 'y']
231 };
232 var data = [];
233 for (var i = 0; i < 10; i++) {
234 data.push([i, 2 * i]);
235 }
236 var graph = document.getElementById("graph");
237 var g = new Dygraph(graph, data, opts);
238 g.updateOptions({
239 xValueFormatter: function(x) {
240 return 'xvf' + x;
241 }
242 });
243 g.updateOptions({
244 yValueFormatter: function(y) {
245 return 'yvf' + y;
246 }
247 });
248 g.updateOptions({
249 xAxisLabelFormatter: function(x, granularity) {
250 return 'x' + x;
251 }
252 });
253 g.updateOptions({
254 yAxisLabelFormatter: function(y) {
255 return 'y' + y;
256 }
257 });
258
846038aa
RK
259 assertEquals(["x0","x2","x4","x6","x8"], Util.getXLabels());
260 assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], Util.getYLabels());
ad69cb8a 261
48e614ac 262 g.setSelection(9);
846038aa 263 assertEquals("xvf9: y:yvf18", Util.getLegend());
ad69cb8a 264};