Regression test for issue #411
[dygraphs.git] / auto_tests / tests / multiple_axes-old.js
CommitLineData
48e614ac
DV
1/**
2 * @fileoverview Tests involving multiple y-axes.
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
6
850fb736 7var MultipleAxesOldTestCase = TestCase("multiple-axes-old-tests");
48e614ac 8
850fb736 9MultipleAxesOldTestCase.prototype.setUp = function() {
48e614ac
DV
10 document.body.innerHTML = "<div id='graph'></div>";
11};
12
850fb736 13MultipleAxesOldTestCase.getData = function() {
48e614ac
DV
14 var data = [];
15 for (var i = 1; i <= 100; i++) {
16 var m = "01", d = i;
17 if (d > 31) { m = "02"; d -= 31; }
18 if (m == "02" && d > 28) { m = "03"; d -= 28; }
19 if (m == "03" && d > 31) { m = "04"; d -= 31; }
20 if (d < 10) d = "0" + d;
21 // two series, one with range 1-100, one with range 1-2M
22 data.push([new Date("2010/" + m + "/" + d),
23 i,
24 100 - i,
25 1e6 * (1 + i * (100 - i) / (50 * 50)),
26 1e6 * (2 - i * (100 - i) / (50 * 50))]);
27 }
28 return data;
29};
30
850fb736 31MultipleAxesOldTestCase.prototype.testOldBasicMultipleAxes = function() {
48e614ac
DV
32 var data = MultipleAxesTestCase.getData();
33
107f9d8e 34 var g = new Dygraph(
48e614ac
DV
35 document.getElementById("graph"),
36 data,
37 {
38 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
39 width: 640,
40 height: 350,
41 'Y3': {
42 axis: {
43 // set axis-related properties here
44 labelsKMB: true
45 }
46 },
47 'Y4': {
48 axis: 'Y3' // use the same y-axis as series Y3
49 }
50 }
51 );
52
fa607ffb
RK
53 assertEquals(["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"], Util.getYLabels("1"));
54 assertEquals(["900K", "1.01M", "1.12M", "1.23M", "1.34M", "1.45M", "1.55M", "1.66M", "1.77M", "1.88M", "1.99M"], Util.getYLabels("2"));
48e614ac
DV
55};
56
850fb736 57MultipleAxesOldTestCase.prototype.testOldNewStylePerAxisOptions = function() {
48e614ac
DV
58 var data = MultipleAxesTestCase.getData();
59
107f9d8e 60 var g = new Dygraph(
48e614ac
DV
61 document.getElementById("graph"),
62 data,
63 {
64 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
65 width: 640,
66 height: 350,
67 'Y3': {
68 axis: { }
69 },
70 'Y4': {
71 axis: 'Y3' // use the same y-axis as series Y3
72 },
73 axes: {
74 y2: {
75 labelsKMB: true
76 }
77 }
78 }
79 );
80
fa607ffb
RK
81 assertEquals(["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"], Util.getYLabels("1"));
82 assertEquals(["900K", "1.01M", "1.12M", "1.23M", "1.34M", "1.45M", "1.55M", "1.66M", "1.77M", "1.88M", "1.99M"], Util.getYLabels("2"));
48e614ac 83};
015b31df 84
850fb736 85MultipleAxesOldTestCase.prototype.testOldMultiAxisLayout = function() {
015b31df
AV
86 var data = MultipleAxesTestCase.getData();
87
88 var el = document.getElementById("graph");
89
107f9d8e 90 var g = new Dygraph(
015b31df
AV
91 el,
92 data,
93 {
94 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
95 width: 640,
96 height: 350,
97 'Y3': {
98 axis: { }
99 },
100 'Y4': {
101 axis: 'Y3' // use the same y-axis as series Y3
102 },
103 axes: {
104 y2: {
105 labelsKMB: true
106 }
107 }
108 }
109 );
110
111 // Test that all elements are inside the bounds of the graph, set above
112 var innerDiv = el.firstChild;
113 for (var child = innerDiv.firstChild; child != null; child = child.nextSibling) {
114 assertTrue(child.offsetLeft >= 0);
115 assertTrue((child.offsetLeft + child.offsetWidth) <= 640);
116 assertTrue(child.offsetTop >= 0);
117 // TODO(flooey@google.com): Text sometimes linebreaks,
118 // causing the labels to appear outside the allocated area.
119 // assertTrue((child.offsetTop + child.offsetHeight) <= 350);
120 }
121};
a2da3777 122
850fb736 123MultipleAxesOldTestCase.prototype.testOldTwoAxisVisibility = function() {
a2da3777
DV
124 var data = [];
125 data.push([0,0,0]);
126 data.push([1,2,2000]);
127 data.push([2,4,1000]);
128
395e98a3 129 var g = new Dygraph(
a2da3777
DV
130 document.getElementById("graph"),
131 data,
132 {
133 labels: [ 'X', 'bar', 'zot' ],
134 'zot': {
135 axis: {
136 labelsKMB: true
137 }
138 }
139 }
140 );
141
142 assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0);
143 assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0);
144
145 g.setVisibility(0, false);
146
147 assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0);
148 assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0);
149
150 g.setVisibility(0, true);
151 g.setVisibility(1, false);
152
153 assertTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0);
154 assertTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0);
155};
d0c39108
DV
156
157// verifies that all four chart labels (title, x-, y-, y2-axis label) can be
158// used simultaneously.
850fb736 159MultipleAxesOldTestCase.prototype.testOldMultiChartLabels = function() {
d0c39108
DV
160 var data = MultipleAxesTestCase.getData();
161
162 var el = document.getElementById("graph");
163 el.style.border = '1px solid black';
164 el.style.marginLeft = '200px';
165 el.style.marginTop = '200px';
166
107f9d8e 167 var g = new Dygraph(
d0c39108
DV
168 el,
169 data,
170 {
171 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
172 width: 640,
173 height: 350,
174 'Y3': {
175 axis: { }
176 },
177 'Y4': {
178 axis: 'Y3' // use the same y-axis as series Y3
179 },
180 xlabel: 'x-axis',
181 ylabel: 'y-axis',
182 y2label: 'y2-axis',
183 title: 'Chart title'
184 }
185 );
186
d0c39108 187 assertEquals(["Chart title", "x-axis", "y-axis", "y2-axis"],
fa607ffb
RK
188 Util.getClassTexts("dygraph-label"));
189 assertEquals(["Chart title"], Util.getClassTexts("dygraph-title"));
190 assertEquals(["x-axis"], Util.getClassTexts("dygraph-xlabel"));
191 assertEquals(["y-axis"], Util.getClassTexts("dygraph-ylabel"));
192 assertEquals(["y2-axis"], Util.getClassTexts("dygraph-y2label"));
d0c39108
DV
193
194 // TODO(danvk): check relative positioning here: title on top, y left of y2.
195};
107f9d8e
DV
196
197// Check that a chart w/o a secondary y-axis will not get a y2label, even if one
198// is specified.
850fb736 199MultipleAxesOldTestCase.prototype.testOldNoY2LabelWithoutSecondaryAxis = function() {
107f9d8e
DV
200 var g = new Dygraph(
201 document.getElementById("graph"),
202 MultipleAxesTestCase.getData(),
203 {
204 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
205 width: 640,
206 height: 350,
207 xlabel: 'x-axis',
208 ylabel: 'y-axis',
209 y2label: 'y2-axis',
210 title: 'Chart title'
211 }
212 );
213
214 assertEquals(["Chart title", "x-axis", "y-axis"],
fa607ffb
RK
215 Util.getClassTexts("dygraph-label"));
216 assertEquals(["Chart title"], Util.getClassTexts("dygraph-title"));
217 assertEquals(["x-axis"], Util.getClassTexts("dygraph-xlabel"));
218 assertEquals(["y-axis"], Util.getClassTexts("dygraph-ylabel"));
219 assertEquals([], Util.getClassTexts("dygraph-y2label"));
107f9d8e 220};
4dd0ac55 221
850fb736 222MultipleAxesOldTestCase.prototype.testOldValueRangePerAxisOptions = function() {
4dd0ac55
RV
223 var data = MultipleAxesTestCase.getData();
224
225 g = new Dygraph(
226 document.getElementById("graph"),
227 data,
228 {
229 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
230 'Y3': {
231 axis: {
232 }
233 },
234 'Y4': {
235 axis: 'Y3' // use the same y-axis as series Y3
236 },
237 axes: {
238 y: {
239 valueRange: [40, 70]
240 },
241 y2: {
242 // set axis-related properties here
243 labelsKMB: true
244 }
245 },
246 ylabel: 'Primary y-axis',
247 y2label: 'Secondary y-axis',
248 yAxisLabelWidth: 60
249 }
250 );
fa607ffb
RK
251 assertEquals(["40", "45", "50", "55", "60", "65"], Util.getYLabels("1"));
252 assertEquals(["900K","1.1M","1.3M","1.5M","1.7M","1.9M"], Util.getYLabels("2"));
4dd0ac55
RV
253
254 g.updateOptions(
255 {
256 axes: {
257 y: {
258 valueRange: [40, 80]
259 },
260 y2: {
261 valueRange: [1e6, 1.2e6]
262 }
263 }
264 }
265 );
fa607ffb
RK
266 assertEquals(["40", "45", "50", "55", "60", "65", "70", "75"], Util.getYLabels("1"));
267 assertEquals(["1M", "1.02M", "1.05M", "1.08M", "1.1M", "1.13M", "1.15M", "1.18M"], Util.getYLabels("2"));
289e5e99
RK
268};
269
850fb736 270MultipleAxesOldTestCase.prototype.testOldDrawPointCallback = function() {
289e5e99
RK
271 var data = MultipleAxesTestCase.getData();
272
273 var results = { y : {}, y2 : {}};
121e43ff 274 var firstCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) {
289e5e99 275 results.y[seriesName] = 1;
121e43ff
RK
276 Dygraph.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius);
277
289e5e99 278 };
121e43ff 279 var secondCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) {
289e5e99 280 results.y2[seriesName] = 1;
464b5f50 281 Dygraph.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius);
289e5e99
RK
282 };
283
284 g = new Dygraph(
285 document.getElementById("graph"),
286 data,
287 {
288 labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
289 drawPoints : true,
121e43ff 290 pointSize : 3,
289e5e99
RK
291 'Y3': {
292 axis: {
293 }
294 },
295 'Y4': {
296 axis: 'Y3' // use the same y-axis as series Y3
297 },
298 axes: {
299 y2: {
300 drawPointCallback: secondCallback
301 }
302 },
303 drawPointCallback: firstCallback
304 }
305 );
306
307 assertEquals(1, results.y["Y1"]);
308 assertEquals(1, results.y["Y2"]);
309 assertEquals(1, results.y2["Y3"]);
310 assertEquals(1, results.y2["Y4"]);
311};