Commit | Line | Data |
---|---|---|
71e72c82 RK |
1 | /** |
2 | * @fileoverview Tests involving multiple y-axes. | |
3 | * | |
4 | * @author danvdk@gmail.com (Dan Vanderkam) | |
5 | */ | |
6 | ||
e8c70e4e DV |
7 | import Dygraph from '../../src/dygraph'; |
8 | import * as utils from '../../src/dygraph-utils'; | |
9 | import Util from './Util'; | |
10 | ||
89fdcedb | 11 | describe("multiple-axes-tests", function() { |
71e72c82 | 12 | |
e8c70e4e | 13 | cleanupAfterEach(); |
71e72c82 | 14 | |
319d0361 | 15 | var getData = function() { |
71e72c82 RK |
16 | var data = []; |
17 | for (var i = 1; i <= 100; i++) { | |
18 | var m = "01", d = i; | |
19 | if (d > 31) { m = "02"; d -= 31; } | |
20 | if (m == "02" && d > 28) { m = "03"; d -= 28; } | |
21 | if (m == "03" && d > 31) { m = "04"; d -= 31; } | |
22 | if (d < 10) d = "0" + d; | |
23 | // two series, one with range 1-100, one with range 1-2M | |
24 | data.push([new Date("2010/" + m + "/" + d), | |
25 | i, | |
26 | 100 - i, | |
27 | 1e6 * (1 + i * (100 - i) / (50 * 50)), | |
28 | 1e6 * (2 - i * (100 - i) / (50 * 50))]); | |
29 | } | |
30 | return data; | |
31 | }; | |
32 | ||
89fdcedb | 33 | it('testBasicMultipleAxes', function() { |
319d0361 | 34 | var data = getData(); |
71e72c82 RK |
35 | |
36 | var g = new Dygraph( | |
37 | document.getElementById("graph"), | |
38 | data, | |
39 | { | |
40 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
41 | width: 640, | |
42 | height: 350, | |
43 | series : { | |
44 | 'Y3': { | |
45 | axis: 'y2' | |
46 | }, | |
47 | 'Y4': { | |
48 | axis: 'y2' | |
49 | } | |
50 | }, | |
51 | axes : { | |
52 | y2 : { | |
53 | // set axis-related properties here | |
54 | labelsKMB: true | |
55 | } | |
56 | } | |
57 | } | |
58 | ); | |
59 | ||
89fdcedb DV |
60 | assert.deepEqual(["0","20","40","60","80","100"], Util.getYLabels("1")); |
61 | assert.deepEqual(["900K","1.12M","1.34M","1.55M","1.77M","1.99M"], Util.getYLabels("2")); | |
62 | }); | |
71e72c82 | 63 | |
89fdcedb | 64 | it('testTwoAxisVisibility', function() { |
71e72c82 RK |
65 | var data = []; |
66 | data.push([0,0,0]); | |
67 | data.push([1,2,2000]); | |
68 | data.push([2,4,1000]); | |
69 | ||
70 | var g = new Dygraph( | |
71 | document.getElementById("graph"), | |
72 | data, | |
73 | { | |
74 | labels: [ 'X', 'bar', 'zot' ], | |
75 | series : { | |
76 | zot : { | |
77 | axis : 'y2' | |
78 | } | |
79 | }, | |
80 | axes : { | |
81 | y2: { | |
82 | labelsKMB: true | |
83 | } | |
84 | } | |
85 | } | |
86 | ); | |
87 | ||
89fdcedb DV |
88 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); |
89 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); | |
71e72c82 RK |
90 | |
91 | g.setVisibility(0, false); | |
92 | ||
89fdcedb DV |
93 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); |
94 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); | |
71e72c82 RK |
95 | |
96 | g.setVisibility(0, true); | |
97 | g.setVisibility(1, false); | |
98 | ||
89fdcedb DV |
99 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y").length > 0); |
100 | assert.isTrue(document.getElementsByClassName("dygraph-axis-label-y2").length > 0); | |
101 | }); | |
71e72c82 RK |
102 | |
103 | // verifies that all four chart labels (title, x-, y-, y2-axis label) can be | |
104 | // used simultaneously. | |
89fdcedb | 105 | it('testMultiChartLabels', function() { |
319d0361 | 106 | var data = getData(); |
71e72c82 RK |
107 | |
108 | var el = document.getElementById("graph"); | |
109 | el.style.border = '1px solid black'; | |
110 | el.style.marginLeft = '200px'; | |
111 | el.style.marginTop = '200px'; | |
112 | ||
113 | var g = new Dygraph( | |
114 | el, | |
115 | data, | |
116 | { | |
117 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
118 | width: 640, | |
119 | height: 350, | |
120 | series : { | |
121 | 'Y3': { | |
122 | axis: 'y2' | |
123 | }, | |
124 | 'Y4': { | |
125 | axis: 'y2' | |
126 | } | |
127 | }, | |
128 | xlabel: 'x-axis', | |
129 | ylabel: 'y-axis', | |
130 | y2label: 'y2-axis', | |
131 | title: 'Chart title' | |
132 | } | |
133 | ); | |
134 | ||
89fdcedb | 135 | assert.deepEqual(["Chart title", "x-axis", "y-axis", "y2-axis"], |
fa607ffb | 136 | Util.getClassTexts("dygraph-label")); |
89fdcedb DV |
137 | assert.deepEqual(["Chart title"], Util.getClassTexts("dygraph-title")); |
138 | assert.deepEqual(["x-axis"], Util.getClassTexts("dygraph-xlabel")); | |
139 | assert.deepEqual(["y-axis"], Util.getClassTexts("dygraph-ylabel")); | |
140 | assert.deepEqual(["y2-axis"], Util.getClassTexts("dygraph-y2label")); | |
71e72c82 RK |
141 | |
142 | // TODO(danvk): check relative positioning here: title on top, y left of y2. | |
89fdcedb | 143 | }); |
71e72c82 RK |
144 | |
145 | // Check that a chart w/o a secondary y-axis will not get a y2label, even if one | |
146 | // is specified. | |
89fdcedb | 147 | it('testNoY2LabelWithoutSecondaryAxis', function() { |
71e72c82 RK |
148 | var g = new Dygraph( |
149 | document.getElementById("graph"), | |
319d0361 | 150 | getData(), |
71e72c82 RK |
151 | { |
152 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
153 | width: 640, | |
154 | height: 350, | |
155 | xlabel: 'x-axis', | |
156 | ylabel: 'y-axis', | |
157 | y2label: 'y2-axis', | |
158 | title: 'Chart title' | |
159 | } | |
160 | ); | |
161 | ||
89fdcedb | 162 | assert.deepEqual(["Chart title", "x-axis", "y-axis"], |
fa607ffb | 163 | Util.getClassTexts("dygraph-label")); |
89fdcedb DV |
164 | assert.deepEqual(["Chart title"], Util.getClassTexts("dygraph-title")); |
165 | assert.deepEqual(["x-axis"], Util.getClassTexts("dygraph-xlabel")); | |
166 | assert.deepEqual(["y-axis"], Util.getClassTexts("dygraph-ylabel")); | |
167 | assert.deepEqual([], Util.getClassTexts("dygraph-y2label")); | |
168 | }); | |
71e72c82 | 169 | |
89fdcedb | 170 | it('testValueRangePerAxisOptions', function() { |
319d0361 | 171 | var data = getData(); |
71e72c82 | 172 | |
89fdcedb | 173 | var g = new Dygraph( |
71e72c82 RK |
174 | document.getElementById("graph"), |
175 | data, | |
176 | { | |
177 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
178 | series : { | |
179 | 'Y3': { | |
180 | axis: 'y2' | |
181 | }, | |
182 | 'Y4': { | |
183 | axis: 'y2' | |
184 | } | |
185 | }, | |
186 | axes: { | |
187 | y: { | |
bfb3e0a4 | 188 | axisLabelWidth: 60, |
71e72c82 RK |
189 | valueRange: [40, 70] |
190 | }, | |
191 | y2: { | |
192 | // set axis-related properties here | |
193 | labelsKMB: true | |
194 | } | |
195 | }, | |
196 | ylabel: 'Primary y-axis', | |
197 | y2label: 'Secondary y-axis', | |
71e72c82 RK |
198 | } |
199 | ); | |
89fdcedb DV |
200 | assert.deepEqual(["40", "45", "50", "55", "60", "65"], Util.getYLabels("1")); |
201 | assert.deepEqual(["900K","1.1M","1.3M","1.5M","1.7M","1.9M"], Util.getYLabels("2")); | |
71e72c82 RK |
202 | |
203 | g.updateOptions( | |
204 | { | |
205 | axes: { | |
206 | y: { | |
207 | valueRange: [40, 80] | |
208 | }, | |
209 | y2: { | |
210 | valueRange: [1e6, 1.2e6] | |
211 | } | |
212 | } | |
213 | } | |
214 | ); | |
89fdcedb DV |
215 | assert.deepEqual(["40", "45", "50", "55", "60", "65", "70", "75"], Util.getYLabels("1")); |
216 | assert.deepEqual(["1M", "1.02M", "1.05M", "1.08M", "1.1M", "1.13M", "1.15M", "1.18M"], Util.getYLabels("2")); | |
217 | }); | |
71e72c82 | 218 | |
89fdcedb | 219 | it('testDrawPointCallback', function() { |
319d0361 | 220 | var data = getData(); |
71e72c82 RK |
221 | |
222 | var results = { y : {}, y2 : {}}; | |
223 | var firstCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) { | |
224 | results.y[seriesName] = 1; | |
e8c70e4e | 225 | utils.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); |
71e72c82 RK |
226 | |
227 | }; | |
228 | var secondCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) { | |
229 | results.y2[seriesName] = 1; | |
e8c70e4e | 230 | utils.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); |
71e72c82 RK |
231 | }; |
232 | ||
89fdcedb | 233 | var g = new Dygraph( |
71e72c82 RK |
234 | document.getElementById("graph"), |
235 | data, | |
236 | { | |
237 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
238 | drawPoints : true, | |
239 | pointSize : 3, | |
240 | series : { | |
241 | 'Y3': { | |
242 | axis: 'y2' | |
243 | }, | |
244 | 'Y4': { | |
245 | axis: 'y2' | |
246 | } | |
247 | }, | |
248 | axes: { | |
249 | y2: { | |
250 | drawPointCallback: secondCallback | |
251 | } | |
252 | }, | |
253 | drawPointCallback: firstCallback | |
254 | } | |
255 | ); | |
256 | ||
89fdcedb DV |
257 | assert.equal(1, results.y["Y1"]); |
258 | assert.equal(1, results.y["Y2"]); | |
259 | assert.equal(1, results.y2["Y3"]); | |
260 | assert.equal(1, results.y2["Y4"]); | |
261 | }); | |
4ecb55b5 RK |
262 | |
263 | // Test for http://code.google.com/p/dygraphs/issues/detail?id=436 | |
89fdcedb | 264 | it('testRemovingSecondAxis', function() { |
319d0361 | 265 | var data = getData(); |
4ecb55b5 RK |
266 | |
267 | var results = { y : {}, y2 : {}}; | |
268 | ||
89fdcedb | 269 | var g = new Dygraph( |
4ecb55b5 RK |
270 | document.getElementById("graph"), |
271 | data, | |
272 | { | |
273 | labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ], | |
274 | drawPoints : true, | |
275 | pointSize : 3, | |
276 | series : { | |
277 | 'Y4': { | |
278 | axis: 'y2' | |
279 | } | |
280 | }, | |
281 | } | |
282 | ); | |
283 | ||
284 | g.updateOptions({ series : { Y4 : { axis : 'y' } } }); | |
89fdcedb DV |
285 | }); |
286 | ||
287 | }); |