New dygraphs home page
[dygraphs.git] / docs / annotations.html
CommitLineData
14403441
DV
1<!--#include virtual="header.html" -->
2
3<style type="text/css">
4 code { white-space: pre; }
5 pre { white-space: pre; }
6 .annotation {
7 font-size: 12px !important;
8 }
9</style>
10
11<h2>dygraphs Annotations</h2>
12
13<p>dygraphs lets you add annotations (markers) to individual points on your
14chart. These markers have a short bit of text or an icon that's displayed
15over the chart, plus a longer description that appears when you hover over
16them.</p>
17
18<h3>Demo: Dow Jones Industrial Average</h3>
19<div id="dow_chart" style="width: 800px; height: 250px;"></div>
20<script type="text/javascript">
21 // From http://www.econstats.com/eqty/eq_d_mi_3.csv
22 stockchart = new Dygraph(
23 document.getElementById('dow_chart'),
24 "dow.txt",
25 {
26 showRoller: true,
27 customBars: true,
28 labelsKMB: true,
29 labelsDivWidth: 300,
30 drawCallback: function(g, is_initial) {
31 if (!is_initial) return;
32
33 g.setAnnotations( [
34 {
35 series: "Real",
36 x: "1929-08-15",
37 shortText: "A",
38 text: "1929 Stock Market Peak",
39 cssClass: 'annotation'
40 },
534f1a05 41 {
14403441
DV
42 series: "Nominal",
43 x: "1987-08-15",
44 shortText: "B",
45 text: "1987 Crash",
46 cssClass: 'annotation'
47 },
48 {
49 series: "Nominal",
50 x: "1999-12-15",
51 shortText: "C",
52 text: "1999 (.com) Peak",
53 cssClass: 'annotation'
54 },
55 {
56 series: "Nominal",
57 x: "2007-10-15",
58 shortText: "D",
59 text: "All-Time Market Peak",
60 cssClass: 'annotation'
534f1a05 61 }
14403441
DV
62 ] );
63 }
64 }
65 );
66</script>
67
68<h3>Adding Annotations</h3>
69
70<p>There are two methods which are used to add, remove and modify annotations:</p>
71
72<table class="thinborder">
73<tr>
74 <td><code>setAnnotations(array)</code></td>
75 <td>Set the list of annotations currently displayed. This overwrites
76 existing annotations and redraws the dygraph.</td>
77</tr>
78<tr>
79 <td><code>annotations</code></td>
80 <td>Returns an array of the currently-displayed annotations.</td>
81</tr>
82</table>
83
84<p>Calling <code>dygraph.setAnnotations(dygraph.annotations())</code> is a
85no-op: it simply causes the chart to refresh.</p>
86
87<p>Let's say we have a simple chart and wish to add an annotation. Here's how:</p>
88
89 <div class="example" style="clear:both;">
90 <div class="codeblock" style="float:left;width:400px;">
91 <h3 style="text-align:center">HTML</h3>
534f1a05
DV
92 <pre>
93&lt;script type=&quot;text/javascript&quot;&gt;
94 g = new Dygraph(
95 document.getElementById(&quot;graphdiv&quot;),
96 &quot;Date,Temperature\n&quot; +
97 &quot;2008-05-07,75\n&quot; +
98 &quot;2008-05-08,70\n&quot; +
99 &quot;2008-05-09,80\n&quot;
100 );
101
102 g.setAnnotations([
103 {
104 series: &quot;Temperature&quot;,
105 x: &quot;2008-05-08&quot;,
106 shortText: &quot;L&quot;,
107 text: &quot;Coldest Day&quot;
108 }
109 ]);
110&lt;/script&gt;
111</pre>
14403441
DV
112</div>
113
114<div class="codeoutput" style="float:left;">
115 <h3 style="text-align:center">OUTPUT</h3>
116 <div id="graphdiv" style="width: 350px; height: 250px;"></div>
117 <script type="text/javascript">
118 g = new Dygraph(
119
120 // containing div
121 document.getElementById("graphdiv"),
122
123 // CSV or path to a CSV file.
124 "Date,Temperature\n" +
125 "2008-05-07,75\n" +
126 "2008-05-08,70\n" +
127 "2008-05-09,80\n"
128 );
129 g.setAnnotations([
130 {
131 series: "Temperature",
132 x: "2008-05-08",
133 shortText: "L",
134 text: "Coldest Day"
534f1a05 135 }
14403441
DV
136 ]);
137 </script>
138</div>
139</div>
140
141<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>
142
143<p>If you are using <a href="http://dygraphs.com/data.html#array">native format</a>, you need to pass in a numeric value for the <code>x</code> field. For a numeric x-axis, simply pass in the x-value of the data point on which you wish to attach the annotation. For a date axis, pass in <code>Date.parse('YYYY/MM/DD')</code>. This returns milliseconds since epoch for the date.</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
148array, then pass it back in to <code>setAnnotations</code>. For example, this
149code deletes the second annotation and changes the series to which the first
150is attached:</p>
151
152<pre>
153var annotations = g.annotations();
154annotations.splice(1,1); // remove the second annotation
155annotations[0].series = "Series 2";
156g.setAnnotations(annotations); // causes a redraw
157</pre>
534f1a05 158
14403441
DV
159<p>For a more real-life example, see the <a
160href="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
186specified 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>g.updateOptions( {
199 annotationClickHandler: function(annotation, point, dygraph, event) {
200 // ... access/modify annotation.series, annotation.x, ...
201 }
202}); </pre>
534f1a05 203
14403441
DV
204<p>Again, check out the <a href="tests/annotation.html">annotations demo</a>
205for concrete examples of usage of all these handlers.</p>
d731e964 206
14403441 207<!--#include virtual="footer.html" -->