2 * @fileoverview Test cases for toDomCoords/toDataCoords
4 * @author danvk@google.com (Dan Vanderkam)
7 var ToDomCoordsTestCase
= TestCase("to-dom-coords");
9 ToDomCoordsTestCase
._origFunc
= Dygraph
.getContext
;
10 ToDomCoordsTestCase
.prototype.setUp
= function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
12 Dygraph
.getContext
= function(canvas
) {
13 return new Proxy(ToDomCoordsTestCase
._origFunc(canvas
));
17 ToDomCoordsTestCase
.prototype.tearDown
= function() {
18 Dygraph
.getContext
= ToDomCoordsTestCase
._origFunc
;
21 // Checks that toDomCoords and toDataCoords are inverses of one another.
22 ToDomCoordsTestCase
.prototype.checkForInverses
= function(g
) {
23 var x_range
= g
.xAxisRange();
24 var y_range
= g
.yAxisRange();
25 for (var i
= 0; i
<= 10; i
++) {
26 var x
= x_range
[0] + i
/ 10.0 * (x_range
[1] - x_range
[0]);
27 for (var j
= 0; j
<= 10; j
++) {
28 var y
= y_range
[0] + j
/ 10.0 * (y_range
[1] - y_range
[0]);
29 assertEquals(x
, g
.toDataXCoord(g
.toDomXCoord(x
)));
30 assertEquals(y
, g
.toDataYCoord(g
.toDomYCoord(y
)));
35 ToDomCoordsTestCase
.prototype.testPlainChart
= function() {
55 var graph
= document
.getElementById("graph");
56 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
58 assertEquals([0, 100], g
.toDataCoords(0, 0));
59 assertEquals([0, 0], g
.toDataCoords(0, 400));
60 assertEquals([100, 100], g
.toDataCoords(400, 0));
61 assertEquals([100, 0], g
.toDataCoords(400, 400));
63 this.checkForInverses(g
);
65 // TODO(konigsberg): This doesn't really belong here. Move to its own test.
66 var htx
= g
.hidden_ctx_
;
67 assertEquals(1, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
70 ToDomCoordsTestCase
.prototype.testChartWithAxes
= function() {
87 var graph
= document
.getElementById("graph");
88 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
90 assertEquals([0, 100], g
.toDataCoords(100, 0));
91 assertEquals([0, 0], g
.toDataCoords(100, 400));
92 assertEquals([100, 100], g
.toDataCoords(500, 0));
93 assertEquals([100, 0], g
.toDataCoords(500, 400));
95 this.checkForInverses(g
);
98 ToDomCoordsTestCase
.prototype.testChartWithAxesAndLabels
= function() {
103 yAxisLabelWidth
: 100,
108 valueRange
: [0, 100],
109 dateWindow
: [0, 100],
113 ylabel
: 'This is the y-axis',
114 xlabel
: 'This is the x-axis',
116 title
: 'This is the title of the chart',
120 var graph
= document
.getElementById("graph");
121 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
123 assertEquals([0, 100], g
.toDataCoords(100, 25));
124 assertEquals([0, 0], g
.toDataCoords(100, 425));
125 assertEquals([100, 100], g
.toDataCoords(500, 25));
126 assertEquals([100, 0], g
.toDataCoords(500, 425));
128 this.checkForInverses(g
);
131 ToDomCoordsTestCase
.prototype.testYAxisLabelWidth
= function() {
133 yAxisLabelWidth
: 100,
136 valueRange
: [0, 100],
137 dateWindow
: [0, 100],
142 var graph
= document
.getElementById("graph");
143 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
145 assertEquals([100, 0], g
.toDomCoords(0, 100));
146 assertEquals([500, 486], g
.toDomCoords(100, 0));
148 g
.updateOptions({ yAxisLabelWidth
: 50 });
149 assertEquals([50, 0], g
.toDomCoords(0, 100));
150 assertEquals([500, 486], g
.toDomCoords(100, 0));
153 ToDomCoordsTestCase
.prototype.testAxisTickSize
= function() {
155 yAxisLabelWidth
: 100,
158 valueRange
: [0, 100],
159 dateWindow
: [0, 100],
164 var graph
= document
.getElementById("graph");
165 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
167 assertEquals([100, 0], g
.toDomCoords(0, 100));
168 assertEquals([500, 486], g
.toDomCoords(100, 0));
170 g
.updateOptions({ axisTickSize
: 50 });
171 assertEquals([200, 0], g
.toDomCoords(0, 100));
172 assertEquals([500, 386], g
.toDomCoords(100, 0));
175 ToDomCoordsTestCase
.prototype.testChartLogarithmic_YAxis
= function() {
196 var graph
= document
.getElementById("graph");
197 g
= new Dygraph(graph
, [ [1,1], [4,4] ], opts
);
200 assertEqualsDelta([0, 4], g
.toDataCoords(0, 0), epsilon
);
201 assertEqualsDelta([0, 1], g
.toDataCoords(0, 400), epsilon
);
202 assertEqualsDelta([10, 4], g
.toDataCoords(400, 0), epsilon
);
203 assertEqualsDelta([10, 1], g
.toDataCoords(400, 400), epsilon
);
204 assertEqualsDelta([10, 2], g
.toDataCoords(400, 200), epsilon
);
206 assertEquals([0, 0], g
.toDomCoords(0, 4));
207 assertEquals([0, 400], g
.toDomCoords(0, 1));
208 assertEquals([400, 0], g
.toDomCoords(10, 4));
209 assertEquals([400, 400], g
.toDomCoords(10, 1));
210 assertEquals([400, 200], g
.toDomCoords(10, 2));
213 ToDomCoordsTestCase
.prototype.testChartLogarithmic_XAxis
= function() {
216 valueRange
: [1, 1000],
217 dateWindow
: [1, 1000],
234 var graph
= document
.getElementById("graph");
235 g
= new Dygraph(graph
, [ [1,1], [10, 10], [100,100], [1000,1000] ], opts
);
238 assertEqualsDelta(1, g
.toDataXCoord(0), epsilon
);
239 assertEqualsDelta(5.623413251903489, g
.toDataXCoord(100), epsilon
);
240 assertEqualsDelta(31.62277660168378, g
.toDataXCoord(200), epsilon
);
241 assertEqualsDelta(177.8279410038921, g
.toDataXCoord(300), epsilon
);
242 assertEqualsDelta(1000, g
.toDataXCoord(400), epsilon
);
244 assertEqualsDelta(0, g
.toDomXCoord(1), epsilon
);
245 assertEqualsDelta(3.6036036036036037, g
.toDomXCoord(10), epsilon
);
246 assertEqualsDelta(39.63963963963964, g
.toDomXCoord(100), epsilon
);
247 assertEqualsDelta(400, g
.toDomXCoord(1000), epsilon
);
249 assertEqualsDelta(0, g
.toPercentXCoord(1), epsilon
);
250 assertEqualsDelta(0.3333333333, g
.toPercentXCoord(10), epsilon
);
251 assertEqualsDelta(0.6666666666, g
.toPercentXCoord(100), epsilon
);
252 assertEqualsDelta(1, g
.toPercentXCoord(1000), epsilon
);
254 // Now zoom in and ensure that the methods return reasonable values.
255 g
.updateOptions({dateWindow
: [ 10, 100 ]});
257 assertEqualsDelta(10, g
.toDataXCoord(0), epsilon
);
258 assertEqualsDelta(17.78279410038923, g
.toDataXCoord(100), epsilon
);
259 assertEqualsDelta(31.62277660168379, g
.toDataXCoord(200), epsilon
);
260 assertEqualsDelta(56.23413251903491, g
.toDataXCoord(300), epsilon
);
261 assertEqualsDelta(100, g
.toDataXCoord(400), epsilon
);
263 assertEqualsDelta(-40, g
.toDomXCoord(1), epsilon
);
264 assertEqualsDelta(0, g
.toDomXCoord(10), epsilon
);
265 assertEqualsDelta(400, g
.toDomXCoord(100), epsilon
);
266 assertEqualsDelta(4400, g
.toDomXCoord(1000), epsilon
);
268 assertEqualsDelta(-1, g
.toPercentXCoord(1), epsilon
);
269 assertEqualsDelta(0, g
.toPercentXCoord(10), epsilon
);
270 assertEqualsDelta(1, g
.toPercentXCoord(100), epsilon
);
271 assertEqualsDelta(2, g
.toPercentXCoord(1000), epsilon
);