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() {
49 var graph
= document
.getElementById("graph");
50 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
52 assertEquals([0, 100], g
.toDataCoords(0, 0));
53 assertEquals([0, 0], g
.toDataCoords(0, 400));
54 assertEquals([100, 100], g
.toDataCoords(400, 0));
55 assertEquals([100, 0], g
.toDataCoords(400, 400));
57 this.checkForInverses(g
);
59 // TODO(konigsberg): This doesn't really belong here. Move to its own test.
60 var htx
= g
.hidden_ctx_
;
61 assertEquals(1, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
64 ToDomCoordsTestCase
.prototype.testChartWithAxes
= function() {
81 var graph
= document
.getElementById("graph");
82 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
84 assertEquals([0, 100], g
.toDataCoords(100, 0));
85 assertEquals([0, 0], g
.toDataCoords(100, 400));
86 assertEquals([100, 100], g
.toDataCoords(500, 0));
87 assertEquals([100, 0], g
.toDataCoords(500, 400));
89 this.checkForInverses(g
);
92 ToDomCoordsTestCase
.prototype.testChartWithAxesAndLabels
= function() {
102 valueRange
: [0, 100],
103 dateWindow
: [0, 100],
107 ylabel
: 'This is the y-axis',
108 xlabel
: 'This is the x-axis',
110 title
: 'This is the title of the chart',
114 var graph
= document
.getElementById("graph");
115 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
117 assertEquals([0, 100], g
.toDataCoords(100, 25));
118 assertEquals([0, 0], g
.toDataCoords(100, 425));
119 assertEquals([100, 100], g
.toDataCoords(500, 25));
120 assertEquals([100, 0], g
.toDataCoords(500, 425));
122 this.checkForInverses(g
);
125 ToDomCoordsTestCase
.prototype.testYAxisLabelWidth
= function() {
127 yAxisLabelWidth
: 100,
130 valueRange
: [0, 100],
131 dateWindow
: [0, 100],
136 var graph
= document
.getElementById("graph");
137 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
139 assertEquals([100, 0], g
.toDomCoords(0, 100));
140 assertEquals([500, 486], g
.toDomCoords(100, 0));
142 g
.updateOptions({ yAxisLabelWidth
: 50 });
143 assertEquals([50, 0], g
.toDomCoords(0, 100));
144 assertEquals([500, 486], g
.toDomCoords(100, 0));
147 ToDomCoordsTestCase
.prototype.testAxisTickSize
= function() {
149 yAxisLabelWidth
: 100,
152 valueRange
: [0, 100],
153 dateWindow
: [0, 100],
158 var graph
= document
.getElementById("graph");
159 g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
161 assertEquals([100, 0], g
.toDomCoords(0, 100));
162 assertEquals([500, 486], g
.toDomCoords(100, 0));
164 g
.updateOptions({ axisTickSize
: 50 });
165 assertEquals([200, 0], g
.toDomCoords(0, 100));
166 assertEquals([500, 386], g
.toDomCoords(100, 0));