| 1 | // Copyright (c) 2012 Google, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | /** |
| 22 | * @fileoverview Test cases for drawing lines with missing points. |
| 23 | * |
| 24 | * @author konigsberg@google.com (Robert Konigsberg) |
| 25 | */ |
| 26 | var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; |
| 27 | |
| 28 | var MissingPointsTestCase = TestCase("missing-points"); |
| 29 | |
| 30 | var _origFunc = Dygraph.getContext; |
| 31 | MissingPointsTestCase.prototype.setUp = function() { |
| 32 | document.body.innerHTML = "<div id='graph'></div>"; |
| 33 | Dygraph.getContext = function(canvas) { |
| 34 | return new Proxy(_origFunc(canvas)); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | MissingPointsTestCase.prototype.tearDown = function() { |
| 39 | Dygraph.getContext = _origFunc; |
| 40 | }; |
| 41 | |
| 42 | MissingPointsTestCase.prototype.testSeparatedPointsDontDraw = function() { |
| 43 | var graph = document.getElementById("graph"); |
| 44 | var g = new Dygraph( |
| 45 | graph, |
| 46 | [[1, 10, 11], |
| 47 | [2, 11, null], |
| 48 | [3, 12, 13]], |
| 49 | { colors: ['red', 'blue']}); |
| 50 | var htx = g.hidden_ctx_; |
| 51 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#ff0000')); |
| 52 | assertEquals(0, CanvasAssertions.numLinesDrawn(htx, '#0000ff')); |
| 53 | } |
| 54 | |
| 55 | MissingPointsTestCase.prototype.testSeparatedPointsDontDraw_expanded = function() { |
| 56 | var graph = document.getElementById("graph"); |
| 57 | var g = new Dygraph( |
| 58 | graph, |
| 59 | [[0, 10], |
| 60 | [1, 11], |
| 61 | [2, null], |
| 62 | [3, 13], |
| 63 | [4, 14]], |
| 64 | { colors: ['blue']}); |
| 65 | var htx = g.hidden_ctx_; |
| 66 | |
| 67 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#0000ff')); |
| 68 | CanvasAssertions.assertLineDrawn(htx, [56, 275], [161, 212], |
| 69 | { strokeStyle: '#0000ff', }); |
| 70 | CanvasAssertions.assertLineDrawn(htx, [370, 87], [475, 25], |
| 71 | { strokeStyle: '#0000ff', }); |
| 72 | } |
| 73 | |
| 74 | MissingPointsTestCase.prototype.testSeparatedPointsDontDraw_expanded_connected = function() { |
| 75 | var graph = document.getElementById("graph"); |
| 76 | var g = new Dygraph( |
| 77 | graph, |
| 78 | [[0, 10], |
| 79 | [1, 11], |
| 80 | [2, null], |
| 81 | [3, 13], |
| 82 | [4, 14]], |
| 83 | { colors: ['blue'], connectSeparatedPoints: true}); |
| 84 | var htx = g.hidden_ctx_; |
| 85 | var num_lines = 0; |
| 86 | |
| 87 | assertEquals(3, CanvasAssertions.numLinesDrawn(htx, '#0000ff')); |
| 88 | CanvasAssertions.assertConsecutiveLinesDrawn(htx, |
| 89 | [[56, 275], [161, 212], [370, 87], [475, 25]], |
| 90 | { strokeStyle: '#0000ff' }); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * At the time of writing this test, the blue series is only points, and not lines. |
| 95 | */ |
| 96 | MissingPointsTestCase.prototype.testConnectSeparatedPoints = function() { |
| 97 | var g = new Dygraph( |
| 98 | document.getElementById("graph"), |
| 99 | [ |
| 100 | [1, null, 3], |
| 101 | [2, 2, null], |
| 102 | [3, null, 7], |
| 103 | [4, 5, null], |
| 104 | [5, null, 5], |
| 105 | [6, 3, null] |
| 106 | ], |
| 107 | { |
| 108 | connectSeparatedPoints: true, |
| 109 | drawPoints: true, |
| 110 | colors: ['red', 'blue'] |
| 111 | } |
| 112 | ); |
| 113 | |
| 114 | var htx = g.hidden_ctx_; |
| 115 | |
| 116 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#0000ff')); |
| 117 | CanvasAssertions.assertConsecutiveLinesDrawn(htx, |
| 118 | [[56, 225], [223, 25], [391, 125]], |
| 119 | { strokeStyle: '#0000ff' }); |
| 120 | |
| 121 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#ff0000')); |
| 122 | CanvasAssertions.assertConsecutiveLinesDrawn(htx, |
| 123 | [[140, 275], [307, 125], [475, 225]], |
| 124 | { strokeStyle: '#ff0000' }); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * At the time of writing this test, the blue series is only points, and not lines. |
| 129 | */ |
| 130 | MissingPointsTestCase.prototype.testConnectSeparatedPointsWithNan = function() { |
| 131 | var g = new Dygraph( |
| 132 | document.getElementById("graph"), |
| 133 | "x,A,B \n" + |
| 134 | "1,,3 \n" + |
| 135 | "2,2, \n" + |
| 136 | "3,,5 \n" + |
| 137 | "4,4, \n" + |
| 138 | "5,,7 \n" + |
| 139 | "6,NaN, \n" + |
| 140 | "8,8, \n" + |
| 141 | "10,10, \n", |
| 142 | { |
| 143 | connectSeparatedPoints: true, |
| 144 | drawPoints: true, |
| 145 | colors: ['red', 'blue'] |
| 146 | } |
| 147 | ); |
| 148 | |
| 149 | var htx = g.hidden_ctx_; |
| 150 | |
| 151 | // Red has two disconnected line segments |
| 152 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#ff0000')); |
| 153 | CanvasAssertions.assertLineDrawn(htx, [102, 275], [195, 212], { strokeStyle: '#ff0000' }); |
| 154 | CanvasAssertions.assertLineDrawn(htx, [381, 87], [475, 25], { strokeStyle: '#ff0000' }); |
| 155 | |
| 156 | // Blue's lines are consecutive, however. |
| 157 | assertEquals(2, CanvasAssertions.numLinesDrawn(htx, '#0000ff')); |
| 158 | CanvasAssertions.assertConsecutiveLinesDrawn(htx, |
| 159 | [[56, 244], [149, 181], [242, 118]], |
| 160 | { strokeStyle: '#0000ff' }); |
| 161 | } |
| 162 | |
| 163 | /* These lines contain awesome powa! |
| 164 | var lines = CanvasAssertions.getLinesDrawn(htx, {strokeStyle: "#0000ff"}); |
| 165 | for (var idx = 0; idx < lines.length; idx++) { |
| 166 | var line = lines[idx]; |
| 167 | console.log(line[0].args, line[1].args, line[0].properties.strokeStyle); |
| 168 | } |
| 169 | */ |