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 MultipleAxesTestCase
.getData
= function() {
15 for (var i
= 1; i
<= 100; 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
),
25 1e6
* (1 + i
* (100 - i
) / (50 * 50)),
26 1e6
* (2 - i
* (100 - i
) / (50 * 50))]);
31 MultipleAxesTestCase
.prototype.testBasicMultipleAxes
= function() {
32 var data
= MultipleAxesTestCase
.getData();
35 document
.getElementById("graph"),
38 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
51 // set axis-related properties here
58 assertEquals(["0","20","40","60","80","100"], Util
.getYLabels("1"));
59 assertEquals(["900K","1.12M","1.34M","1.55M","1.77M","1.99M"], Util
.getYLabels("2"));
62 MultipleAxesTestCase
.prototype.testTwoAxisVisibility
= function() {
65 data
.push([1,2,2000]);
66 data
.push([2,4,1000]);
69 document
.getElementById("graph"),
72 labels
: [ 'X', 'bar', 'zot' ],
86 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
87 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
89 g
.setVisibility(0, false);
91 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
92 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
94 g
.setVisibility(0, true);
95 g
.setVisibility(1, false);
97 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
98 assertTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
101 // verifies that all four chart labels (title, x-, y-, y2-axis label) can be
102 // used simultaneously.
103 MultipleAxesTestCase
.prototype.testMultiChartLabels
= function() {
104 var data
= MultipleAxesTestCase
.getData();
106 var el
= document
.getElementById("graph");
107 el
.style
.border
= '1px solid black';
108 el
.style
.marginLeft
= '200px';
109 el
.style
.marginTop
= '200px';
115 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
133 assertEquals(["Chart title", "x-axis", "y-axis", "y2-axis"],
134 Util
.getClassTexts("dygraph-label"));
135 assertEquals(["Chart title"], Util
.getClassTexts("dygraph-title"));
136 assertEquals(["x-axis"], Util
.getClassTexts("dygraph-xlabel"));
137 assertEquals(["y-axis"], Util
.getClassTexts("dygraph-ylabel"));
138 assertEquals(["y2-axis"], Util
.getClassTexts("dygraph-y2label"));
140 // TODO(danvk): check relative positioning here: title on top, y left of y2.
143 // Check that a chart w/o a secondary y
-axis will not get a y2label
, even
if one
145 MultipleAxesTestCase
.prototype.testNoY2LabelWithoutSecondaryAxis
= function() {
147 document
.getElementById("graph"),
148 MultipleAxesTestCase
.getData(),
150 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
160 assertEquals(["Chart title", "x-axis", "y-axis"],
161 Util
.getClassTexts("dygraph-label"));
162 assertEquals(["Chart title"], Util
.getClassTexts("dygraph-title"));
163 assertEquals(["x-axis"], Util
.getClassTexts("dygraph-xlabel"));
164 assertEquals(["y-axis"], Util
.getClassTexts("dygraph-ylabel"));
165 assertEquals([], Util
.getClassTexts("dygraph-y2label"));
168 MultipleAxesTestCase
.prototype.testValueRangePerAxisOptions
= function() {
169 var data
= MultipleAxesTestCase
.getData();
172 document
.getElementById("graph"),
175 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
190 // set axis-related properties here
194 ylabel
: 'Primary y-axis',
195 y2label
: 'Secondary y-axis',
198 assertEquals(["40", "45", "50", "55", "60", "65"], Util
.getYLabels("1"));
199 assertEquals(["900K","1.1M","1.3M","1.5M","1.7M","1.9M"], Util
.getYLabels("2"));
208 valueRange
: [1e6
, 1.2e6
]
213 assertEquals(["40", "45", "50", "55", "60", "65", "70", "75"], Util
.getYLabels("1"));
214 assertEquals(["1M", "1.02M", "1.05M", "1.08M", "1.1M", "1.13M", "1.15M", "1.18M"], Util
.getYLabels("2"));
217 MultipleAxesTestCase
.prototype.testDrawPointCallback
= function() {
218 var data
= MultipleAxesTestCase
.getData();
220 var results
= { y
: {}, y2
: {}};
221 var firstCallback
= function(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
) {
222 results
.y
[seriesName
] = 1;
223 Dygraph
.Circles
.DEFAULT(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
);
226 var secondCallback
= function(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
) {
227 results
.y2
[seriesName
] = 1;
228 Dygraph
.Circles
.DEFAULT(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
);
232 document
.getElementById("graph"),
235 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
248 drawPointCallback
: secondCallback
251 drawPointCallback
: firstCallback
255 assertEquals(1, results
.y
["Y1"]);
256 assertEquals(1, results
.y
["Y2"]);
257 assertEquals(1, results
.y2
["Y3"]);
258 assertEquals(1, results
.y2
["Y4"]);
261 // Test for http://code.google.com/p/dygraphs/issues/detail
?id
=436
262 MultipleAxesTestCase
.prototype.testRemovingSecondAxis
= function() {
263 var data
= MultipleAxesTestCase
.getData();
265 var results
= { y
: {}, y2
: {}};
268 document
.getElementById("graph"),
271 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
282 g
.updateOptions({ series
: { Y4
: { axis
: 'y' } } });