| 1 | /*global Gallery,Dygraph,data */ |
| 2 | Gallery.register( |
| 3 | 'annotations', |
| 4 | { |
| 5 | name: 'Annotations', |
| 6 | title: 'Dynamic Annotations Demo', |
| 7 | setup: function(parent) { |
| 8 | parent.innerHTML = [ |
| 9 | "<p>Click any point to add an annotation to it or click 'Add Annotation'.</p>", |
| 10 | "<button id='add'>Add Annotation></button>", |
| 11 | "<button id='bottom'>Shove to bottom</button>", |
| 12 | "<div id='list'></div>", |
| 13 | "<div id='g_div'></div>", |
| 14 | "<div id='events'></div>" ].join("\n"); |
| 15 | }, |
| 16 | |
| 17 | run: function() { |
| 18 | var eventDiv = document.getElementById("events"); |
| 19 | function nameAnnotation(ann) { |
| 20 | return "(" + ann.series + ", " + ann.x + ")"; |
| 21 | } |
| 22 | |
| 23 | var g = new Dygraph( |
| 24 | document.getElementById("g_div"), |
| 25 | function() { |
| 26 | var zp = function(x) { if (x < 10) return "0"+x; else return x; }; |
| 27 | var r = "date,parabola,line,another line,sine wave\n"; |
| 28 | for (var i=1; i<=31; i++) { |
| 29 | r += "200610" + zp(i); |
| 30 | r += "," + 10*(i*(31-i)); |
| 31 | r += "," + 10*(8*i); |
| 32 | r += "," + 10*(250 - 8*i); |
| 33 | r += "," + 10*(125 + 125 * Math.sin(0.3*i)); |
| 34 | r += "\n"; |
| 35 | } |
| 36 | return r; |
| 37 | }, |
| 38 | { |
| 39 | rollPeriod: 1, |
| 40 | showRoller: true, |
| 41 | width: 480, |
| 42 | height: 320, |
| 43 | drawCallback: function(g) { |
| 44 | var ann = g.annotations(); |
| 45 | var html = ""; |
| 46 | for (var i = 0; i < ann.length; i++) { |
| 47 | var name = nameAnnotation(ann[i]); |
| 48 | html += "<span id='" + name + "'>"; |
| 49 | html += name + ": " + (ann[i].shortText || '(icon)'); |
| 50 | html += " -> " + ann[i].text + "</span><br/>"; |
| 51 | } |
| 52 | document.getElementById("list").innerHTML = html; |
| 53 | } |
| 54 | } |
| 55 | ); |
| 56 | |
| 57 | var last_ann = 0; |
| 58 | var annotations = []; |
| 59 | for (var x = 10; x < 15; x += 2) { |
| 60 | annotations.push( { |
| 61 | series: 'sine wave', |
| 62 | x: "200610" + x, |
| 63 | shortText: x, |
| 64 | text: 'Stock Market Crash ' + x |
| 65 | } ); |
| 66 | last_ann = x; |
| 67 | } |
| 68 | annotations.push( { |
| 69 | series: 'another line', |
| 70 | x: "20061013", |
| 71 | icon: 'images/dollar.png', |
| 72 | width: 18, |
| 73 | height: 23, |
| 74 | tickHeight: 4, |
| 75 | text: 'Another one', |
| 76 | cssClass: 'annotation', |
| 77 | clickHandler: function() { |
| 78 | eventDiv.innerHTML += "special handler<br/>"; |
| 79 | } |
| 80 | } ); |
| 81 | g.setAnnotations(annotations); |
| 82 | |
| 83 | document.getElementById('add').onclick = function() { |
| 84 | var x = last_ann + 2; |
| 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 | }); |