| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2012 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | Dygraph.Plugins.ChartLabels = (function() { |
| 7 | |
| 8 | // TODO(danvk): move chart label options out of dygraphs and into the plugin. |
| 9 | |
| 10 | var chart_labels = function() { |
| 11 | this.title_div_ = null; |
| 12 | this.xlabel_div_ = null; |
| 13 | this.ylabel_div_ = null; |
| 14 | this.y2label_div_ = null; |
| 15 | }; |
| 16 | |
| 17 | chart_labels.prototype.toString = function() { |
| 18 | return "ChartLabels Plugin"; |
| 19 | }; |
| 20 | |
| 21 | chart_labels.prototype.activate = function(g) { |
| 22 | return { |
| 23 | layout: this.layout, |
| 24 | // clearChart: this.clearChart, |
| 25 | drawChart: this.drawChart |
| 26 | }; |
| 27 | }; |
| 28 | |
| 29 | // QUESTION: should there be a plugin-utils.js? |
| 30 | var createDivInRect = function(r) { |
| 31 | var div = document.createElement('div'); |
| 32 | div.style.position = 'absolute'; |
| 33 | div.style.left = r.x + 'px'; |
| 34 | div.style.top = r.y + 'px'; |
| 35 | div.style.width = r.w + 'px'; |
| 36 | div.style.height = r.h + 'px'; |
| 37 | return div; |
| 38 | }; |
| 39 | |
| 40 | // Detach and null out any existing nodes. |
| 41 | chart_labels.prototype.detachLabels_ = function() { |
| 42 | var els = [ this.title_div_, |
| 43 | this.xlabel_div_, |
| 44 | this.ylabel_div_, |
| 45 | this.y2label_div_ ]; |
| 46 | for (var i = 0; i < els.length; i++) { |
| 47 | var el = els[i]; |
| 48 | if (!el) continue; |
| 49 | if (el.parentNode) el.parentNode.removeChild(el); |
| 50 | } |
| 51 | |
| 52 | this.title_div_ = null; |
| 53 | this.xlabel_div_ = null; |
| 54 | this.ylabel_div_ = null; |
| 55 | this.y2label_div_ = null; |
| 56 | }; |
| 57 | |
| 58 | chart_labels.prototype.layout = function(e) { |
| 59 | this.detachLabels_(); |
| 60 | |
| 61 | var g = e.dygraph; |
| 62 | var div = e.chart_div; |
| 63 | if (g.getOption('title')) { |
| 64 | // QUESTION: should this return an absolutely-positioned div instead? |
| 65 | var title_rect = e.reserveSpaceTop(g.getOption('titleHeight')); |
| 66 | this.title_div_ = createDivInRect(title_rect); |
| 67 | this.title_div_.innerHTML = g.getOption('title'); |
| 68 | this.title_div_.style.textAlign = 'center'; |
| 69 | this.title_div_.style.fontSize = (g.getOption('titleHeight') - 8) + 'px'; |
| 70 | this.title_div_.style.fontWeight = 'bold'; |
| 71 | div.appendChild(this.title_div_); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | if (g.getOption('xlabel')) { |
| 76 | var x_rect = e.reserveSpaceBottom(g.getOption('xLabelHeight')); |
| 77 | } |
| 78 | |
| 79 | if (g.getOption('ylabel')) { |
| 80 | var y_rect = e.reserveSpaceLeft(0); |
| 81 | } |
| 82 | |
| 83 | if (g.getOption('y2label')) { |
| 84 | var y2_rect = e.reserveSpaceRight(0); |
| 85 | } |
| 86 | */ |
| 87 | }; |
| 88 | |
| 89 | chart_labels.prototype.drawChart = function(e) { |
| 90 | var g = e.dygraph; |
| 91 | if (this.title_div_) { |
| 92 | this.title_div_.innerHTML = g.getOption('title'); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | chart_labels.prototype.clearChart = function() { |
| 97 | }; |
| 98 | |
| 99 | chart_labels.prototype.destroy = function() { |
| 100 | detachLabels(); |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | return chart_labels; |
| 105 | })(); |