narrower chart
[dygraphs.git] / docs / annotations.html
1 <html>
2 <head>
3 <title>dygraphs annotations</title>
4 <style type="text/css">
5 code { white-space: pre; }
6 pre { white-space: pre; }
7 body { max-width: 800px; }
8 </style>
9 <!--[if IE]>
10 <script type="text/javascript" src="excanvas.js"></script>
11 <![endif]-->
12 <script type="text/javascript" src="annotations/dygraph-combined.js"></script>
13 <script type="text/javascript" src="annotations/dygraph.js"></script>
14 <script type="text/javascript" src="annotations/dygraph-canvas.js"></script>
15 <link rel="stylesheet" href="style.css">
16 </head>
17 <body>
18 <h2>dygraphs Annotations</h2>
19
20 <p>dygraphs lets you add annotations (markers) to individual points on your
21 chart. These markers have a short bit of text or an icon that's displayed
22 over the chart, plus a longer description that appears when you hover over
23 them.</p>
24
25 <h3>Demo: Dow Jones Industrial Average</h3>
26 <div id="dow_chart" style="width: 800px; height: 250px;"></div>
27 <script type="text/javascript">
28 // From http://www.econstats.com/eqty/eq_d_mi_3.csv
29 stockchart = new Dygraph(
30 document.getElementById('dow_chart'),
31 "dow.txt",
32 {
33 showRoller: true,
34 customBars: true,
35 labelsKMB: true,
36 labelsDivWidth: 300,
37 drawCallback: function(g, is_initial) {
38 if (!is_initial) return;
39
40 g.setAnnotations( [
41 {
42 series: "Real",
43 x: "1929-08-15",
44 shortText: "A",
45 text: "1929 Stock Market Peak"
46 },
47 {
48 series: "Nominal",
49 x: "1987-08-15",
50 shortText: "B",
51 text: "1987 Crash"
52 },
53 {
54 series: "Nominal",
55 x: "1999-12-15",
56 shortText: "C",
57 text: "1999 (.com) Peak"
58 },
59 {
60 series: "Nominal",
61 x: "2007-10-15",
62 shortText: "D",
63 text: "All-Time Market Peak"
64 }
65 ] );
66 }
67 }
68 );
69 </script>
70
71 <h3>Adding Annotations</h3>
72
73 <p>There are two methods which are used to add, remove and modify annotations:</p>
74
75 <table class="thinborder">
76 <tr>
77 <td><code>setAnnotations(array)</code></td>
78 <td>Set the list of annotations currently displayed. This overwrites
79 existing annotations and redraws the dygraph.</td>
80 </tr>
81 <tr>
82 <td><code>annotations</code></td>
83 <td>Returns an array of the currently-displayed annotations.</td>
84 </tr>
85 </table>
86
87 <p>Calling <code>dygraph.setAnnotations(dygraph.annotations())</code> is a
88 no-op: it simply causes the chart to refresh.</p>
89
90 <p>Let's say we have a simple chart and wish to add an annotation. Here's how:</p>
91
92 <div class="example" style="clear:both;">
93 <div class="codeblock" style="float:left;width:400px;">
94 <h3 style="text-align:center">HTML</h3>
95 <pre>
96 &lt;script type=&quot;text/javascript&quot;&gt;
97 g = new Dygraph(
98 document.getElementById(&quot;graphdiv&quot;),
99 &quot;Date,Temperature\n&quot; +
100 &quot;2008-05-07,75\n&quot; +
101 &quot;2008-05-08,70\n&quot; +
102 &quot;2008-05-09,80\n&quot;
103 );
104
105 g.setAnnotations([
106 {
107 series: &quot;Temperature&quot;,
108 x: &quot;2008-05-08&quot;,
109 shortText: &quot;L&quot;,
110 text: &quot;Coldest Day&quot;
111 }
112 ]);
113 &lt;/script&gt;
114 </pre>
115 </div>
116 <div class="codeoutput" style="float:left;">
117 <h3 style="text-align:center">OUTPUT</h3>
118 <div id="graphdiv" style="width: 350px; height: 250px;"></div>
119 <script type="text/javascript">
120 g = new Dygraph(
121
122 // containing div
123 document.getElementById("graphdiv"),
124
125 // CSV or path to a CSV file.
126 "Date,Temperature\n" +
127 "2008-05-07,75\n" +
128 "2008-05-08,70\n" +
129 "2008-05-09,80\n"
130 );
131 g.setAnnotations([
132 {
133 series: "Temperature",
134 x: "2008-05-08",
135 shortText: "L",
136 text: "Coldest Day"
137 }
138 ]);
139 </script>
140 </div>
141 </div>
142
143 <p style="clear:both">Annotations are JavaScript dictionaries. The <code>series</code> and <code>x</code> fields are required: they indicate which point the annotation should be attached to. If specified, <code>shortText</code> will appear on the annotation "flag". If you don't specify <code>shortText</code>, you can specify <code>icon</code> instead to display a small picture. The <code>text</code> parameter specifies hovertext. If you highlight the annotation and leave the mouse still, it will appear.</p>
144
145 <h3>Modifying Annotations</h3>
146 <p>To remove or modify existing annotations, call the
147 <code>annotations</code> method to get an array of annotations. Modify that
148 array, then pass it back in to <code>setAnnotations</code>. For example, this
149 code deletes the second annotation and changes the series to which the first
150 is attached:</p>
151
152 <pre>
153 var annotations = g.annotations();
154 annotations.splice(1,1); // remove the second annotation
155 annotations[0].series = "Series 2";
156 g.setAnnotations(annotations); // causes a redraw
157 </pre>
158
159 <p>For a more real-life example, see the <a
160 href="tests/annotation.html">annotations demo</a></p>
161
162 <h3>Annotations property reference</h3>
163 <p>These properties can all be set in the dictionary for an annotation. The use of each one is demonstrated on the <a href="tests/annotation.html">annotations demo</a> page.</p>
164
165 <table class="thinborder">
166 <tr> <td><b>Property</b></td><td><b>Description</b></td> </tr>
167 <tr><td><code>series</code></td> <td><i>Required</i> The name of the series to which the annotated point belongs.</td></tr>
168 <tr><td><code>x</code></td><td><i>Required</i> The x value of the point. This should be the same as the value you specified in your CSV file, e.g. "2010-09-13".</td></tr>
169 <tr><td><code>shortText</code></td><td>Text that will appear on the annotation's flag.</td></tr>
170 <tr><td><code>text</code></td><td>A longer description of the annotation which will appear when the user hovers over it.</td></tr>
171 <tr><td><code>icon</code></td><td>Specify in place of <code>shortText</code> to mark the annotation with an image rather than text. If you specify this, you must specify <code>width</code> and <code>height</code>.</td></tr>
172 <tr><td><code>width</code></td><td>Width (in pixels) of the annotation flag or icon.</td></tr>
173 <tr><td><code>height</code></td><td>Height (in pixels) of the annotation flag or icon.</td></tr>
174 <tr><td><code>cssClass</code></td><td>CSS class to use for styling the annotation.</td></tr>
175 <tr><td><code>tickHeight</code></td><td>Height of the tick mark (in pixels) connecting the point to its flag or icon.</td></tr>
176 <tr><td><code>attachAtBottom</code></td><td>If true, attach annotations to the x-axis, rather than to actual points.</td></tr>
177 <tr><td><code>clickHandler</code></td> <td>See Handlers, below</td></tr>
178 <tr><td><code>mouseOverHandler</code></td><td>See Handlers, below</td></tr>
179 <tr><td><code>mouseOutHandler</code></td> <td>See Handlers, below</td></tr>
180 <tr><td><code>dblClickHandler</code></td> <td>See Handlers, below</td></tr>
181 </table>
182
183 <h3>Annotation event handlers</h3>
184
185 <p>dygraphs lets you attach event handlers to your annotations. These can be
186 specified globally (for all annotations) or on a per-annotation basis:</p>
187
188 <table class="thinborder">
189 <tr><td><b>Per-point field</b></td><td><b>Global option</b></td></tr>
190 <tr><td><code>clickHandler</code></td> <td><code>annotationClickHandler</code></td></tr>
191 <tr><td><code>mouseOverHandler</code></td><td><code>annotationMouseOverHandler</code></td></tr>
192 <tr><td><code>mouseOutHandler</code></td> <td><code>annotationMouseOutHandler</code></td></tr>
193 <tr><td><code>dblClickHandler</code></td> <td><code>annotationDblClickHandler</code></td></tr>
194 </table>
195
196 <p>These event handlers all take the same parameters:</p>
197
198 <pre>
199 g.updateOptions( {
200 annotationClickHandler: function(annotation, point, dygraph, event) {
201 // ... access/modify annotation.series, annotation.x, ...
202 }
203 });
204 </pre>
205
206 <p>Again, check out the <a href="tests/annotation.html">annotations demo</a>
207 for concrete examples of usage of all these handlers.</p>
208
209 </body>
210 </html>