File: annotations.js

package info (click to toggle)
dygraphs 2.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,340 kB
  • sloc: javascript: 24,842; sh: 800; python: 581; makefile: 45
file content (153 lines) | stat: -rw-r--r-- 4,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*global Gallery,Dygraph,data */
//galleryActive=true
Gallery.register(
  'annotations',
  {
    name: 'Annotations',
    title: 'Dynamic Annotations Demo',
    setup: function(parent) {
      parent.innerHTML = [
          "<p>Click any point to add an annotation to it or click 'Add Annotation'.</p>",
          "<button id='add'>Add Annotation></button>",
          "<button id='bottom'>Shove to bottom</button>",
          "<div id='list'></div>",
          "<div id='g_div'></div>",
          "<div id='events'></div>" ].join("\n");
     },

    run: function() {
      var eventDiv = document.getElementById("events");
      function nameAnnotation(ann) {
        return "(" + ann.series + ", " + ann.x + ")";
      }

      var g = new Dygraph(
              document.getElementById("g_div"),
              function() {
                var zp = function(x) { if (x < 10) return "0"+x; else return x; };
                var r = "date,parabola,line,another line,sine wave\n";
                for (var i=1; i<=31; i++) {
                  r += "2006-10-" + zp(i);
                  r += "," + 10*(i*(31-i));
                  r += "," + 10*(8*i);
                  r += "," + 10*(250 - 8*i);
                  r += "," + 10*(125 + 125 * Math.sin(0.3*i));
                  r += "\n";
                }
                return r;
              },
              {
                rollPeriod: 1,
                showRoller: true,
                width: 480,
                height: 320,
                drawCallback: function(g) {
                  var ann = g.annotations();
                  var html = "";
                  for (var i = 0; i < ann.length; i++) {
                    var name = nameAnnotation(ann[i]);
                    html += "<span id='" + name + "'>";
                    html += name + ": " + (ann[i].shortText || '(icon)');
                    html += " -> " + ann[i].text + "</span><br />";
                  }
                  document.getElementById("list").innerHTML = html;
                }
              }
          );

      var last_ann = 0;
      var annotations = [];
      for (var x = 10; x < 15; x += 2) {
        annotations.push( {
          series: 'sine wave',
          x: "2006-10-" + x,
          shortText: x,
          text: 'Stock Market Crash ' + x
        } );
        last_ann = x;
      }
      annotations.push( {
        series: 'another line',
        x: "2006-10-13",
        icon: '../common/dollar.png',
        width: 18,
        height: 23,
        tickHeight: 4,
        text: 'Another one',
        cssClass: 'annotation',
        clickHandler: function() {
          eventDiv.innerHTML += "special handler<br />";
        }
      } );
      g.setAnnotations(annotations);

      document.getElementById('add').onclick = function() {
        var x = last_ann + 2;
        annotations.push( {
          series: 'line',
          x: "2006-10-" + x,
          shortText: x,
          text: 'Line ' + x,
          tickHeight: 10
        } );
        last_ann = x;
        g.setAnnotations(annotations);
      };

      var bottom = document.getElementById('bottom');

      bottom.onclick = function() {
        var to_bottom = bottom.textContent == 'Shove to bottom';

        var anns = g.annotations();
        for (var i = 0; i < anns.length; i++) {
          anns[i].attachAtBottom = to_bottom;
        }
        g.setAnnotations(anns);

        if (to_bottom) {
          bottom.textContent = 'Lift back up';
        } else {
          bottom.textContent = 'Shove to bottom';
        }
      };

      var saveBg = '';
      var num = 0;
      g.updateOptions( {
        annotationClickHandler: function(ann, point, dg, event) {
          eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "<br />";
        },
        annotationDblClickHandler: function(ann, point, dg, event) {
          eventDiv.innerHTML += "dblclick: " + nameAnnotation(ann) + "<br />";
        },
        annotationMouseOverHandler: function(ann, point, dg, event) {
          document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold';
          saveBg = ann.div.style.backgroundColor;
          ann.div.style.backgroundColor = '#ddd';
        },
        annotationMouseOutHandler: function(ann, point, dg, event) {
          document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal';
          ann.div.style.backgroundColor = saveBg;
        },

        pointClickCallback: function(event, p) {
          // Check if the point is already annotated.
          if (p.annotation) return;

          // If not, add one.
          var ann = {
            series: p.name,
            xval: p.xval,
            shortText: num,
            text: "Annotation #" + num
          };
          var anns = g.annotations();
          anns.push(ann);
          g.setAnnotations(anns);

          num++;
        }
      });
    }
  });