EXAMPLE: added new example for fixed bug
[dygraphs.git] / auto_tests / tests / connect_separated_points.js
CommitLineData
d413f791 1/**\r
2 * @fileoverview Test cases for the option "connectSeparatedPoints" especially for the scenario where not every series has a value for each timestamp.\r
3 *\r
4 * @author julian.eichstaedt@ch.sauter-bc.com (Fr. Sauter AG)\r
5 */\r
126e230e 6var ConnectSeparatedPointsTestCase = TestCase("connect-separated-points");\r
d413f791 7\r
126e230e 8ConnectSeparatedPointsTestCase.prototype.setUp = function() {\r
d413f791 9 document.body.innerHTML = "<div id='graph'></div>";\r
10};\r
11\r
126e230e 12ConnectSeparatedPointsTestCase.origFunc = Dygraph.getContext;\r
d413f791 13\r
126e230e 14ConnectSeparatedPointsTestCase.prototype.setUp = function() {\r
d413f791 15 document.body.innerHTML = "<div id='graph'></div>";\r
16 Dygraph.getContext = function(canvas) {\r
126e230e 17 return new Proxy(ConnectSeparatedPointsTestCase.origFunc(canvas));\r
d413f791 18 };\r
19};\r
20\r
126e230e 21ConnectSeparatedPointsTestCase.prototype.tearDown = function() {\r
22 Dygraph.getContext = ConnectSeparatedPointsTestCase.origFunc;\r
d413f791 23};\r
24\r
126e230e 25ConnectSeparatedPointsTestCase.prototype.testEdgePointsSimple = function() {\r
d413f791 26 var opts = {\r
27 width: 480,\r
28 height: 320,\r
eb55da0f 29 labels: ["x", "series1", "series2", "additionalSeries"],\r
d413f791 30 connectSeparatedPoints: true,\r
eb55da0f 31 dateWindow: [2.5,7.5]\r
d413f791 32 };\r
33\r
34 var data = [\r
eb55da0f 35 [0,-1,0,null],\r
36 [1,null,2,null],\r
37 [2,null,4,null],\r
38 [3,0.5,0,null],\r
39 [4,1,-1,5],\r
40 [5,2,-2,6],\r
41 [6,2.5,-2.5,7],\r
42 [7,3,-3,null],\r
43 [8,4,null,null],\r
44 [9,4,-10,null],\r
d413f791 45 ];\r
46\r
47 var graph = document.getElementById("graph");\r
48 var g = new Dygraph(graph, data, opts);\r
49 \r
50 htx = g.hidden_ctx_;\r
51\r
52 var attrs = {}; \r
53\r
eb55da0f 54 // Test if series1 is drawn correctly.\r
55 // ------------------------------------\r
d413f791 56 \r
57 // The first point of the first series\r
58 var x1 = data[0][0];\r
59 var y1 = data[0][1];\r
60 var xy1 = g.toDomCoords(x1, y1);\r
61 \r
eb55da0f 62 // The next valid point of this series\r
63 var x2 = data[3][0];\r
64 var y2 = data[3][1];\r
d413f791 65 var xy2 = g.toDomCoords(x2, y2);\r
66 \r
67 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
68 // even if the point is outside the visible range and only one series has a valid value for this point.\r
69 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
70\r
eb55da0f 71 // Test if series2 is drawn correctly.\r
72 // ------------------------------------\r
73\r
d413f791 74 // The last point of the second series.\r
eb55da0f 75 var x2 = data[9][0];\r
76 var y2 = data[9][2];\r
d413f791 77 var xy2 = g.toDomCoords(x2, y2);\r
eb55da0f 78\r
79 // The previous valid point of this series\r
80 var x1 = data[7][0];\r
81 var y1 = data[7][2];\r
82 var xy1 = g.toDomCoords(x1, y1);\r
d413f791 83 \r
84 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
85 // even if the point is outside the visible range and only one series has a valid value for this point.\r
86 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
126e230e 87};\r
88\r
eb55da0f 89ConnectSeparatedPointsTestCase.prototype.testEdgePointsCustomBars = function() {\r
126e230e 90 var opts = {\r
91 width: 480,\r
92 height: 320,\r
eb55da0f 93 labels: ["x", "series1", "series2", "additionalSeries"],\r
126e230e 94 connectSeparatedPoints: true,\r
eb55da0f 95 dateWindow: [2.5,7.5],\r
96 customBars: true\r
126e230e 97 };\r
eb55da0f 98 \r
126e230e 99 var data = [\r
eb55da0f 100 [0,[4,5,6], [1,2,3], [null, null, null]],\r
101 [1,[null,null,null], [2,3,4], [null, null, null]],\r
102 [2,[null,null,null], [3,4,5], [null, null, null]],\r
103 [3,[0,1,2], [2,3,4], [null, null, null]], \r
104 [4,[1,2,3], [2,3,4], [4, 5, 6]],\r
105 [5,[1,2,3], [3,4,5], [4, 5, 6]],\r
106 [6,[0,1,2], [4,5,6], [5, 6, 7]],\r
107 [7,[0,1,2], [4,5,6], [null, null, null]],\r
108 [8,[2,3,4], [null,null,null], [null, null, null]],\r
109 [9,[0,1,2], [2,4,9], [null, null, null]]\r
110 \r
126e230e 111 ];\r
112\r
113 var graph = document.getElementById("graph");\r
114 var g = new Dygraph(graph, data, opts);\r
115 \r
116 htx = g.hidden_ctx_;\r
117\r
118 var attrs = {}; \r
119\r
eb55da0f 120 \r
121 // Test if values of the series1 are drawn correctly.\r
122 // ------------------------------------\r
126e230e 123 \r
124 // The first point of the first series\r
125 var x1 = data[0][0];\r
eb55da0f 126 var y1 = data[0][1][1];\r
126e230e 127 var xy1 = g.toDomCoords(x1, y1);\r
128 \r
eb55da0f 129 // The next valid point of this series\r
130 var x2 = data[3][0];\r
131 var y2 = data[3][1][1];\r
126e230e 132 var xy2 = g.toDomCoords(x2, y2);\r
133 \r
134 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
135 // even if the point is outside the visible range and only one series has a valid value for this point.\r
136 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
eb55da0f 137 \r
138 // Test if the custom bars of the series1 are drawn correctly\r
139 // --------------------------------------------\r
140 \r
141 // The first min-point of this series\r
142 x1 = data[0][0];\r
143 y1 = data[0][1][0];\r
144 xy1 = g.toDomCoords(x1, y1);\r
d413f791 145\r
eb55da0f 146 // The next valid min-point of the second series.\r
147 x2 = data[3][0];\r
148 y2 = data[3][1][0];\r
149 xy2 = g.toDomCoords(x2, y2);\r
126e230e 150 \r
eb55da0f 151 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
152 // even if the point is outside the visible range and only one series has a valid value for this point.\r
153 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
154 \r
155 // The first max-point of this series\r
156 x1 = data[0][0];\r
157 y1 = data[0][1][2];\r
158 xy1 = g.toDomCoords(x1, y1);\r
159 \r
160 // The next valid max-point of the second series.\r
161 x2 = data[3][0];\r
162 y2 = data[3][1][2];\r
163 xy2 = g.toDomCoords(x2, y2);\r
164 \r
165 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
166 // even if the point is outside the visible range and only one series has a valid value for this point.\r
167 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
168 \r
169 // Test if values of the series2 are drawn correctly.\r
170 // ------------------------------------\r
126e230e 171 \r
172 // The last point of the second series.\r
eb55da0f 173 var x2 = data[9][0];\r
174 var y2 = data[9][2][1];\r
126e230e 175 var xy2 = g.toDomCoords(x2, y2);\r
176 \r
eb55da0f 177 // The previous valid point of this series\r
178 var x1 = data[7][0];\r
179 var y1 = data[7][2][1];\r
180 var xy1 = g.toDomCoords(x1, y1);\r
181 \r
182 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
183 // even if the point is outside the visible range and only one series has a valid value for this point.\r
184 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
185 \r
186 // Test if the custom bars of the series2 are drawn correctly\r
187 // --------------------------------------------\r
188 \r
189 // The last min-point of the second series.\r
190 x2 = data[9][0];\r
191 y2 = data[9][2][0];\r
192 xy2 = g.toDomCoords(x2, y2);\r
193 \r
194 // The previous valid min-point of this series\r
195 x1 = data[7][0];\r
196 y1 = data[7][2][0];\r
197 xy1 = g.toDomCoords(x1, y1);\r
198 \r
199 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
200 // even if the point is outside the visible range and only one series has a valid value for this point.\r
201 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
202 \r
203 // The last max-point of the second series.\r
204 x2 = data[9][0];\r
205 y2 = data[9][2][2];\r
206 xy2 = g.toDomCoords(x2, y2);\r
207 \r
208 // The previous valid max-point of this series\r
209 x1 = data[7][0];\r
210 y1 = data[7][2][2];\r
211 xy1 = g.toDomCoords(x1, y1);\r
212 \r
213 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
214 // even if the point is outside the visible range and only one series has a valid value for this point.\r
215 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
216};\r
217\r
218ConnectSeparatedPointsTestCase.prototype.testEdgePointsErrorBars = function() {\r
219 var opts = {\r
220 width: 480,\r
221 height: 320,\r
222 labels: ["x", "series1", "series2", "seriesTestHelper"],\r
223 connectSeparatedPoints: true,\r
224 dateWindow: [2,7.5],\r
225 errorBars: true\r
226 \r
227 };\r
228 \r
229 var data = [\r
230 [0,[5,1], [2,1], [null,null]],\r
231 [1,[null,null], [3,1], [null,null]],\r
232 [2,[null,null], [4,1], [null,null]],\r
233 [3,[1,1], [3,1], [null,null]], \r
234 [4,[2,1], [3,1], [5,1]],\r
235 [5,[2,1], [4,1], [5,1]],\r
236 [6,[1,1], [5,1], [6,1]],\r
237 [7,[1,1], [5,1], [null,null]],\r
238 [8,[3,1], [null,null], [null,null]],\r
239 [9,[1,1], [4,1], [null,null]]\r
240 \r
241 ];\r
242\r
243 var graph = document.getElementById("graph");\r
244 var g = new Dygraph(graph, data, opts);\r
245 \r
246 htx = g.hidden_ctx_;\r
247\r
248 var attrs = {}; \r
249\r
250 \r
251 // Test if values of the series1 are drawn correctly.\r
252 // ------------------------------------\r
253 \r
254 // The first point of the first series\r
255 var x1 = data[0][0];\r
256 var y1 = data[0][1][0];\r
257 var xy1 = g.toDomCoords(x1, y1);\r
258 \r
259 // The next valid point of this series\r
260 var x2 = data[3][0];\r
261 var y2 = data[3][1][0];\r
262 var xy2 = g.toDomCoords(x2, y2);\r
263 \r
264 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
265 // even if the point is outside the visible range and only one series has a valid value for this point.\r
266 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
267 \r
268 // Test if the upper error bars of series1 are drawn correctly\r
269 // --------------------------------------------\r
270 \r
271 // The first upper error-point of this series\r
272 x1 = data[0][0];\r
273 var y1error = y1 + (data[0][1][1]*2);\r
274 xy1 = g.toDomCoords(x1, y1error);\r
275 \r
276 // The next valid upper error-point of the second series.\r
277 x2 = data[3][0];\r
278 var y2error = y2 + (data[3][1][1]*2);\r
279 xy2 = g.toDomCoords(x2, y2error);\r
280 \r
281 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
282 // even if the point is outside the visible range and only one series has a valid value for this point.\r
283 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
284 \r
285 // Test if the lower error bars of series1 are drawn correctly\r
286 // --------------------------------------------\r
287 \r
288 // The first lower error-point of this series\r
289 x1 = data[0][0];\r
290 y1error = y1 - (data[0][1][1]*2);\r
291 xy1 = g.toDomCoords(x1, y1error);\r
292 \r
293 //The next valid lower error-point of the second series.\r
294 x2 = data[3][0];\r
295 y2error = y2 - (data[3][1][1]*2);\r
296 xy2 = g.toDomCoords(x2, y2error);\r
297 \r
298 // Check if both points are connected at the left edge of the canvas and if the option "connectSeparatedPoints" works properly\r
299 // even if the point is outside the visible range and only one series has a valid value for this point.\r
300 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
301 \r
302 \r
303 // Test if values of the series2 are drawn correctly.\r
304 // ------------------------------------\r
305 \r
306 // The last point of this series\r
307 x2 = data[9][0];\r
308 y2 = data[9][2][0];\r
309 xy2 = g.toDomCoords(x2, y2);\r
310 \r
311 // The previous valid point of the first series\r
312 x1 = data[7][0];\r
313 y1 = data[7][2][0];\r
314 xy1 = g.toDomCoords(x1, y1);\r
315 \r
316 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
317 // even if the point is outside the visible range and only one series has a valid value for this point.\r
318 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
319 \r
320 // Test if the upper error bars of series2 are drawn correctly\r
321 // --------------------------------------------\r
322 \r
323 // The last upper error-point of the second series.\r
324 x2 = data[9][0];\r
325 var y2error = y2 + (data[9][2][1]*2);\r
326 xy2 = g.toDomCoords(x2, y2error);\r
327 \r
328 // The previous valid upper error-point of this series\r
329 x1 = data[7][0];\r
330 var y1error = y1 + (data[7][2][1]*2);\r
331 xy1 = g.toDomCoords(x1, y1error);\r
332 \r
333 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
334 // even if the point is outside the visible range and only one series has a valid value for this point.\r
335 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
336 \r
337 // Test if the lower error bars of series1 are drawn correctly\r
338 // --------------------------------------------\r
339 \r
340 // The last lower error-point of the second series.\r
341 x2 = data[9][0];\r
342 y2error = y2 - (data[9][2][1]*2);\r
343 xy2 = g.toDomCoords(x2, y2error);\r
344 \r
345 // The previous valid lower error-point of this series\r
346 x1 = data[7][0];\r
347 y1error = y1 - (data[7][2][1]*2);\r
348 xy1 = g.toDomCoords(x1, y1error);\r
349 \r
126e230e 350 // Check if both points are connected at the right edge of the canvas and if the option "connectSeparatedPoints" works properly\r
351 // even if the point is outside the visible range and only one series has a valid value for this point.\r
352 CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs);\r
d413f791 353};\r