From: Dan Vanderkam Date: Sat, 11 Sep 2010 05:17:14 +0000 (-0700) Subject: add public interface; add cssClass property X-Git-Tag: v1.0.0~675 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=5c528fa23539203d0bfaa3fa1b3792c45b2a0d04;p=dygraphs.git add public interface; add cssClass property --- diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 13c8061..4f5bd6c 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -19,6 +19,7 @@ DygraphLayout = function(dygraph, options) { 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) { @@ -30,23 +31,20 @@ DygraphLayout.prototype.addDataset = function(setname, set_xy) { }; // 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() { @@ -499,9 +497,6 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { "zIndex": 10, "width": "20px", "overflow": "hidden", - "border": "1px solid black", - "background-color": "white", - "text-align": "center" }; // Get a list of point with annotations. @@ -514,6 +509,10 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { 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"; diff --git a/dygraph.js b/dygraph.js index 1889a29..fed7056 100644 --- a/dygraph.js +++ b/dygraph.js @@ -134,6 +134,9 @@ Dygraph.INFO = 2; 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, @@ -170,6 +173,7 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { 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. @@ -227,6 +231,8 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { // 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_(); @@ -1646,9 +1652,6 @@ Dygraph.prototype.drawGraph_ = function(data) { 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(); @@ -2297,6 +2300,39 @@ Dygraph.prototype.setVisibility = function(num, value) { }; /** + * 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. */ diff --git a/tests/annotation.html b/tests/annotation.html index 11d47a4..100334b 100644 --- a/tests/annotation.html +++ b/tests/annotation.html @@ -8,9 +8,15 @@ + -
+
+
+ +