Added new fillStepPlot option for dygraph
[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
7f6a7190 55 if (g.getOptionForAxis('drawAxis', 'y')) {
f8540c66 56 var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
0cd1ad15 57 e.reserveSpaceLeft(w);
f8540c66
DV
58 }
59
7f6a7190 60 if (g.getOptionForAxis('drawAxis', 'x')) {
f8540c66 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) {
9f890c23 74 // TODO(danvk): introduce a 'drawAxis' per-axis property.
7f6a7190 75 if (g.getOptionForAxis('drawAxis', 'y')) {
9f890c23
DV
76 // TODO(danvk): per-axis setting.
77 var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
78 e.reserveSpaceRight(w);
79 }
f8540c66
DV
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
86axes.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
100axes.prototype.clearChart = function(e) {
f8540c66 101 this.detachLabels();
42a9ebb8 102};
f8540c66 103
98eb4713 104axes.prototype.willDrawChart = function(e) {
f8540c66 105 var g = e.dygraph;
7f6a7190
RK
106
107 if (!g.getOptionForAxis('drawAxis', 'x') && !g.getOptionForAxis('drawAxis', 'y')) return;
f8540c66
DV
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;
beeabac2
DV
115 var canvasWidth = e.canvas.width;
116 var canvasHeight = e.canvas.height;
f8540c66
DV
117
118 var label, x, y, tick, i;
119
48dc3815
RK
120 var makeLabelStyle = function(axis) {
121 return {
122 position: "absolute",
123 fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px",
124 zIndex: 10,
3a84670d 125 color: g.getOptionForAxis('axisLabelColor', axis),
48dc3815
RK
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 };
83b0c192 131 };
48dc3815
RK
132
133 var labelStyles = {
134 x : makeLabelStyle('x'),
135 y : makeLabelStyle('y'),
83b0c192 136 y2 : makeLabelStyle('y2')
f8540c66 137 };
48dc3815 138
f8540c66 139 var makeDiv = function(txt, axis, prec_axis) {
48dc3815 140 /*
7e1d5659 141 * This seems to be called with the following three sets of axis/prec_axis:
48dc3815
RK
142 * x: undefined
143 * y: y1
144 * y: y2
145 */
f8540c66 146 var div = document.createElement("div");
48dc3815 147 var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis];
f8540c66
DV
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();
f8540c66
DV
164
165 var layout = g.layout_;
166 var area = e.dygraph.plotter_.area;
167
7f6a7190 168 if (g.getOptionForAxis('drawAxis', 'y')) {
f8540c66
DV
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 }
48dc3815 182 var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis);
f8540c66
DV
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);
48dc3815 194 var top = (y - fontSize / 2);
f8540c66
DV
195 if (top < 0) top = 0;
196
48dc3815 197 if (top + fontSize + 3 > canvasHeight) {
f8540c66
DV
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];
48dc3815
RK
219 // Interested in the y2 axis also?
220 var fontSize = g.getOptionForAxis('axisLabelFontSize', "y");
f8540c66 221 var bottom = parseInt(bottomTick.style.top, 10) + fontSize;
beeabac2 222 if (bottom > canvasHeight - fontSize) {
f8540c66
DV
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')) {
beeabac2 231 var r = g.toPercentXCoord(0);
33e96f11 232 if (r > 1 || r < 0 || isNaN(r)) r = 0;
f8540c66
DV
233 axisX = halfUp(area.x + r * area.w);
234 } else {
235 axisX = halfUp(area.x);
236 }
b67b868c
RK
237
238 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y');
239 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y');
240
f8540c66
DV
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) {
b67b868c
RK
249 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2');
250 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2');
f8540c66
DV
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
7f6a7190 259 if (g.getOptionForAxis('drawAxis', 'x')) {
f8540c66
DV
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);
beeabac2
DV
279 if (left + g.getOption('axisLabelWidth') > canvasWidth) {
280 left = canvasWidth - g.getOption('xAxisLabelWidth');
f8540c66
DV
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
b67b868c
RK
295 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
296 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x');
f8540c66
DV
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();
42a9ebb8 313};
f8540c66
DV
314
315return axes;
316})();