reasonable behavior for title
authorDan Vanderkam <dan@dygraphs.com>
Fri, 6 Jul 2012 01:35:15 +0000 (21:35 -0400)
committerDan Vanderkam <dan@dygraphs.com>
Fri, 6 Jul 2012 01:35:15 +0000 (21:35 -0400)
dygraph.js
plugins/chart-labels.js

index 99fdf3b..38c1d05 100644 (file)
@@ -2103,7 +2103,7 @@ Dygraph.prototype.predraw_ = function() {
 
   // Create a new plotter.
   if (this.plotter_) {
-    this.cascadeEvents_('clear');
+    this.cascadeEvents_('clearChart');
     this.plotter_.clear();
   }
   this.plotter_ = new DygraphCanvasRenderer(this,
@@ -2333,7 +2333,7 @@ Dygraph.prototype.drawGraph_ = function() {
  * @private
  */
 Dygraph.prototype.renderGraph_ = function(is_initial_draw) {
-  this.cascadeEvents_('clear');
+  this.cascadeEvents_('clearChart');
   this.plotter_.clear();
 
   this.plotter_.render();
index 3dad191..aa56337 100644 (file)
@@ -20,7 +20,9 @@ chart_labels.prototype.toString = function() {
 
 chart_labels.prototype.activate = function(g) {
   return {
-    layout: this.layout
+    layout: this.layout,
+    // clearChart: this.clearChart,
+    drawChart: this.drawChart
   };
 };
 
@@ -35,7 +37,27 @@ var createDivInRect = function(r) {
   return div;
 };
 
+// Detach and null out any existing nodes.
+chart_labels.prototype.detachLabels_ = function() {
+  var els = [ this.title_div_,
+              this.xlabel_div_,
+              this.ylabel_div_,
+              this.y2label_div_ ];
+  for (var i = 0; i < els.length; i++) {
+    var el = els[i];
+    if (!el) continue;
+    if (el.parentNode) el.parentNode.removeChild(el);
+  }
+
+  this.title_div_ = null;
+  this.xlabel_div_ = null;
+  this.ylabel_div_ = null;
+  this.y2label_div_ = null;
+};
+
 chart_labels.prototype.layout = function(e) {
+  this.detachLabels_();
+
   var g = e.dygraph;
   var div = e.chart_div;
   if (g.getOption('title')) {
@@ -64,11 +86,18 @@ chart_labels.prototype.layout = function(e) {
   */
 };
 
+chart_labels.prototype.drawChart = function(e) {
+  var g = e.dygraph;
+  if (this.title_div_) {
+    this.title_div_.innerHTML = g.getOption('title');
+  }
+};
+
+chart_labels.prototype.clearChart = function() {
+};
+
 chart_labels.prototype.destroy = function() {
-  this.title_div_ = null;
-  this.xlabel_div_ = null;
-  this.ylabel_div_ = null;
-  this.y2label_div_ = null;
+  detachLabels();
 };