exclude externs from lint
[dygraphs.git] / plugins / chart-labels.js
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 "use strict";
9
10 // TODO(danvk): move chart label options out of dygraphs and into the plugin.
11 // TODO(danvk): only tear down & rebuild the DIVs when it's necessary.
12
13 var chart_labels = function() {
14 this.title_div_ = null;
15 this.xlabel_div_ = null;
16 this.ylabel_div_ = null;
17 this.y2label_div_ = null;
18 };
19
20 chart_labels.prototype.toString = function() {
21 return "ChartLabels Plugin";
22 };
23
24 chart_labels.prototype.activate = function(g) {
25 return {
26 layout: this.layout,
27 // clearChart: this.clearChart,
28 didDrawChart: this.didDrawChart
29 };
30 };
31
32 // QUESTION: should there be a plugin-utils.js?
33 var createDivInRect = function(r) {
34 var div = document.createElement('div');
35 div.style.position = 'absolute';
36 div.style.left = r.x + 'px';
37 div.style.top = r.y + 'px';
38 div.style.width = r.w + 'px';
39 div.style.height = r.h + 'px';
40 return div;
41 };
42
43 // Detach and null out any existing nodes.
44 chart_labels.prototype.detachLabels_ = function() {
45 var els = [ this.title_div_,
46 this.xlabel_div_,
47 this.ylabel_div_,
48 this.y2label_div_ ];
49 for (var i = 0; i < els.length; i++) {
50 var el = els[i];
51 if (!el) continue;
52 if (el.parentNode) el.parentNode.removeChild(el);
53 }
54
55 this.title_div_ = null;
56 this.xlabel_div_ = null;
57 this.ylabel_div_ = null;
58 this.y2label_div_ = null;
59 };
60
61 var createRotatedDiv = function(g, box, axis, classes, html) {
62 // TODO(danvk): is this outer div actually necessary?
63 var div = document.createElement("div");
64 div.style.position = 'absolute';
65 if (axis == 1) {
66 // NOTE: this is cheating. Should be positioned relative to the box.
67 div.style.left = '0px';
68 } else {
69 div.style.left = box.x + 'px';
70 }
71 div.style.top = box.y + 'px';
72 div.style.width = box.w + 'px';
73 div.style.height = box.h + 'px';
74 div.style.fontSize = (g.getOption('yLabelWidth') - 2) + 'px';
75
76 var inner_div = document.createElement("div");
77 inner_div.style.position = 'absolute';
78 inner_div.style.width = box.h + 'px';
79 inner_div.style.height = box.w + 'px';
80 inner_div.style.top = (box.h / 2 - box.w / 2) + 'px';
81 inner_div.style.left = (box.w / 2 - box.h / 2) + 'px';
82 inner_div.style.textAlign = 'center';
83
84 // CSS rotation is an HTML5 feature which is not standardized. Hence every
85 // browser has its own name for the CSS style.
86 var val = 'rotate(' + (axis == 1 ? '-' : '') + '90deg)';
87 inner_div.style.transform = val; // HTML5
88 inner_div.style.WebkitTransform = val; // Safari/Chrome
89 inner_div.style.MozTransform = val; // Firefox
90 inner_div.style.OTransform = val; // Opera
91 inner_div.style.msTransform = val; // IE9
92
93 if (typeof(document.documentMode) !== 'undefined' &&
94 document.documentMode < 9) {
95 // We're dealing w/ an old version of IE, so we have to rotate the text
96 // using a BasicImage transform. This uses a different origin of rotation
97 // than HTML5 rotation (top left of div vs. its center).
98 inner_div.style.filter =
99 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' +
100 (axis == 1 ? '3' : '1') + ')';
101 inner_div.style.left = '0px';
102 inner_div.style.top = '0px';
103 }
104
105 var class_div = document.createElement("div");
106 class_div.className = classes;
107 class_div.innerHTML = html;
108
109 inner_div.appendChild(class_div);
110 div.appendChild(inner_div);
111 return div;
112 };
113
114 chart_labels.prototype.layout = function(e) {
115 this.detachLabels_();
116
117 var g = e.dygraph;
118 var div = e.chart_div;
119 if (g.getOption('title')) {
120 // QUESTION: should this return an absolutely-positioned div instead?
121 var title_rect = e.reserveSpaceTop(g.getOption('titleHeight'));
122 this.title_div_ = createDivInRect(title_rect);
123 this.title_div_.style.textAlign = 'center';
124 this.title_div_.style.fontSize = (g.getOption('titleHeight') - 8) + 'px';
125 this.title_div_.style.fontWeight = 'bold';
126 this.title_div_.style.zIndex = 10;
127
128 var class_div = document.createElement("div");
129 class_div.className = 'dygraph-label dygraph-title';
130 class_div.innerHTML = g.getOption('title');
131 this.title_div_.appendChild(class_div);
132 div.appendChild(this.title_div_);
133 }
134
135 if (g.getOption('xlabel')) {
136 var x_rect = e.reserveSpaceBottom(g.getOption('xLabelHeight'));
137 this.xlabel_div_ = createDivInRect(x_rect);
138 this.xlabel_div_.style.textAlign = 'center';
139 this.xlabel_div_.style.fontSize = (g.getOption('xLabelHeight') - 2) + 'px';
140
141 var class_div = document.createElement("div");
142 class_div.className = 'dygraph-label dygraph-xlabel';
143 class_div.innerHTML = g.getOption('xlabel');
144 this.xlabel_div_.appendChild(class_div);
145 div.appendChild(this.xlabel_div_);
146 }
147
148 if (g.getOption('ylabel')) {
149 // It would make sense to shift the chart here to make room for the y-axis
150 // label, but the default yAxisLabelWidth is large enough that this results
151 // in overly-padded charts. The y-axis label should fit fine. If it
152 // doesn't, the yAxisLabelWidth option can be increased.
153 var y_rect = e.reserveSpaceLeft(0);
154
155 this.ylabel_div_ = createRotatedDiv(
156 g, y_rect,
157 1, // primary (left) y-axis
158 'dygraph-label dygraph-ylabel',
159 g.getOption('ylabel'));
160 div.appendChild(this.ylabel_div_);
161 }
162
163 if (g.getOption('y2label') && g.numAxes() == 2) {
164 // same logic applies here as for ylabel.
165 var y2_rect = e.reserveSpaceRight(0);
166 this.y2label_div_ = createRotatedDiv(
167 g, y2_rect,
168 2, // secondary (right) y-axis
169 'dygraph-label dygraph-y2label',
170 g.getOption('y2label'));
171 div.appendChild(this.y2label_div_);
172 }
173 };
174
175 chart_labels.prototype.didDrawChart = function(e) {
176 var g = e.dygraph;
177 if (this.title_div_) {
178 this.title_div_.children[0].innerHTML = g.getOption('title');
179 }
180 if (this.xlabel_div_) {
181 this.xlabel_div_.children[0].innerHTML = g.getOption('xlabel');
182 }
183 if (this.ylabel_div_) {
184 this.ylabel_div_.children[0].children[0].innerHTML = g.getOption('ylabel');
185 }
186 if (this.y2label_div_) {
187 this.y2label_div_.children[0].children[0].innerHTML = g.getOption('y2label');
188 }
189 };
190
191 chart_labels.prototype.clearChart = function() {
192 };
193
194 chart_labels.prototype.destroy = function() {
195 this.detachLabels_();
196 };
197
198
199 return chart_labels;
200 })();