4 <title>Hairlines demo
</title>
5 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
7 <!-- Include the Javascript for the plug-in --
>
8 <script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
9 <script src=
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
11 <link rel='stylesheet' href='http://code.jquery.com/ui/
1.10.1/themes/base/jquery-ui.css'
/>
13 <script type=
"text/javascript" src=
"../extras/hairlines.js"></script>
14 <script type=
"text/javascript" src=
"../extras/super-annotations.js"></script>
22 display: inline-block;
29 display: inline-block;
37 /* This style & the next show how you can customize the appearance of the
40 border:
1px solid black;
41 border-top-right-radius:
5px;
42 border-bottom-right-radius:
5px;
44 display: table; /* shrink to fit */
47 z-index:
10; /* should appear on top of the chart */
55 /* border-right-style: dotted !important; */
59 .dygraph-hairline.selected div {
61 width:
2px !important;
63 .hairline-info.selected {
64 border:
2px solid black;
73 display: table; /* shrink to fit */
74 box-shadow:
0 0 4px gray;
77 min-width:
120px; /* prevents squishing at the right edge of the chart */
79 .annotation-info.editable {
80 min-width:
180px; /* prevents squishing at the right edge of the chart */
83 .dygraph-annotation-line {
84 box-shadow:
0 0 4px gray;
89 <h2>Hairlines Demo
</h2>
91 <p>Click the chart to add a hairline. Drag the hairline to move it.
</p>
92 <p>Click a point to add an editable annotation. Drag it to move it up/down.
</p>
95 The
"info box" for each hairline is based on this template.
96 Customize it as you wish. The .hairline-legend element will be populated
97 with data about the current points and the .hairline-kill-button element
98 will remove the hairline when clicked. Everything else will be untouched.
100 <div id=
"hairline-template" class=
"hairline-info" style=
"display:none">
101 <button class='hairline-kill-button'
>Kill
</button>
102 <div class='hairline-legend'
></div>
104 <div id=
"annotation-template" class=
"annotation-info" style=
"display:none">
106 <div>{{x}}, {{series}}: {{y}}
</div>
108 <div id=
"annotation-editable-template" class=
"annotation-info" style=
"display:none">
109 <button class='annotation-kill-button'
>Delete
</button>
110 <button class='annotation-update'
>Change
</button>
111 <button class='annotation-cancel'
>Cancel
</button><br/>
112 <input dg-ann-field='text' type='text' size=
30 value='{{text}}'
/>
113 <div>{{x}}, {{series}}: {{y}}
</div>
115 <script type=
"text/javascript">
116 $(document).on('keyup', '.annotation-info input', function(e) {
117 var $annotationDiv = $(this).parent('.annotation-info');
118 if (e.keyCode ==
13 || e.keyCode ==
10) { // enter
119 $annotationDiv.find('.annotation-update').click();
120 } else if (e.keyCode ==
27) { // escape
121 $annotationDiv.find('.annotation-cancel').click();
124 .on('dblclick', '.annotation-info', function(e) {
125 if (e.target.tagName == 'INPUT') return;
126 $(this).find('.annotation-cancel').click();
130 <div id=
"demodiv"></div>
131 <div id=
"status"></div>
134 <input type=
"checkbox" id=
"update" checked=true
><label for=
"update"> Update
</label>
136 <button id=
"add-button">Add a Hairline
</button>
137 <button id=
"remove-button">Remove a Hairline
</button>
138 <button id=
"reset-button">Reset Hairlines
</button>
141 <input type=radio
name=
"hairline-mode" id=
"hairline-interpolated" checked=true
>
142 <label for=
"hairline-interpolated"> Interpolated
</label>
143 <input type=radio
name=
"hairline-mode" id=
"hairline-closest">
144 <label for=
"hairline-closest"> Closest
</label>
146 <p>Learn more about the
<a href=
"https://docs.google.com/document/d/1OHNE8BNNmMtFlRQ969DACIYIJ9VVJ7w3dSPRJDEeIew/edit">Hairlines/Super-annotations plugins and their APIs
</a>.
</p>
151 <script type=
"text/javascript">
154 var fn = function(t) {
155 return Math.sin(Math.PI/
180 * t *
4);
157 for (; last_t <
200; last_t++) {
158 data.push([last_t, fn(last_t)]);
161 hairlines = new Dygraph.Plugins.Hairlines({
162 divFiller: function(div, data) {
163 // This behavior is identical to what you'd get if you didn't set
164 // this option. It illustrates how to write a 'divFiller'.
165 var html = Dygraph.Plugins.Legend.generateLegendHTML(
166 data.dygraph, data.hairline.xval, data.points,
10);
167 $('.hairline-legend', div).html(html);
168 $(div).data({xval: data.hairline.xval}); // see .hover() below.
171 annotations = new Dygraph.Plugins.SuperAnnotations({
172 defaultAnnotationProperties: {
173 'text': 'Annotation Description'
177 document.getElementById(
"demodiv"),
180 labelsDiv: document.getElementById('status'),
181 labelsSeparateLines: true,
183 labels: [ 'Time', 'Value' ],
187 valueFormatter: function(val) {
188 return val.toFixed(
2);
196 // Set the plug-ins in the options.
204 var shouldUpdate = true;
205 var update = function() {
206 if (!shouldUpdate) return;
207 data.push([last_t, fn(last_t)]);
210 g.updateOptions({file: data});
212 window.setInterval(update,
1000);
215 $('#update').on('change', function() {
216 shouldUpdate = $(this).is(':checked');
219 $('#add-button').on('click', function(e) {
220 var h = hairlines.get();
224 $('#remove-button').on('click', function(e) {
225 var h = hairlines.get();
227 var idx = Math.floor(h.length /
2);
232 $('#reset-button').on('click', function(e) {
235 function setHairlineModeRadio() {
236 var hs = hairlines.get();
238 var interpolated = hs[
0].interpolated;
239 $('#hairline-interpolated').prop('checked', interpolated);
240 $('#hairline-closest').prop('checked', !interpolated);
243 $('[name=hairline-mode]').change(function() {
244 var interpolated = $('#hairline-interpolated').is(':checked');
245 var hs = hairlines.get();
246 for (var i =
0; i < hs.length; i++) {
247 hs[i].interpolated = interpolated;
253 function loadFromStorage() {
254 hairlines.set(JSON.parse(localStorage.getItem('hairlines')));
255 annotations.set(JSON.parse(localStorage.getItem('annotations')));
256 setHairlineModeRadio();
258 $(hairlines).on('hairlinesChanged', function(e) {
259 localStorage.setItem('hairlines', JSON.stringify(hairlines.get()));
260 setHairlineModeRadio();
262 $(annotations).on('annotationsChanged', function(e) {
263 localStorage.setItem('annotations', JSON.stringify(annotations.get()));
265 function setDefaultState() {
266 // triggers 'hairlinesChanged' and 'annotationsChanged' events, above.
267 hairlines.set([{xval:
55}]);
280 if (!localStorage.getItem('hairlines') ||
281 !localStorage.getItem('annotations')) {
287 // Set focus on text box when you edit an annotation.
288 $(annotations).on('beganEditAnnotation', function(e, a) {
289 $('input[type=text]', a.infoDiv).focus();
292 // Select/Deselect hairlines on click.
293 $(document).on('click', '.hairline-info', function() {
294 console.log('click');
295 var xval = $(this).data('xval');
296 var hs = hairlines.get();
297 for (var i =
0; i < hs.length; i++) {
298 if (hs[i].xval == xval) {
299 hs[i].selected = !hs[i].selected;
305 // Demonstration of how to use various other event listeners
307 'hairlineMoved': function(e, data) {
308 // console.log('hairline moved from', data.oldXVal, ' to ', data.newXVal);
310 'hairlineCreated': function(e, data) {
311 console.log('hairline created at ', data.xval);
313 'hairlineDeleted': function(e, data) {
314 console.log('hairline deleted at ', data.xval);
318 'annotationCreated': function(e, data) {
319 console.log('annotation created at ', data.series, data.xval);
321 'annotationMoved': function(e, data) {
322 console.log('annotation moved from ', data.oldYFrac, ' to ', data.newYFrac);
324 'annotationDeleted': function(e, data) {
325 console.log('annotation deleted at ', data.series, data.xval);
327 'annotationEdited': function(e, data) {
328 console.log('edited annotation at ', data.series, data.xval);
330 'cancelEditAnnotation': function(e, data) {
331 console.log('edit canceled on annotation at ', data.series, data.xval);
335 // TODO(danvk): demonstrate other annotations API methods.