2 * @fileoverview Tests involving multiple y-axes.
4 * @author danvdk@gmail.com (Dan Vanderkam)
7 import Dygraph from
'../../src/dygraph';
8 import * as utils from
'../../src/dygraph-utils';
9 import Util from
'./Util';
11 describe("multiple-axes-tests", function() {
15 var getData
= function() {
17 for (var i
= 1; i
<= 100; 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
),
27 1e6
* (1 + i
* (100 - i
) / (50 * 50)),
28 1e6
* (2 - i
* (100 - i
) / (50 * 50))]);
33 it('testBasicMultipleAxes', function() {
37 document
.getElementById("graph"),
40 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
53 // set axis-related properties here
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"));
64 it('testTwoAxisVisibility', function() {
67 data
.push([1,2,2000]);
68 data
.push([2,4,1000]);
71 document
.getElementById("graph"),
74 labels
: [ 'X', 'bar', 'zot' ],
88 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
89 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
91 g
.setVisibility(0, false);
93 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
94 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
96 g
.setVisibility(0, true);
97 g
.setVisibility(1, false);
99 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y").length
> 0);
100 assert
.isTrue(document
.getElementsByClassName("dygraph-axis-label-y2").length
> 0);
103 // verifies that all four chart labels (title, x-, y-, y2-axis label) can be
104 // used simultaneously.
105 it('testMultiChartLabels', function() {
106 var data
= getData();
108 var el
= document
.getElementById("graph");
109 el
.style
.border
= '1px solid black';
110 el
.style
.marginLeft
= '200px';
111 el
.style
.marginTop
= '200px';
117 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
135 assert
.deepEqual(["Chart title", "x-axis", "y-axis", "y2-axis"],
136 Util
.getClassTexts("dygraph-label"));
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"));
142 // TODO(danvk): check relative positioning here: title on top, y left of y2.
145 // Check that a chart w/o a secondary y
-axis will not get a y2label
, even
if one
147 it('testNoY2LabelWithoutSecondaryAxis', function() {
149 document
.getElementById("graph"),
152 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
162 assert
.deepEqual(["Chart title", "x-axis", "y-axis"],
163 Util
.getClassTexts("dygraph-label"));
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"));
170 it('testValueRangePerAxisOptions', function() {
171 var data
= getData();
174 document
.getElementById("graph"),
177 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
192 // set axis-related properties here
196 ylabel
: 'Primary y-axis',
197 y2label
: 'Secondary y-axis',
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"));
210 valueRange
: [1e6
, 1.2e6
]
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"));
219 it('testDrawPointCallback', function() {
220 var data
= getData();
222 var results
= { y
: {}, y2
: {}};
223 var firstCallback
= function(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
) {
224 results
.y
[seriesName
] = 1;
225 utils
.Circles
.DEFAULT(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
);
228 var secondCallback
= function(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
) {
229 results
.y2
[seriesName
] = 1;
230 utils
.Circles
.DEFAULT(g
, seriesName
, ctx
, canvasx
, canvasy
, color
, radius
);
234 document
.getElementById("graph"),
237 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
250 drawPointCallback
: secondCallback
253 drawPointCallback
: firstCallback
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"]);
263 // Test for http://code.google.com/p/dygraphs/issues/detail
?id
=436
264 it('testRemovingSecondAxis', function() {
265 var data
= getData();
267 var results
= { y
: {}, y2
: {}};
270 document
.getElementById("graph"),
273 labels
: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],
284 g
.updateOptions({ series
: { Y4
: { axis
: 'y' } } });