454050115bb6af37fef85f740ac2a9bcb6969997
2 * @fileoverview Test cases for toDomCoords/toDataCoords
4 * @author danvk@google.com (Dan Vanderkam)
6 import Dygraph from
'../../src/dygraph';
7 import * as utils from
'../../src/dygraph-utils';
9 import Proxy from
'./Proxy';
10 import CanvasAssertions from
'./CanvasAssertions';
11 import {assertDeepCloseTo
} from
'./custom_asserts';
13 describe("to-dom-coords", function() {
16 useProxyCanvas(utils
, Proxy
);
18 // Checks that toDomCoords and toDataCoords are inverses of one another.
19 var checkForInverses
= function(g
) {
20 var x_range
= g
.xAxisRange();
21 var y_range
= g
.yAxisRange();
22 for (var i
= 0; i
<= 10; i
++) {
23 var x
= x_range
[0] + i
/ 10.0 * (x_range
[1] - x_range
[0]);
24 for (var j
= 0; j
<= 10; j
++) {
25 var y
= y_range
[0] + j
/ 10.0 * (y_range
[1] - y_range
[0]);
26 assert
.equal(x
, g
.toDataXCoord(g
.toDomXCoord(x
)));
27 assert
.equal(y
, g
.toDataYCoord(g
.toDomYCoord(y
)));
32 it('testPlainChart', function() {
53 var graph
= document
.getElementById("graph");
54 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
56 assert
.deepEqual([0, 100], g
.toDataCoords(0, 0));
57 assert
.deepEqual([0, 0], g
.toDataCoords(0, 400));
58 assert
.deepEqual([100, 100], g
.toDataCoords(400, 0));
59 assert
.deepEqual([100, 0], g
.toDataCoords(400, 400));
63 // TODO(konigsberg): This doesn't really belong here. Move to its own test.
64 var htx
= g
.hidden_ctx_
;
65 assert
.equal(1, CanvasAssertions
.numLinesDrawn(htx
, '#ff0000'));
68 it('testChartWithAxes', function() {
92 var graph
= document
.getElementById("graph");
93 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
95 assert
.deepEqual([0, 100], g
.toDataCoords(100, 0));
96 assert
.deepEqual([0, 0], g
.toDataCoords(100, 400));
97 assert
.deepEqual([100, 100], g
.toDataCoords(500, 0));
98 assert
.deepEqual([100, 0], g
.toDataCoords(500, 400));
103 it('testChartWithAxesAndLabels', function() {
119 valueRange
: [0, 100],
120 dateWindow
: [0, 100],
124 ylabel
: 'This is the y-axis',
125 xlabel
: 'This is the x-axis',
127 title
: 'This is the title of the chart',
132 var graph
= document
.getElementById("graph");
133 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
135 assert
.deepEqual([0, 100], g
.toDataCoords(100, 25));
136 assert
.deepEqual([0, 0], g
.toDataCoords(100, 425));
137 assert
.deepEqual([100, 100], g
.toDataCoords(500, 25));
138 assert
.deepEqual([100, 0], g
.toDataCoords(500, 425));
143 it('testYAxisLabelWidth', function() {
145 axes
: { y
: { axisLabelWidth
: 100 } },
148 valueRange
: [0, 100],
149 dateWindow
: [0, 100],
155 var graph
= document
.getElementById("graph");
156 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
158 assert
.deepEqual([100, 0], g
.toDomCoords(0, 100));
159 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
162 axes
: { y
: { axisLabelWidth
: 50 }},
164 assert
.deepEqual([50, 0], g
.toDomCoords(0, 100));
165 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
168 it('testAxisTickSize', function() {
170 axes
: { y
: { axisLabelWidth
: 100 } },
173 valueRange
: [0, 100],
174 dateWindow
: [0, 100],
180 var graph
= document
.getElementById("graph");
181 var g
= new Dygraph(graph
, [ [0,0], [100,100] ], opts
);
183 assert
.deepEqual([100, 0], g
.toDomCoords(0, 100));
184 assert
.deepEqual([500, 486], g
.toDomCoords(100, 0));
186 g
.updateOptions({ axisTickSize
: 50 });
187 assert
.deepEqual([200, 0], g
.toDomCoords(0, 100));
188 assert
.deepEqual([500, 386], g
.toDomCoords(100, 0));
191 it('testChartLogarithmic_YAxis', function() {
213 var graph
= document
.getElementById("graph");
214 var g
= new Dygraph(graph
, [ [1,1], [4,4] ], opts
);
217 assertDeepCloseTo([0, 4], g
.toDataCoords(0, 0), epsilon
);
218 assertDeepCloseTo([0, 1], g
.toDataCoords(0, 400), epsilon
);
219 assertDeepCloseTo([10, 4], g
.toDataCoords(400, 0), epsilon
);
220 assertDeepCloseTo([10, 1], g
.toDataCoords(400, 400), epsilon
);
221 assertDeepCloseTo([10, 2], g
.toDataCoords(400, 200), epsilon
);
223 assert
.deepEqual([0, 0], g
.toDomCoords(0, 4));
224 assert
.deepEqual([0, 400], g
.toDomCoords(0, 1));
225 assert
.deepEqual([400, 0], g
.toDomCoords(10, 4));
226 assert
.deepEqual([400, 400], g
.toDomCoords(10, 1));
227 assert
.deepEqual([400, 200], g
.toDomCoords(10, 2));
230 it('testChartLogarithmic_XAxis', function() {
233 valueRange
: [1, 1000],
234 dateWindow
: [1, 1000],
252 var graph
= document
.getElementById("graph");
253 var g
= new Dygraph(graph
, [ [1,1], [10, 10], [100,100], [1000,1000] ], opts
);
256 assert
.closeTo(1, g
.toDataXCoord(0), epsilon
);
257 assert
.closeTo(5.623413251903489, g
.toDataXCoord(100), epsilon
);
258 assert
.closeTo(31.62277660168378, g
.toDataXCoord(200), epsilon
);
259 assert
.closeTo(177.8279410038921, g
.toDataXCoord(300), epsilon
);
260 assert
.closeTo(1000, g
.toDataXCoord(400), epsilon
);
262 assert
.closeTo(0, g
.toDomXCoord(1), epsilon
);
263 assert
.closeTo(3.6036036036036037, g
.toDomXCoord(10), epsilon
);
264 assert
.closeTo(39.63963963963964, g
.toDomXCoord(100), epsilon
);
265 assert
.closeTo(400, g
.toDomXCoord(1000), epsilon
);
267 assert
.closeTo(0, g
.toPercentXCoord(1), epsilon
);
268 assert
.closeTo(0.3333333333, g
.toPercentXCoord(10), epsilon
);
269 assert
.closeTo(0.6666666666, g
.toPercentXCoord(100), epsilon
);
270 assert
.closeTo(1, g
.toPercentXCoord(1000), epsilon
);
272 // Now zoom in and ensure that the methods return reasonable values.
273 g
.updateOptions({dateWindow
: [ 10, 100 ]});
275 assert
.closeTo(10, g
.toDataXCoord(0), epsilon
);
276 assert
.closeTo(17.78279410038923, g
.toDataXCoord(100), epsilon
);
277 assert
.closeTo(31.62277660168379, g
.toDataXCoord(200), epsilon
);
278 assert
.closeTo(56.23413251903491, g
.toDataXCoord(300), epsilon
);
279 assert
.closeTo(100, g
.toDataXCoord(400), epsilon
);
281 assert
.closeTo(-40, g
.toDomXCoord(1), epsilon
);
282 assert
.closeTo(0, g
.toDomXCoord(10), epsilon
);
283 assert
.closeTo(400, g
.toDomXCoord(100), epsilon
);
284 assert
.closeTo(4400, g
.toDomXCoord(1000), epsilon
);
286 assert
.closeTo(-1, g
.toPercentXCoord(1), epsilon
);
287 assert
.closeTo(0, g
.toPercentXCoord(10), epsilon
);
288 assert
.closeTo(1, g
.toPercentXCoord(100), epsilon
);
289 assert
.closeTo(2, g
.toPercentXCoord(1000), epsilon
);