this.options = {}; // TODO(danvk): remove, use attr_ instead.
Dygraph.update(this.options, options ? options : {});
this.datasets = new Array();
+ this.annotations = new Array()
};
DygraphLayout.prototype.attr_ = function(name) {
};
// TODO(danvk): CONTRACT remove
-DygraphLayout.prototype.addAnnotation = function() {
- // Add an annotation to one series.
- this.annotations = [];
- for (var x = 10; x < 30; x += 2) {
- this.annotations.push( {
- series: 'sine wave',
- xval: this.attr_('xValueParser')("200610" + x),
- shortText: x,
- text: 'Stock Market Crash ' + x
- } );
+DygraphLayout.prototype.setAnnotations = function(ann) {
+ // The Dygraph object's annotations aren't parsed. We parse them here and
+ // save a copy.
+ var parse = this.attr_('xValueParser');
+ for (var i = 0; i < ann.length; i++) {
+ var a = {};
+ if (!ann[i].x) {
+ this.dygraph_.error("Annotations must have an 'x' property");
+ return;
+ }
+ Dygraph.update(a, ann[i]);
+ a.xval = parse(a.x);
+ this.annotations.push(a);
}
- this.annotations.push( {
- series: 'another line',
- xval: this.attr_('xValueParser')("20061013"),
- shortText: 'X',
- text: 'Another one'
- } );
};
DygraphLayout.prototype.evaluate = function() {
"zIndex": 10,
"width": "20px",
"overflow": "hidden",
- "border": "1px solid black",
- "background-color": "white",
- "text-align": "center"
};
// Get a list of point with annotations.
div.style[name] = annotationStyle[name];
}
}
+ div.className = "dygraphDefaultAnnotation";
+ if (p.annotation.hasOwnProperty('cssClass')) {
+ div.className += " " + p.annotation.cssClass;
+ }
div.appendChild(document.createTextNode(p.annotation.shortText));
div.style.left = (p.canvasx - 10) + "px";
div.style.top = p.canvasy + "px";
Dygraph.WARNING = 3;
Dygraph.ERROR = 3;
+// Used for initializing annotation CSS rules only once.
+Dygraph.addedAnnotationCSS = false;
+
Dygraph.prototype.__old_init__ = function(div, file, labels, attrs) {
// Labels is no longer a constructor parameter, since it's typically set
// directly from the data source. It also conains a name for the x-axis,
this.valueRange_ = attrs.valueRange || null;
this.wilsonInterval_ = attrs.wilsonInterval || true;
this.is_initial_draw_ = true;
+ this.annotations_ = [];
// Clear the div. This ensure that, if multiple dygraphs are passed the same
// div, then only one will be drawn.
// Make a note of whether labels will be pulled from the CSV file.
this.labelsFromCSV_ = (this.attr_("labels") == null);
+ Dygraph.addAnnotationRule();
+
// Create the containing DIV and other interactive elements
this.createInterface_();
this.addXTicks_();
- // TODO(danvk): CONTRACT remove
- this.layout_.addAnnotation();
-
// Tell PlotKit to use this new data and render itself
this.layout_.updateOptions({dateWindow: this.dateWindow_});
this.layout_.evaluateWithError();
};
/**
+ * Update the list of annotations and redraw the chart.
+ */
+Dygraph.prototype.setAnnotations = function(ann) {
+ this.annotations_ = ann;
+ this.layout_.setAnnotations(this.annotations_);
+ this.drawGraph_(this.rawData_);
+};
+
+/**
+ * Return the list of annotations.
+ */
+Dygraph.prototype.annotations = function() {
+ return this.annotations_;
+};
+
+Dygraph.addAnnotationRule = function() {
+ if (Dygraph.addedAnnotationCSS) return;
+
+ var mysheet=document.styleSheets[0]
+ var totalrules=mysheet.cssRules? mysheet.cssRules.length : mysheet.rules.length
+ var rule = "border: 1px solid black; " +
+ "background-color: white; " +
+ "text-align: center;";
+ if (mysheet.insertRule) { // Firefox
+ mysheet.insertRule(".dygraphDefaultAnnotation { " + rule + " }");
+ } else if (mysheet.addRule) { // IE
+ mysheet.addRule(".dygraphDefaultAnnotation", rule);
+ }
+
+ Dygraph.addedAnnotationCSS = true;
+}
+
+/**
* Create a new canvas element. This is more complex than a simple
* document.createElement("canvas") because of IE and excanvas.
*/
<script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
<script type="text/javascript" src="../dygraph-canvas.js"></script>
<script type="text/javascript" src="../dygraph.js"></script>
+ <style type="text/css">
+ .annotation {
+ background-color: #dddddd
+ }
+ </style>
</head>
<body>
- <div id="g"></div>
+ <div style="position:absolute; left:100px; top: 200px;" id="g"></div>
+ <div style="position:absolute; left:600px; top: 200px;" id="list"></div>
<script type="text/javascript">
g = new DateGraph(
},
{
rollPeriod: 1,
+ showRoller: true,
width: 480,
- height: 320
+ height: 320,
+ drawCallback: function(g) {
+ var ann = g.annotations();
+ var html = "";
+ for (var i = 0; i < ann.length; i++) {
+ html += "(" + ann[i].series + ", " + ann[i].x + "): " + ann[i].shortText + " -> " + ann[i].text + "<br/>";
+ }
+ document.getElementById("list").innerHTML = html;
+ },
}
);
+
+ var last_ann = 0;
+ annotations = [];
+ for (var x = 10; x < 15; x += 2) {
+ annotations.push( {
+ series: 'sine wave',
+ x: "200610" + x,
+ shortText: x,
+ text: 'Stock Market Crash ' + x
+ } );
+ last_ann = x;
+ }
+ annotations.push( {
+ series: 'another line',
+ x: "20061013",
+ shortText: 'X',
+ text: 'Another one',
+ cssClass: 'annotation'
+ } );
+ g.setAnnotations(annotations);
+
+ function add() {
+ var x = last_ann + 2;
+ var annnotations = g.annotations();
+ annotations.push( {
+ series: 'line',
+ x: "200610" + x,
+ shortText: x,
+ text: 'Line ' + x
+ } );
+ last_ann = x;
+ g.setAnnotations(annotations);
+ }
</script>
+
+ <input type="button" value="Add Annotation" onclick="add()" />
</body>
</html>