2 * @fileoverview Test cases for toDomCoords/toDataCoords
4 * @author danvk@google.com (Dan Vanderkam)
7 describe("to-dom-coords", function() {
9 var origFunc
= Dygraph
.getContext
;
10 beforeEach(function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
12 Dygraph
.getContext
= function(canvas
) {
13 return new Proxy(origFunc(canvas
));
17 afterEach(function() {
18 Dygraph
.getContext
= origFunc
;
21 // Checks that toDomCoords and toDataCoords are inverses of one another.
22 var 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 assert
.equal(x
, g
.toDataXCoord(g
.toDomXCoord(x
)));
30 assert
.equal(y
, g
.toDataYCoord(g
.toDomYCoord(y
)));
35 it('testPlainChart', function() {
56 var graph
= document
.getElementById("graph");
57 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
59 assert
.deepEqual([0, 100], g
.toDataCoords(0, 0));
60 assert
.deepEqual([0, 0], g
.toDataCoords(0, 400));
61 assert
.deepEqual([100, 100], g
.toDataCoords(400, 0));
62 assert
.deepEqual([100, 0], g
.toDataCoords(400, 400));
66 // TODO(konigsberg): This doesn't really belong here. Move to its own test.
67 var htx
= g
.hidden_ctx_
;
68 assert
.equal(1, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
71 it('testChartWithAxes', function() {
95 var graph
= document
.getElementById("graph");
96 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
98 assert
.deepEqual([0, 100], g
.toDataCoords(100, 0));
99 assert
.deepEqual([0, 0], g
.toDataCoords(100, 400));
100 assert
.deepEqual([100, 100], g
.toDataCoords(500, 0));
101 assert
.deepEqual([100, 0], g
.toDataCoords(500, 400));
106 it('testChartWithAxesAndLabels', function() {
122 valueRange
: [0, 100],
123 dateWindow
: [0, 100],
127 ylabel
: 'This is the y-axis',
128 xlabel
: 'This is the x-axis',
130 title
: 'This is the title of the chart',
135 var graph
= document
.getElementById("graph");
136 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
138 assert
.deepEqual([0, 100], g
.toDataCoords(100, 25));
139 assert
.deepEqual([0, 0], g
.toDataCoords(100, 425));
140 assert
.deepEqual([100, 100], g
.toDataCoords(500, 25));
141 assert
.deepEqual([100, 0], g
.toDataCoords(500, 425));
146 it('testYAxisLabelWidth', function() {
148 axes
: { y
: { axisLabelWidth
: 100 } },
151 valueRange
: [0, 100],
152 dateWindow
: [0, 100],
158 var graph
= document
.getElementById("graph");
159 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
161 assert
.deepEqual([100, 0], g
.toDomCoords(0, 100));
162 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
165 axes
: { y
: { axisLabelWidth
: 50 }},
167 assert
.deepEqual([50, 0], g
.toDomCoords(0, 100));
168 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
171 it('testAxisTickSize', function() {
173 axes
: { y
: { axisLabelWidth
: 100 } },
176 valueRange
: [0, 100],
177 dateWindow
: [0, 100],
183 var graph
= document
.getElementById("graph");
184 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
186 assert
.deepEqual([100, 0], g
.toDomCoords(0, 100));
187 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
189 g
.updateOptions({ axisTickSize
: 50 });
190 assert
.deepEqual([200, 0], g
.toDomCoords(0, 100));
191 assert
.deepEqual([500, 386], g
.toDomCoords(100, 0));
194 it('testChartLogarithmic_YAxis', function() {
216 var graph
= document
.getElementById("graph");
217 var g
= new Dygraph(graph
, [ [1,1], [4,4] ], opts
);
220 assertDeepCloseTo([0, 4], g
.toDataCoords(0, 0), epsilon
);
221 assertDeepCloseTo([0, 1], g
.toDataCoords(0, 400), epsilon
);
222 assertDeepCloseTo([10, 4], g
.toDataCoords(400, 0), epsilon
);
223 assertDeepCloseTo([10, 1], g
.toDataCoords(400, 400), epsilon
);
224 assertDeepCloseTo([10, 2], g
.toDataCoords(400, 200), epsilon
);
226 assert
.deepEqual([0, 0], g
.toDomCoords(0, 4));
227 assert
.deepEqual([0, 400], g
.toDomCoords(0, 1));
228 assert
.deepEqual([400, 0], g
.toDomCoords(10, 4));
229 assert
.deepEqual([400, 400], g
.toDomCoords(10, 1));
230 assert
.deepEqual([400, 200], g
.toDomCoords(10, 2));
233 it('testChartLogarithmic_XAxis', function() {
236 valueRange
: [1, 1000],
237 dateWindow
: [1, 1000],
255 var graph
= document
.getElementById("graph");
256 var g
= new Dygraph(graph
, [ [1,1], [10, 10], [100,100], [1000,1000] ], opts
);
259 assert
.closeTo(1, g
.toDataXCoord(0), epsilon
);
260 assert
.closeTo(5.623413251903489, g
.toDataXCoord(100), epsilon
);
261 assert
.closeTo(31.62277660168378, g
.toDataXCoord(200), epsilon
);
262 assert
.closeTo(177.8279410038921, g
.toDataXCoord(300), epsilon
);
263 assert
.closeTo(1000, g
.toDataXCoord(400), epsilon
);
265 assert
.closeTo(0, g
.toDomXCoord(1), epsilon
);
266 assert
.closeTo(3.6036036036036037, g
.toDomXCoord(10), epsilon
);
267 assert
.closeTo(39.63963963963964, g
.toDomXCoord(100), epsilon
);
268 assert
.closeTo(400, g
.toDomXCoord(1000), epsilon
);
270 assert
.closeTo(0, g
.toPercentXCoord(1), epsilon
);
271 assert
.closeTo(0.3333333333, g
.toPercentXCoord(10), epsilon
);
272 assert
.closeTo(0.6666666666, g
.toPercentXCoord(100), epsilon
);
273 assert
.closeTo(1, g
.toPercentXCoord(1000), epsilon
);
275 // Now zoom in and ensure that the methods return reasonable values.
276 g
.updateOptions({dateWindow
: [ 10, 100 ]});
278 assert
.closeTo(10, g
.toDataXCoord(0), epsilon
);
279 assert
.closeTo(17.78279410038923, g
.toDataXCoord(100), epsilon
);
280 assert
.closeTo(31.62277660168379, g
.toDataXCoord(200), epsilon
);
281 assert
.closeTo(56.23413251903491, g
.toDataXCoord(300), epsilon
);
282 assert
.closeTo(100, g
.toDataXCoord(400), epsilon
);
284 assert
.closeTo(-40, g
.toDomXCoord(1), epsilon
);
285 assert
.closeTo(0, g
.toDomXCoord(10), epsilon
);
286 assert
.closeTo(400, g
.toDomXCoord(100), epsilon
);
287 assert
.closeTo(4400, g
.toDomXCoord(1000), epsilon
);
289 assert
.closeTo(-1, g
.toPercentXCoord(1), epsilon
);
290 assert
.closeTo(0, g
.toPercentXCoord(10), epsilon
);
291 assert
.closeTo(1, g
.toPercentXCoord(100), epsilon
);
292 assert
.closeTo(2, g
.toPercentXCoord(1000), epsilon
);