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