Handling zoom handle left position as a floating-point number for better accuracy...
[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
7Dygraph.Plugins.Axes = (function() {
8
13f8b047
DV
9"use strict";
10
f8540c66 11/*
f8540c66
DV
12Bits of jankiness:
13- Direct layout access
14- Direct area access
15- Should include calculation of ticks, not just the drawing.
16
b67b868c
RK
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')
f8540c66
DV
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,
98eb4713 46 willDrawChart: this.willDrawChart
f8540c66
DV
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;
31c87125
RK
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.)
f8540c66
DV
63 if (g.getOption('xAxisHeight')) {
64 h = g.getOption('xAxisHeight');
65 } else {
48dc3815 66 h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOption('axisTickSize');
f8540c66
DV
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();
42a9ebb8 98};
f8540c66 99
98eb4713 100axes.prototype.willDrawChart = function(e) {
f8540c66
DV
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;
beeabac2
DV
110 var canvasWidth = e.canvas.width;
111 var canvasHeight = e.canvas.height;
f8540c66
DV
112
113 var label, x, y, tick, i;
114
48dc3815
RK
115 var makeLabelStyle = function(axis) {
116 return {
117 position: "absolute",
118 fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px",
119 zIndex: 10,
3a84670d 120 color: g.getOptionForAxis('axisLabelColor', axis),
48dc3815
RK
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'),
f8540c66 132 };
48dc3815 133
f8540c66 134 var makeDiv = function(txt, axis, prec_axis) {
48dc3815 135 /*
7e1d5659 136 * This seems to be called with the following three sets of axis/prec_axis:
48dc3815
RK
137 * x: undefined
138 * y: y1
139 * y: y2
140 */
f8540c66 141 var div = document.createElement("div");
48dc3815 142 var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis];
f8540c66
DV
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();
f8540c66
DV
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 }
48dc3815 177 var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis);
f8540c66
DV
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);
48dc3815 189 var top = (y - fontSize / 2);
f8540c66
DV
190 if (top < 0) top = 0;
191
48dc3815 192 if (top + fontSize + 3 > canvasHeight) {
f8540c66
DV
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];
48dc3815
RK
214 // Interested in the y2 axis also?
215 var fontSize = g.getOptionForAxis('axisLabelFontSize', "y");
f8540c66 216 var bottom = parseInt(bottomTick.style.top, 10) + fontSize;
beeabac2 217 if (bottom > canvasHeight - fontSize) {
f8540c66
DV
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')) {
beeabac2 226 var r = g.toPercentXCoord(0);
f8540c66
DV
227 if (r > 1 || r < 0) r = 0;
228 axisX = halfUp(area.x + r * area.w);
229 } else {
230 axisX = halfUp(area.x);
231 }
b67b868c
RK
232
233 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y');
234 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y');
235
f8540c66
DV
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) {
b67b868c
RK
244 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2');
245 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2');
f8540c66
DV
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);
beeabac2
DV
274 if (left + g.getOption('axisLabelWidth') > canvasWidth) {
275 left = canvasWidth - g.getOption('xAxisLabelWidth');
f8540c66
DV
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
b67b868c
RK
290 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
291 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x');
f8540c66
DV
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();
42a9ebb8 308};
f8540c66
DV
309
310return axes;
311})();