| 1 | Gallery.register( |
| 2 | 'annotations', |
| 3 | { |
| 4 | name: 'Annotations', |
| 5 | title: 'Dynamic Annotations Demo', |
| 6 | setup: function(parent) { |
| 7 | parent.innerHTML = [ |
| 8 | "<p>Click any point to add an annotation to it or click 'Add Annotation'.</p>", |
| 9 | "<button id='add'>Add Annotation></button>", |
| 10 | "<button id='bottom'>Shove to bottom</button>", |
| 11 | "<div id='list'></div>", |
| 12 | "<div id='g_div'></div>", |
| 13 | "<div id='events'></div>" ].join("\n"); |
| 14 | }, |
| 15 | |
| 16 | run: function() { |
| 17 | var eventDiv = document.getElementById("events"); |
| 18 | function nameAnnotation(ann) { |
| 19 | return "(" + ann.series + ", " + ann.x + ")"; |
| 20 | } |
| 21 | |
| 22 | g = new Dygraph( |
| 23 | document.getElementById("g_div"), |
| 24 | function() { |
| 25 | var zp = function(x) { if (x < 10) return "0"+x; else return x; }; |
| 26 | var r = "date,parabola,line,another line,sine wave\n"; |
| 27 | for (var i=1; i<=31; i++) { |
| 28 | r += "200610" + zp(i); |
| 29 | r += "," + 10*(i*(31-i)); |
| 30 | r += "," + 10*(8*i); |
| 31 | r += "," + 10*(250 - 8*i); |
| 32 | r += "," + 10*(125 + 125 * Math.sin(0.3*i)); |
| 33 | r += "\n"; |
| 34 | } |
| 35 | return r; |
| 36 | }, |
| 37 | { |
| 38 | rollPeriod: 1, |
| 39 | showRoller: true, |
| 40 | width: 480, |
| 41 | height: 320, |
| 42 | drawCallback: function(g) { |
| 43 | var ann = g.annotations(); |
| 44 | var html = ""; |
| 45 | for (var i = 0; i < ann.length; i++) { |
| 46 | var name = nameAnnotation(ann[i]); |
| 47 | html += "<span id='" + name + "'>" |
| 48 | html += name + ": " + (ann[i].shortText || '(icon)') |
| 49 | html += " -> " + ann[i].text + "</span><br/>"; |
| 50 | } |
| 51 | document.getElementById("list").innerHTML = html; |
| 52 | } |
| 53 | } |
| 54 | ); |
| 55 | |
| 56 | var last_ann = 0; |
| 57 | annotations = []; |
| 58 | for (var x = 10; x < 15; x += 2) { |
| 59 | annotations.push( { |
| 60 | series: 'sine wave', |
| 61 | x: "200610" + x, |
| 62 | shortText: x, |
| 63 | text: 'Stock Market Crash ' + x |
| 64 | } ); |
| 65 | last_ann = x; |
| 66 | } |
| 67 | annotations.push( { |
| 68 | series: 'another line', |
| 69 | x: "20061013", |
| 70 | icon: 'images/dollar.png', |
| 71 | width: 18, |
| 72 | height: 23, |
| 73 | tickHeight: 4, |
| 74 | text: 'Another one', |
| 75 | cssClass: 'annotation', |
| 76 | clickHandler: function() { |
| 77 | eventDiv.innerHTML += "special handler<br/>"; |
| 78 | } |
| 79 | } ); |
| 80 | g.setAnnotations(annotations); |
| 81 | |
| 82 | document.getElementById('add').onclick = function() { |
| 83 | var x = last_ann + 2; |
| 84 | var annnotations = g.annotations(); |
| 85 | annotations.push( { |
| 86 | series: 'line', |
| 87 | x: "200610" + x, |
| 88 | shortText: x, |
| 89 | text: 'Line ' + x, |
| 90 | tickHeight: 10 |
| 91 | } ); |
| 92 | last_ann = x; |
| 93 | g.setAnnotations(annotations); |
| 94 | } |
| 95 | |
| 96 | var bottom = document.getElementById('bottom'); |
| 97 | |
| 98 | bottom.onclick = function() { |
| 99 | var to_bottom = bottom.textContent == 'Shove to bottom'; |
| 100 | |
| 101 | var anns = g.annotations(); |
| 102 | for (var i = 0; i < anns.length; i++) { |
| 103 | anns[i].attachAtBottom = to_bottom; |
| 104 | } |
| 105 | g.setAnnotations(anns); |
| 106 | |
| 107 | if (to_bottom) { |
| 108 | bottom.textContent = 'Lift back up'; |
| 109 | } else { |
| 110 | bottom.textContent = 'Shove to bottom'; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | var saveBg = ''; |
| 115 | var num = 0; |
| 116 | g.updateOptions( { |
| 117 | annotationClickHandler: function(ann, point, dg, event) { |
| 118 | eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "<br/>"; |
| 119 | }, |
| 120 | annotationDblClickHandler: function(ann, point, dg, event) { |
| 121 | eventDiv.innerHTML += "dblclick: " + nameAnnotation(ann) + "<br/>"; |
| 122 | }, |
| 123 | annotationMouseOverHandler: function(ann, point, dg, event) { |
| 124 | document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold'; |
| 125 | saveBg = ann.div.style.backgroundColor; |
| 126 | ann.div.style.backgroundColor = '#ddd'; |
| 127 | }, |
| 128 | annotationMouseOutHandler: function(ann, point, dg, event) { |
| 129 | document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal'; |
| 130 | ann.div.style.backgroundColor = saveBg; |
| 131 | }, |
| 132 | |
| 133 | pointClickCallback: function(event, p) { |
| 134 | // Check if the point is already annotated. |
| 135 | if (p.annotation) return; |
| 136 | |
| 137 | // If not, add one. |
| 138 | var ann = { |
| 139 | series: p.name, |
| 140 | xval: p.xval, |
| 141 | shortText: num, |
| 142 | text: "Annotation #" + num |
| 143 | }; |
| 144 | var anns = g.annotations(); |
| 145 | anns.push(ann); |
| 146 | g.setAnnotations(anns); |
| 147 | |
| 148 | num++; |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | }); |