@protected
[dygraphs.git] / plugins / axes.js
1 /**
2 * @license
3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
6
7 /*global Dygraph:false */
8
9 Dygraph.Plugins.Axes = (function() {
10
11 "use strict";
12
13 /*
14 Bits of jankiness:
15 - Direct layout access
16 - Direct area access
17 - Should include calculation of ticks, not just the drawing.
18
19 Options left to make axis-friendly.
20 ('axisTickSize')
21 ('drawAxesAtZero')
22 ('xAxisHeight')
23
24 These too. What is the difference between axisLablelWidth and {x,y}AxisLabelWidth?
25 ('axisLabelWidth')
26 ('xAxisLabelWidth')
27 ('yAxisLabelWidth')
28 */
29
30 /**
31 * Draws the axes. This includes the labels on the x- and y-axes, as well
32 * as the tick marks on the axes.
33 * It does _not_ draw the grid lines which span the entire chart.
34 */
35 var axes = function() {
36 this.xlabels_ = [];
37 this.ylabels_ = [];
38 };
39
40 axes.prototype.toString = function() {
41 return "Axes Plugin";
42 };
43
44 axes.prototype.activate = function(g) {
45 return {
46 layout: this.layout,
47 clearChart: this.clearChart,
48 willDrawChart: this.willDrawChart
49 };
50 };
51
52 axes.prototype.layout = function(e) {
53 var g = e.dygraph;
54
55 if (g.getOptionForAxis('drawAxis', 'y')) {
56 var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
57 e.reserveSpaceLeft(w);
58 }
59
60 if (g.getOptionForAxis('drawAxis', 'x')) {
61 var h;
62 // NOTE: I think this is probably broken now, since g.getOption() now
63 // hits the dictionary. (That is, g.getOption('xAxisHeight') now always
64 // has a value.)
65 if (g.getOption('xAxisHeight')) {
66 h = g.getOption('xAxisHeight');
67 } else {
68 h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOption('axisTickSize');
69 }
70 e.reserveSpaceBottom(h);
71 }
72
73 if (g.numAxes() == 2) {
74 // TODO(danvk): introduce a 'drawAxis' per-axis property.
75 if (g.getOptionForAxis('drawAxis', 'y')) {
76 // TODO(danvk): per-axis setting.
77 var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
78 e.reserveSpaceRight(w);
79 }
80 } else if (g.numAxes() > 2) {
81 g.error("Only two y-axes are supported at this time. (Trying " +
82 "to use " + g.numAxes() + ")");
83 }
84 };
85
86 axes.prototype.detachLabels = function() {
87 function removeArray(ary) {
88 for (var i = 0; i < ary.length; i++) {
89 var el = ary[i];
90 if (el.parentNode) el.parentNode.removeChild(el);
91 }
92 }
93
94 removeArray(this.xlabels_);
95 removeArray(this.ylabels_);
96 this.xlabels_ = [];
97 this.ylabels_ = [];
98 };
99
100 axes.prototype.clearChart = function(e) {
101 this.detachLabels();
102 };
103
104 axes.prototype.willDrawChart = function(e) {
105 var g = e.dygraph;
106
107 if (!g.getOptionForAxis('drawAxis', 'x') && !g.getOptionForAxis('drawAxis', 'y')) return;
108
109 // Round pixels to half-integer boundaries for crisper drawing.
110 function halfUp(x) { return Math.round(x) + 0.5; }
111 function halfDown(y){ return Math.round(y) - 0.5; }
112
113 var context = e.drawingContext;
114 var containerDiv = e.canvas.parentNode;
115 var canvasWidth = e.canvas.width;
116 var canvasHeight = e.canvas.height;
117
118 var label, x, y, tick, i;
119
120 var makeLabelStyle = function(axis) {
121 return {
122 position: "absolute",
123 fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px",
124 zIndex: 10,
125 color: g.getOptionForAxis('axisLabelColor', axis),
126 width: g.getOption('axisLabelWidth') + "px",
127 // height: g.getOptionForAxis('axisLabelFontSize', 'x') + 2 + "px",
128 lineHeight: "normal", // Something other than "normal" line-height screws up label positioning.
129 overflow: "hidden"
130 };
131 };
132
133 var labelStyles = {
134 x : makeLabelStyle('x'),
135 y : makeLabelStyle('y'),
136 y2 : makeLabelStyle('y2')
137 };
138
139 var makeDiv = function(txt, axis, prec_axis) {
140 /*
141 * This seems to be called with the following three sets of axis/prec_axis:
142 * x: undefined
143 * y: y1
144 * y: y2
145 */
146 var div = document.createElement("div");
147 var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis];
148 for (var name in labelStyle) {
149 if (labelStyle.hasOwnProperty(name)) {
150 div.style[name] = labelStyle[name];
151 }
152 }
153 var inner_div = document.createElement("div");
154 inner_div.className = 'dygraph-axis-label' +
155 ' dygraph-axis-label-' + axis +
156 (prec_axis ? ' dygraph-axis-label-' + prec_axis : '');
157 inner_div.innerHTML = txt;
158 div.appendChild(inner_div);
159 return div;
160 };
161
162 // axis lines
163 context.save();
164
165 var layout = g.layout_;
166 var area = e.dygraph.plotter_.area;
167
168 if (g.getOptionForAxis('drawAxis', 'y')) {
169 if (layout.yticks && layout.yticks.length > 0) {
170 var num_axes = g.numAxes();
171 for (i = 0; i < layout.yticks.length; i++) {
172 tick = layout.yticks[i];
173 if (typeof(tick) == "function") return;
174 x = area.x;
175 var sgn = 1;
176 var prec_axis = 'y1';
177 if (tick[0] == 1) { // right-side y-axis
178 x = area.x + area.w;
179 sgn = -1;
180 prec_axis = 'y2';
181 }
182 var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis);
183 y = area.y + tick[1] * area.h;
184
185 /* Tick marks are currently clipped, so don't bother drawing them.
186 context.beginPath();
187 context.moveTo(halfUp(x), halfDown(y));
188 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
189 context.closePath();
190 context.stroke();
191 */
192
193 label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null);
194 var top = (y - fontSize / 2);
195 if (top < 0) top = 0;
196
197 if (top + fontSize + 3 > canvasHeight) {
198 label.style.bottom = "0px";
199 } else {
200 label.style.top = top + "px";
201 }
202 if (tick[0] === 0) {
203 label.style.left = (area.x - g.getOption('yAxisLabelWidth') - g.getOption('axisTickSize')) + "px";
204 label.style.textAlign = "right";
205 } else if (tick[0] == 1) {
206 label.style.left = (area.x + area.w +
207 g.getOption('axisTickSize')) + "px";
208 label.style.textAlign = "left";
209 }
210 label.style.width = g.getOption('yAxisLabelWidth') + "px";
211 containerDiv.appendChild(label);
212 this.ylabels_.push(label);
213 }
214
215 // The lowest tick on the y-axis often overlaps with the leftmost
216 // tick on the x-axis. Shift the bottom tick up a little bit to
217 // compensate if necessary.
218 var bottomTick = this.ylabels_[0];
219 // Interested in the y2 axis also?
220 var fontSize = g.getOptionForAxis('axisLabelFontSize', "y");
221 var bottom = parseInt(bottomTick.style.top, 10) + fontSize;
222 if (bottom > canvasHeight - fontSize) {
223 bottomTick.style.top = (parseInt(bottomTick.style.top, 10) -
224 fontSize / 2) + "px";
225 }
226 }
227
228 // draw a vertical line on the left to separate the chart from the labels.
229 var axisX;
230 if (g.getOption('drawAxesAtZero')) {
231 var r = g.toPercentXCoord(0);
232 if (r > 1 || r < 0 || isNaN(r)) r = 0;
233 axisX = halfUp(area.x + r * area.w);
234 } else {
235 axisX = halfUp(area.x);
236 }
237
238 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y');
239 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y');
240
241 context.beginPath();
242 context.moveTo(axisX, halfDown(area.y));
243 context.lineTo(axisX, halfDown(area.y + area.h));
244 context.closePath();
245 context.stroke();
246
247 // if there's a secondary y-axis, draw a vertical line for that, too.
248 if (g.numAxes() == 2) {
249 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2');
250 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2');
251 context.beginPath();
252 context.moveTo(halfDown(area.x + area.w), halfDown(area.y));
253 context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h));
254 context.closePath();
255 context.stroke();
256 }
257 }
258
259 if (g.getOptionForAxis('drawAxis', 'x')) {
260 if (layout.xticks) {
261 for (i = 0; i < layout.xticks.length; i++) {
262 tick = layout.xticks[i];
263 x = area.x + tick[0] * area.w;
264 y = area.y + area.h;
265
266 /* Tick marks are currently clipped, so don't bother drawing them.
267 context.beginPath();
268 context.moveTo(halfUp(x), halfDown(y));
269 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
270 context.closePath();
271 context.stroke();
272 */
273
274 label = makeDiv(tick[1], 'x');
275 label.style.textAlign = "center";
276 label.style.top = (y + g.getOption('axisTickSize')) + 'px';
277
278 var left = (x - g.getOption('axisLabelWidth')/2);
279 if (left + g.getOption('axisLabelWidth') > canvasWidth) {
280 left = canvasWidth - g.getOption('xAxisLabelWidth');
281 label.style.textAlign = "right";
282 }
283 if (left < 0) {
284 left = 0;
285 label.style.textAlign = "left";
286 }
287
288 label.style.left = left + "px";
289 label.style.width = g.getOption('xAxisLabelWidth') + "px";
290 containerDiv.appendChild(label);
291 this.xlabels_.push(label);
292 }
293 }
294
295 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
296 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x');
297 context.beginPath();
298 var axisY;
299 if (g.getOption('drawAxesAtZero')) {
300 var r = g.toPercentYCoord(0, 0);
301 if (r > 1 || r < 0) r = 1;
302 axisY = halfDown(area.y + r * area.h);
303 } else {
304 axisY = halfDown(area.y + area.h);
305 }
306 context.moveTo(halfUp(area.x), axisY);
307 context.lineTo(halfUp(area.x + area.w), axisY);
308 context.closePath();
309 context.stroke();
310 }
311
312 context.restore();
313 };
314
315 return axes;
316 })();