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