2 * @fileoverview Tests involving multiple y-axes.
4 * @author danvdk@gmail.com (Dan Vanderkam)
7 var MultipleAxesTestCase
= TestCase("multiple-axes-tests");
9 MultipleAxesTestCase
.prototype.setUp
= function() {
10 document
.body
.innerHTML
= "<div id='graph'></div>";
13 function getYLabelsForAxis(axis_num
) {
14 var y_labels
= document
.getElementsByClassName("dygraph-axis-label-y" + axis_num
);
16 for (var i
= 0; i
< y_labels
.length
; i
++) {
17 ary
.push(y_labels
[i
].innerHTML
);
22 function getLegend() {
23 var legend
= document
.getElementsByClassName("dygraph-legend")[0];
24 return legend
.textContent
;
27 // returns all text in tags w/ a given css
class, sorted
.
28 function getClassTexts(css_class
) {
30 var els
= document
.getElementsByClassName(css_class
);
31 for (var i
= 0; i
< els
.length
; i
++) {
32 texts
[i
] = els
[i
].textContent
;
38 MultipleAxesTestCase
.getData
= function() {
40 for (var i
= 1; i
<= 100; i
++) {
42 if (d
> 31) { m
= "02"; d
-= 31; }
43 if (m
== "02" && d
> 28) { m
= "03"; d
-= 28; }
44 if (m
== "03" && d
> 31) { m
= "04"; d
-= 31; }
45 if (d
< 10) d
= "0" + d
;
46 // two series, one with range 1-100, one with range 1-2M
47 data
.push([new Date("2010/" + m + "/" + d
),
50 1e6
* (1 + i
* (100 - i
) / (50 * 50)),
51 1e6
* (2 - i
* (100 - i
) / (50 * 50))]);
56 MultipleAxesTestCase
.prototype.testBasicMultipleAxes
= function() {
57 var data
= MultipleAxesTestCase
.getData();
60 document
.getElementById("graph"),
63 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
68 // set axis-related properties here
73 axis
: 'Y3' // use the same y-axis as series Y3
78 assertEquals(["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"], getYLabelsForAxis("1"));
79 assertEquals(["900K", "1.01M", "1.12M", "1.23M", "1.34M", "1.45M", "1.55M", "1.66M", "1.77M", "1.88M", "1.99M"], getYLabelsForAxis("2"));
82 MultipleAxesTestCase
.prototype.testNewStylePerAxisOptions
= function() {
83 var data
= MultipleAxesTestCase
.getData();
86 document
.getElementById("graph"),
89 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
96 axis
: 'Y3' // use the same y-axis as series Y3
106 assertEquals(["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"], getYLabelsForAxis("1"));
107 assertEquals(["900K", "1.01M", "1.12M", "1.23M", "1.34M", "1.45M", "1.55M", "1.66M", "1.77M", "1.88M", "1.99M"], getYLabelsForAxis("2"));
110 MultipleAxesTestCase
.prototype.testMultiAxisLayout
= function() {
111 var data
= MultipleAxesTestCase
.getData();
113 var el
= document
.getElementById("graph");
119 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
126 axis
: 'Y3' // use the same y-axis as series Y3
136 // Test that all elements are inside the bounds of the graph, set above
137 var innerDiv
= el
.firstChild
;
138 for (var child
= innerDiv
.firstChild
; child
!= null; child
= child
.nextSibling
) {
139 assertTrue(child
.offsetLeft
>= 0);
140 assertTrue((child
.offsetLeft
+ child
.offsetWidth
) <= 640);
141 assertTrue(child
.offsetTop
>= 0);
142 // TODO(flooey@google.com): Text sometimes linebreaks,
143 // causing the labels to appear outside the allocated area.
144 // assertTrue((child.offsetTop + child.offsetHeight) <= 350);
148 MultipleAxesTestCase
.prototype.testTwoAxisVisibility
= function() {
151 data
.push([1,2,2000]);
152 data
.push([2,4,1000]);
155 document
.getElementById("graph"),
158 labels
: [ 'X', 'bar', 'zot' ],
167 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
168 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
170 g
.setVisibility(0, false);
172 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
173 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
175 g
.setVisibility(0, true);
176 g
.setVisibility(1, false);
178 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
179 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
182 // verifies that all four chart labels (title, x-, y-, y2-axis label) can be
183 // used simultaneously.
184 MultipleAxesTestCase
.prototype.testMultiChartLabels
= function() {
185 var data
= MultipleAxesTestCase
.getData();
187 var el
= document
.getElementById("graph");
188 el
.style
.border
= '1px solid black';
189 el
.style
.marginLeft
= '200px';
190 el
.style
.marginTop
= '200px';
196 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
203 axis
: 'Y3' // use the same y-axis as series Y3
212 assertEquals(["Chart title", "x-axis", "y-axis", "y2-axis"],
213 getClassTexts("dygraph-label"));
214 assertEquals(["Chart title"], getClassTexts("dygraph-title"));
215 assertEquals(["x-axis"], getClassTexts("dygraph-xlabel"));
216 assertEquals(["y-axis"], getClassTexts("dygraph-ylabel"));
217 assertEquals(["y2-axis"], getClassTexts("dygraph-y2label"));
219 // TODO(danvk): check relative positioning here: title on top, y left of y2.
222 // Check that a chart w/o a secondary y
-axis will not get a y2label
, even
if one
224 MultipleAxesTestCase
.prototype.testNoY2LabelWithoutSecondaryAxis
= function() {
226 document
.getElementById("graph"),
227 MultipleAxesTestCase
.getData(),
229 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
239 assertEquals(["Chart title", "x-axis", "y-axis"],
240 getClassTexts("dygraph-label"));
241 assertEquals(["Chart title"], getClassTexts("dygraph-title"));
242 assertEquals(["x-axis"], getClassTexts("dygraph-xlabel"));
243 assertEquals(["y-axis"], getClassTexts("dygraph-ylabel"));
244 assertEquals([], getClassTexts("dygraph-y2label"));
247 MultipleAxesTestCase
.prototype.testValueRangePerAxisOptions
= function() {
248 var data
= MultipleAxesTestCase
.getData();
251 document
.getElementById("graph"),
254 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
260 axis
: 'Y3' // use the same y-axis as series Y3
267 // set axis-related properties here
271 ylabel
: 'Primary y-axis',
272 y2label
: 'Secondary y-axis',
276 assertEquals(["40", "45", "50", "55", "60", "65"], getYLabelsForAxis("1"));
277 assertEquals(["900K","1.1M","1.3M","1.5M","1.7M","1.9M"], getYLabelsForAxis("2"));
286 valueRange
: [1e6
, 1.2e6
]
291 assertEquals(["40", "45", "50", "55", "60", "65", "70", "75"], getYLabelsForAxis("1"));
292 assertEquals(["1M", "1.02M", "1.05M", "1.08M", "1.1M", "1.13M", "1.15M", "1.18M"], getYLabelsForAxis("2"));