From af6e4ad59f1befcd2102a22e81dbfe0295432f3f Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 20 Feb 2013 11:51:53 -0500 Subject: [PATCH] Fix an issue where annotations are set too early in IE8 --- dygraph.js | 7 +++++++ tests/annotation.html | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/dygraph.js b/dygraph.js index 1f55870..9ec1508 100644 --- a/dygraph.js +++ b/dygraph.js @@ -3598,6 +3598,13 @@ Dygraph.prototype.setAnnotations = function(ann, suppressDraw) { // Only add the annotation CSS rule once we know it will be used. Dygraph.addAnnotationRule(); this.annotations_ = ann; + if (!this.layout_) { + this.warn("Tried to setAnnotations before dygraph was ready. " + + "Try setting them in a drawCallback. See " + + "dygraphs.com/tests/annotation.html"); + return; + } + this.layout_.setAnnotations(this.annotations_); if (!suppressDraw) { this.predraw_(); diff --git a/tests/annotation.html b/tests/annotation.html index 0e25fb7..6191436 100644 --- a/tests/annotation.html +++ b/tests/annotation.html @@ -35,6 +35,9 @@ return "(" + ann.series + ", " + ann.x + ")"; } + annotations = []; + var graph_initialized = false; + g = new Dygraph( document.getElementById("g_div"), function() { @@ -55,7 +58,14 @@ showRoller: true, width: 480, height: 320, - drawCallback: function(g) { + drawCallback: function(g, is_initial) { + if (is_initial) { + graph_initialized = true; + if (annotations.length > 0) { + g.setAnnotations(annotations); + } + } + var ann = g.annotations(); var html = ""; for (var i = 0; i < ann.length; i++) { @@ -70,7 +80,6 @@ ); var last_ann = 0; - annotations = []; for (var x = 10; x < 15; x += 2) { annotations.push( { series: 'sine wave', @@ -99,7 +108,10 @@ shortText: 'P', text: 'Parabola Annotation at same x-coord' } ); - g.setAnnotations(annotations); + + if (graph_initialized) { + g.setAnnotations(annotations); + } function add() { var x = last_ann + 2; -- 2.7.4