start porting over chart-labels plugin
authorDan Vanderkam <dan@dygraphs.com>
Fri, 6 Jul 2012 01:11:17 +0000 (21:11 -0400)
committerDan Vanderkam <dan@dygraphs.com>
Fri, 6 Jul 2012 01:11:17 +0000 (21:11 -0400)
plugins/chart-labels.js [new file with mode: 0644]
plugins/install.js

diff --git a/plugins/chart-labels.js b/plugins/chart-labels.js
new file mode 100644 (file)
index 0000000..0b6e4c2
--- /dev/null
@@ -0,0 +1,76 @@
+/**
+ * @license
+ * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
+ * MIT-licensed (http://opensource.org/licenses/MIT)
+ */
+Dygraph.Plugins.ChartLabels = (function() {
+
+// TODO(danvk): move chart label options out of dygraphs and into the plugin.
+
+var chart_labels = function() {
+  this.title_div_ = null;
+  this.xlabel_div_ = null;
+  this.ylabel_div_ = null;
+  this.y2label_div_ = null;
+};
+
+chart_labels.prototype.toString = function() {
+  return "ChartLabels Plugin";
+};
+
+chart_labels.prototype.activate = function(g) {
+  return {
+    layout: this.layout
+  };
+};
+
+// QUESTION: should there be a plugin-utils.js?
+var createDivInRect = function(r) {
+  var div = document.createElement('div');
+  div.style.position = 'absolute';
+  div.style.left = r.x + 'px';
+  div.style.top = r.y + 'px';
+  div.style.width = r.w + 'px';
+  div.style.height = r.h + 'px';
+  return div;
+};
+
+chart_labels.layout = function(e) {
+  var g = e.dygraph;
+  var div = e.chart_div;
+  if (g.getOption('title')) {
+    // QUESTION: should this return an absolutely-positioned div instead?
+    var title_rect = e.reserveSpaceTop(g.getOption('titleHeight'));
+    this.title_div_ = createDivInRect(title_rect);
+    this.title_div_.innerHTML = g.getOption('title');
+    this.title_div_.style.textAlign = 'center';
+    this.title_div_.style.fontSize = (g.getOption('titleHeight') - 8) + 'px';
+    this.title_div_.style.fontWeight = 'bold';
+    div.appendChild(this.title_div_);
+  }
+
+/*
+  if (g.getOption('xlabel')) {
+    var x_rect = e.reserveSpaceBottom(g.getOption('xLabelHeight'));
+  }
+
+  if (g.getOption('ylabel')) {
+    var y_rect = e.reserveSpaceLeft(0);
+  }
+
+  if (g.getOption('y2label')) {
+    var y2_rect = e.reserveSpaceRight(0);
+  }
+  */
+};
+
+chart_labels.prototype.destroy = function() {
+  this.title_div_ = null;
+  this.xlabel_div_ = null;
+  this.ylabel_div_ = null;
+  this.y2label_div_ = null;
+};
+
+
+return chart_labels;
+})();
index a7d8e6a..9490dfc 100644 (file)
@@ -1,4 +1,5 @@
 // TODO(danvk): move this into the top-level directory. Only plugins here.
 Dygraph.PLUGINS.push(
-  Dygraph.Plugins.Legend
+  Dygraph.Plugins.Legend,
+  Dygraph.Plugins.ChartLabels
 );