4 <meta http-equiv=
"X-UA-Compatible" content=
"IE=EmulateIE7; IE=EmulateIE9">
5 <title>Hairlines demo
</title>
7 <script type=
"text/javascript" src=
"../excanvas.js"></script>
9 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
11 <!-- Include the Javascript for the plug-in --
>
12 <script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
13 <script src=
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
15 <link rel='stylesheet' href='http://code.jquery.com/ui/
1.10.1/themes/base/jquery-ui.css'
/>
17 <script type=
"text/javascript" src=
"../extras/hairlines.js"></script>
18 <script type=
"text/javascript" src=
"../extras/super-annotations.js"></script>
26 display: inline-block;
33 display: inline-block;
41 /* This style & the next show how you can customize the appearance of the
44 border:
1px solid black;
45 border-top-right-radius:
5px;
46 border-bottom-right-radius:
5px;
48 display: table; /* shrink to fit */
51 z-index:
10; /* should appear on top of the chart */
59 /* border-right-style: dotted !important; */
63 .dygraph-hairline.selected div {
65 width:
2px !important;
67 .hairline-info.selected {
68 border:
2px solid black;
77 display: table; /* shrink to fit */
78 box-shadow:
0 0 4px gray;
81 min-width:
120px; /* prevents squishing at the right edge of the chart */
83 .annotation-info.editable {
84 min-width:
180px; /* prevents squishing at the right edge of the chart */
87 .dygraph-annotation-line {
88 box-shadow:
0 0 4px gray;
93 <h2>Hairlines Demo
</h2>
95 <p>Click the chart to add a hairline. Drag the hairline to move it.
</p>
96 <p>Click a point to add an editable annotation. Drag it to move it up/down.
</p>
99 The
"info box" for each hairline is based on this template.
100 Customize it as you wish. The .hairline-legend element will be populated
101 with data about the current points and the .hairline-kill-button element
102 will remove the hairline when clicked. Everything else will be untouched.
104 <div id=
"hairline-template" class=
"hairline-info" style=
"display:none">
105 <button class='hairline-kill-button'
>Kill
</button>
106 <div class='hairline-legend'
></div>
108 <div id=
"annotation-template" class=
"annotation-info" style=
"display:none">
110 <div>{{x}}, {{series}}: {{y}}
</div>
112 <div id=
"annotation-editable-template" class=
"annotation-info" style=
"display:none">
113 <button class='annotation-kill-button'
>Delete
</button>
114 <button class='annotation-update'
>Change
</button>
115 <button class='annotation-cancel'
>Cancel
</button><br/>
116 <input dg-ann-field='text' type='text' size=
30 value='{{text}}'
/>
117 <div>{{x}}, {{series}}: {{y}}
</div>
119 <script type=
"text/javascript">
120 $(document).on('keyup', '.annotation-info input', function(e) {
121 var $annotationDiv = $(this).parent('.annotation-info');
122 if (e.keyCode ==
13 || e.keyCode ==
10) { // enter
123 $annotationDiv.find('.annotation-update').click();
124 } else if (e.keyCode ==
27) { // escape
125 $annotationDiv.find('.annotation-cancel').click();
128 .on('dblclick', '.annotation-info', function(e) {
129 if (e.target.tagName == 'INPUT') return;
130 $(this).find('.annotation-cancel').click();
134 <div id=
"demodiv"></div>
135 <div id=
"status"></div>
138 <input type=
"checkbox" id=
"update" checked=true
><label for=
"update"> Update
</label>
140 <button id=
"add-button">Add a Hairline
</button>
141 <button id=
"remove-button">Remove a Hairline
</button>
142 <button id=
"reset-button">Reset Hairlines
</button>
145 <input type=radio
name=
"hairline-mode" id=
"hairline-interpolated" checked=true
>
146 <label for=
"hairline-interpolated"> Interpolated
</label>
147 <input type=radio
name=
"hairline-mode" id=
"hairline-closest">
148 <label for=
"hairline-closest"> Closest
</label>
150 <p>Learn more about the
<a href=
"https://docs.google.com/document/d/1OHNE8BNNmMtFlRQ969DACIYIJ9VVJ7w3dSPRJDEeIew/edit">Hairlines/Super-annotations plugins and their APIs
</a>.
</p>
155 <script type=
"text/javascript">
158 var fn = function(t) {
159 return Math.sin(Math.PI/
180 * t *
4);
161 for (; last_t <
200; last_t++) {
162 data.push([last_t, fn(last_t)]);
165 hairlines = new Dygraph.Plugins.Hairlines({
166 divFiller: function(div, data) {
167 // This behavior is identical to what you'd get if you didn't set
168 // this option. It illustrates how to write a 'divFiller'.
169 var html = Dygraph.Plugins.Legend.generateLegendHTML(
170 data.dygraph, data.hairline.xval, data.points,
10);
171 $('.hairline-legend', div).html(html);
172 $(div).data({xval: data.hairline.xval}); // see .hover() below.
175 annotations = new Dygraph.Plugins.SuperAnnotations({
176 defaultAnnotationProperties: {
177 'text': 'Annotation Description'
181 document.getElementById(
"demodiv"),
184 labelsDiv: document.getElementById('status'),
185 labelsSeparateLines: true,
187 labels: [ 'Time', 'Value' ],
191 valueFormatter: function(val) {
192 return val.toFixed(
2);
200 // Set the plug-ins in the options.
208 var shouldUpdate = true;
209 var update = function() {
210 if (!shouldUpdate) return;
211 data.push([last_t, fn(last_t)]);
214 g.updateOptions({file: data});
216 window.setInterval(update,
1000);
219 $('#update').on('change', function() {
220 shouldUpdate = $(this).is(':checked');
223 $('#add-button').on('click', function(e) {
224 var h = hairlines.get();
228 $('#remove-button').on('click', function(e) {
229 var h = hairlines.get();
231 var idx = Math.floor(h.length /
2);
236 $('#reset-button').on('click', function(e) {
239 function setHairlineModeRadio() {
240 var hs = hairlines.get();
242 var interpolated = hs[
0].interpolated;
243 $('#hairline-interpolated').prop('checked', interpolated);
244 $('#hairline-closest').prop('checked', !interpolated);
247 $('[name=hairline-mode]').change(function() {
248 var interpolated = $('#hairline-interpolated').is(':checked');
249 var hs = hairlines.get();
250 for (var i =
0; i < hs.length; i++) {
251 hs[i].interpolated = interpolated;
257 function loadFromStorage() {
258 hairlines.set(JSON.parse(localStorage.getItem('hairlines')));
259 annotations.set(JSON.parse(localStorage.getItem('annotations')));
260 setHairlineModeRadio();
262 $(hairlines).on('hairlinesChanged', function(e) {
263 localStorage.setItem('hairlines', JSON.stringify(hairlines.get()));
264 setHairlineModeRadio();
266 $(annotations).on('annotationsChanged', function(e) {
267 localStorage.setItem('annotations', JSON.stringify(annotations.get()));
269 function setDefaultState() {
270 // triggers 'hairlinesChanged' and 'annotationsChanged' events, above.
271 hairlines.set([{xval:
55}]);
284 if (!localStorage.getItem('hairlines') ||
285 !localStorage.getItem('annotations')) {
291 // Set focus on text box when you edit an annotation.
292 $(annotations).on('beganEditAnnotation', function(e, a) {
293 $('input[type=text]', a.infoDiv).focus();
296 // Select/Deselect hairlines on click.
297 $(document).on('click', '.hairline-info', function() {
298 console.log('click');
299 var xval = $(this).data('xval');
300 var hs = hairlines.get();
301 for (var i =
0; i < hs.length; i++) {
302 if (hs[i].xval == xval) {
303 hs[i].selected = !hs[i].selected;
309 // Demonstration of how to use various other event listeners
311 'hairlineMoved': function(e, data) {
312 // console.log('hairline moved from', data.oldXVal, ' to ', data.newXVal);
314 'hairlineCreated': function(e, data) {
315 console.log('hairline created at ', data.xval);
317 'hairlineDeleted': function(e, data) {
318 console.log('hairline deleted at ', data.xval);
322 'annotationCreated': function(e, data) {
323 console.log('annotation created at ', data.series, data.xval);
325 'annotationMoved': function(e, data) {
326 console.log('annotation moved from ', data.oldYFrac, ' to ', data.newYFrac);
328 'annotationDeleted': function(e, data) {
329 console.log('annotation deleted at ', data.series, data.xval);
331 'annotationEdited': function(e, data) {
332 console.log('edited annotation at ', data.series, data.xval);
334 'cancelEditAnnotation': function(e, data) {
335 console.log('edit canceled on annotation at ', data.series, data.xval);
339 // TODO(danvk): demonstrate other annotations API methods.