Fix issue 197: Annotations are displayed outside the bounds of the graph
[dygraphs.git] / auto_tests / tests / annotations.js
1 /**
2 * @fileoverview Tests relating to annotations
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
6 var annotationsTestCase = TestCase("annotations");
7
8 annotationsTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 };
11
12 annotationsTestCase.prototype.tearDown = function() {
13 };
14
15 annotationsTestCase.prototype.testAnnotationsDrawn = function() {
16 var opts = {
17 width: 480,
18 height: 320
19 };
20 var data = "X,Y\n" +
21 "0,-1\n" +
22 "1,0\n" +
23 "2,1\n" +
24 "3,0\n"
25 ;
26
27 var graph = document.getElementById("graph");
28 var g = new Dygraph(graph, data, opts);
29 g.setAnnotations([
30 {
31 series: 'Y',
32 x: 1,
33 shortText: 'A',
34 text: 'Long A',
35 cssClass: 'ann1'
36 },
37 {
38 series: 'Y',
39 x: 2,
40 shortText: 'B',
41 text: 'Long B',
42 cssClass: 'ann2'
43 }
44 ]);
45
46 assertEquals(2, g.annotations().length);
47 var a1 = document.getElementsByClassName('ann1');
48 assertEquals(1, a1.length);
49 a1 = a1[0];
50 assertEquals('A', a1.textContent);
51
52 var a2 = document.getElementsByClassName('ann2');
53 assertEquals(1, a2.length);
54 a2 = a2[0];
55 assertEquals('B', a2.textContent);
56 };
57
58 // Some errors that should be flagged:
59 // 1. Invalid series name (e.g. 'X' or 'non-existent')
60 // 2. Passing a string as 'x' instead of a number (e.g. x: '1')
61
62 annotationsTestCase.prototype.testAnnotationsDontDisappearOnResize = function() {
63 var opts = {
64 };
65 var data = "X,Y\n" +
66 "0,-1\n" +
67 "1,0\n" +
68 "2,1\n" +
69 "3,0\n"
70 ;
71
72 var graph = document.getElementById("graph");
73 var g = new Dygraph(graph, data, opts);
74 g.setAnnotations([
75 {
76 series: 'Y',
77 x: 1,
78 shortText: 'A',
79 text: 'Long A',
80 cssClass: 'ann1'
81 }
82 ]);
83
84 // Check that it displays at all
85 assertEquals(1, g.annotations().length);
86 var a1 = document.getElementsByClassName('ann1');
87 assertEquals(1, a1.length);
88 a1 = a1[0];
89 assertEquals('A', a1.textContent);
90
91 // ... and that resizing doesn't kill it.
92 g.resize(400, 300);
93 assertEquals(1, g.annotations().length);
94 var a1 = document.getElementsByClassName('ann1');
95 assertEquals(1, a1.length);
96 a1 = a1[0];
97 assertEquals('A', a1.textContent);
98 };
99
100 // Verify that annotations outside of the visible x-range are not shown.
101 annotationsTestCase.prototype.testAnnotationsOutOfRangeX = function() {
102 var opts = {
103 };
104 var data = "X,Y\n" +
105 "0,-1\n" +
106 "1,0\n" +
107 "2,1\n" +
108 "3,0\n"
109 ;
110
111 var graph = document.getElementById("graph");
112 var g = new Dygraph(graph, data, opts);
113 g.setAnnotations([
114 {
115 series: 'Y',
116 x: 1,
117 shortText: 'A',
118 text: 'Long A',
119 cssClass: 'ann1'
120 }
121 ]);
122
123 // Check that it displays at all
124 assertEquals(1, g.annotations().length);
125 var a1 = document.getElementsByClassName('ann1');
126 assertEquals(1, a1.length);
127 a1 = a1[0];
128 assertEquals('A', a1.textContent);
129
130 // ... and that panning right removes the annotation.
131 g.updateOptions({dateWindow: [2, 6]});
132 assertEquals(1, g.annotations().length);
133 a1 = document.getElementsByClassName('ann1');
134 assertEquals(0, a1.length);
135
136 // ... and that panning left brings it back.
137 g.updateOptions({dateWindow: [0, 4]});
138 assertEquals(1, g.annotations().length);
139 a1 = document.getElementsByClassName('ann1');
140 assertEquals(1, a1.length);
141 };
142
143 // Verify that annotations outside of the visible y-range are not shown.
144 annotationsTestCase.prototype.testAnnotationsOutOfRangeY = function() {
145 var opts = {
146 };
147 var data = "X,Y\n" +
148 "0,-1\n" +
149 "1,0\n" +
150 "2,1\n" +
151 "3,0\n"
152 ;
153
154 var graph = document.getElementById("graph");
155 var g = new Dygraph(graph, data, opts);
156 g.setAnnotations([
157 {
158 series: 'Y',
159 x: 1,
160 shortText: 'A',
161 text: 'Long A',
162 cssClass: 'ann1'
163 }
164 ]);
165
166 // ... check that panning up removes the annotation.
167 g.updateOptions({valueRange: [0.5, 2.5]});
168 assertEquals(1, g.annotations().length);
169 a1 = document.getElementsByClassName('ann1');
170 assertEquals(0, a1.length);
171
172 // ... and that panning down brings it back.
173 g.updateOptions({valueRange: [-1, 1]});
174 assertEquals(1, g.annotations().length);
175 a1 = document.getElementsByClassName('ann1');
176 assertEquals(1, a1.length);
177 };