Rework the README a bit
[dygraphs.git] / src / 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
e0269a3d 9'use strict';
13f8b047 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 17Options left to make axis-friendly.
b67b868c
RK
18 ('drawAxesAtZero')
19 ('xAxisHeight')
f8540c66
DV
20*/
21
f0e47200
DV
22import * as utils from '../dygraph-utils';
23
f8540c66
DV
24/**
25 * Draws the axes. This includes the labels on the x- and y-axes, as well
26 * as the tick marks on the axes.
27 * It does _not_ draw the grid lines which span the entire chart.
28 */
29var axes = function() {
30 this.xlabels_ = [];
31 this.ylabels_ = [];
32};
33
34axes.prototype.toString = function() {
e0269a3d 35 return 'Axes Plugin';
f8540c66
DV
36};
37
38axes.prototype.activate = function(g) {
39 return {
40 layout: this.layout,
41 clearChart: this.clearChart,
98eb4713 42 willDrawChart: this.willDrawChart
f8540c66
DV
43 };
44};
45
46axes.prototype.layout = function(e) {
47 var g = e.dygraph;
48
7f6a7190 49 if (g.getOptionForAxis('drawAxis', 'y')) {
e0269a3d 50 var w = g.getOptionForAxis('axisLabelWidth', 'y') + 2 * g.getOptionForAxis('axisTickSize', 'y');
0cd1ad15 51 e.reserveSpaceLeft(w);
f8540c66
DV
52 }
53
7f6a7190 54 if (g.getOptionForAxis('drawAxis', 'x')) {
f8540c66 55 var h;
31c87125
RK
56 // NOTE: I think this is probably broken now, since g.getOption() now
57 // hits the dictionary. (That is, g.getOption('xAxisHeight') now always
58 // has a value.)
f8540c66
DV
59 if (g.getOption('xAxisHeight')) {
60 h = g.getOption('xAxisHeight');
61 } else {
e0269a3d 62 h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOptionForAxis('axisTickSize', 'x');
f8540c66 63 }
0cd1ad15 64 e.reserveSpaceBottom(h);
f8540c66
DV
65 }
66
67 if (g.numAxes() == 2) {
e0269a3d
DV
68 if (g.getOptionForAxis('drawAxis', 'y2')) {
69 var w = g.getOptionForAxis('axisLabelWidth', 'y2') + 2 * g.getOptionForAxis('axisTickSize', 'y2');
9f890c23
DV
70 e.reserveSpaceRight(w);
71 }
f8540c66 72 } else if (g.numAxes() > 2) {
e0269a3d
DV
73 g.error('Only two y-axes are supported at this time. (Trying ' +
74 'to use ' + g.numAxes() + ')');
f8540c66
DV
75 }
76};
77
78axes.prototype.detachLabels = function() {
79 function removeArray(ary) {
80 for (var i = 0; i < ary.length; i++) {
81 var el = ary[i];
82 if (el.parentNode) el.parentNode.removeChild(el);
83 }
84 }
85
86 removeArray(this.xlabels_);
87 removeArray(this.ylabels_);
88 this.xlabels_ = [];
89 this.ylabels_ = [];
90};
91
92axes.prototype.clearChart = function(e) {
f8540c66 93 this.detachLabels();
42a9ebb8 94};
f8540c66 95
98eb4713 96axes.prototype.willDrawChart = function(e) {
f8540c66 97 var g = e.dygraph;
7f6a7190 98
e0269a3d
DV
99 if (!g.getOptionForAxis('drawAxis', 'x') &&
100 !g.getOptionForAxis('drawAxis', 'y') &&
101 !g.getOptionForAxis('drawAxis', 'y2')) {
102 return;
103 }
bd6ee5dc 104
f8540c66
DV
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;
7c39bb3a
DV
111 var canvasWidth = g.width_; // e.canvas.width is affected by pixel ratio.
112 var canvasHeight = g.height_;
f8540c66
DV
113
114 var label, x, y, tick, i;
115
48dc3815
RK
116 var makeLabelStyle = function(axis) {
117 return {
e0269a3d
DV
118 position: 'absolute',
119 fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + 'px',
e0269a3d 120 width: g.getOptionForAxis('axisLabelWidth', axis) + 'px',
48dc3815 121 };
83b0c192 122 };
48dc3815
RK
123
124 var labelStyles = {
f0e47200
DV
125 x: makeLabelStyle('x'),
126 y: makeLabelStyle('y'),
127 y2: makeLabelStyle('y2')
f8540c66 128 };
48dc3815 129
f8540c66 130 var makeDiv = function(txt, axis, prec_axis) {
48dc3815 131 /*
7e1d5659 132 * This seems to be called with the following three sets of axis/prec_axis:
48dc3815
RK
133 * x: undefined
134 * y: y1
135 * y: y2
136 */
e0269a3d 137 var div = document.createElement('div');
48dc3815 138 var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis];
f0e47200
DV
139 utils.update(div.style, labelStyle);
140 // TODO: combine outer & inner divs
e0269a3d 141 var inner_div = document.createElement('div');
f8540c66
DV
142 inner_div.className = 'dygraph-axis-label' +
143 ' dygraph-axis-label-' + axis +
144 (prec_axis ? ' dygraph-axis-label-' + prec_axis : '');
145 inner_div.innerHTML = txt;
146 div.appendChild(inner_div);
147 return div;
148 };
149
150 // axis lines
151 context.save();
f8540c66
DV
152
153 var layout = g.layout_;
154 var area = e.dygraph.plotter_.area;
155
e0269a3d
DV
156 // Helper for repeated axis-option accesses.
157 var makeOptionGetter = function(axis) {
158 return function(option) {
159 return g.getOptionForAxis(option, axis);
160 };
161 };
162
7f6a7190 163 if (g.getOptionForAxis('drawAxis', 'y')) {
f8540c66
DV
164 if (layout.yticks && layout.yticks.length > 0) {
165 var num_axes = g.numAxes();
e0269a3d 166 var getOptions = [makeOptionGetter('y'), makeOptionGetter('y2')];
fd6b8dad
DV
167 layout.yticks.forEach(tick => {
168 if (tick.label === undefined) return; // this tick only has a grid line.
f8540c66
DV
169 x = area.x;
170 var sgn = 1;
171 var prec_axis = 'y1';
e0269a3d 172 var getAxisOption = getOptions[0];
bd6ee5dc 173 if (tick.axis == 1) { // right-side y-axis
f8540c66
DV
174 x = area.x + area.w;
175 sgn = -1;
176 prec_axis = 'y2';
e0269a3d 177 getAxisOption = getOptions[1];
f8540c66 178 }
e0269a3d 179 var fontSize = getAxisOption('axisLabelFontSize');
bd6ee5dc 180 y = area.y + tick.pos * area.h;
f8540c66
DV
181
182 /* Tick marks are currently clipped, so don't bother drawing them.
183 context.beginPath();
184 context.moveTo(halfUp(x), halfDown(y));
185 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
186 context.closePath();
187 context.stroke();
188 */
189
bd6ee5dc 190 label = makeDiv(tick.label, 'y', num_axes == 2 ? prec_axis : null);
48dc3815 191 var top = (y - fontSize / 2);
f8540c66
DV
192 if (top < 0) top = 0;
193
48dc3815 194 if (top + fontSize + 3 > canvasHeight) {
e0269a3d 195 label.style.bottom = '0';
f8540c66 196 } else {
e0269a3d 197 label.style.top = top + 'px';
f8540c66 198 }
f0e47200 199 // TODO: replace these with css classes?
bd6ee5dc 200 if (tick.axis === 0) {
e0269a3d
DV
201 label.style.left = (area.x - getAxisOption('axisLabelWidth') - getAxisOption('axisTickSize')) + 'px';
202 label.style.textAlign = 'right';
bd6ee5dc 203 } else if (tick.axis == 1) {
f8540c66 204 label.style.left = (area.x + area.w +
e0269a3d
DV
205 getAxisOption('axisTickSize')) + 'px';
206 label.style.textAlign = 'left';
f8540c66 207 }
e0269a3d 208 label.style.width = getAxisOption('axisLabelWidth') + 'px';
f8540c66
DV
209 containerDiv.appendChild(label);
210 this.ylabels_.push(label);
fd6b8dad 211 });
f8540c66
DV
212
213 // The lowest tick on the y-axis often overlaps with the leftmost
214 // tick on the x-axis. Shift the bottom tick up a little bit to
215 // compensate if necessary.
216 var bottomTick = this.ylabels_[0];
48dc3815 217 // Interested in the y2 axis also?
e0269a3d 218 var fontSize = g.getOptionForAxis('axisLabelFontSize', 'y');
f8540c66 219 var bottom = parseInt(bottomTick.style.top, 10) + fontSize;
beeabac2 220 if (bottom > canvasHeight - fontSize) {
f8540c66 221 bottomTick.style.top = (parseInt(bottomTick.style.top, 10) -
e0269a3d 222 fontSize / 2) + 'px';
f8540c66
DV
223 }
224 }
225
226 // draw a vertical line on the left to separate the chart from the labels.
227 var axisX;
228 if (g.getOption('drawAxesAtZero')) {
beeabac2 229 var r = g.toPercentXCoord(0);
33e96f11 230 if (r > 1 || r < 0 || isNaN(r)) r = 0;
f8540c66
DV
231 axisX = halfUp(area.x + r * area.w);
232 } else {
233 axisX = halfUp(area.x);
234 }
b67b868c
RK
235
236 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y');
237 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y');
238
f8540c66
DV
239 context.beginPath();
240 context.moveTo(axisX, halfDown(area.y));
241 context.lineTo(axisX, halfDown(area.y + area.h));
242 context.closePath();
243 context.stroke();
244
245 // if there's a secondary y-axis, draw a vertical line for that, too.
246 if (g.numAxes() == 2) {
b67b868c
RK
247 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2');
248 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2');
f8540c66
DV
249 context.beginPath();
250 context.moveTo(halfDown(area.x + area.w), halfDown(area.y));
251 context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h));
252 context.closePath();
253 context.stroke();
254 }
255 }
256
7f6a7190 257 if (g.getOptionForAxis('drawAxis', 'x')) {
f8540c66 258 if (layout.xticks) {
e0269a3d 259 var getAxisOption = makeOptionGetter('x');
fd6b8dad
DV
260 layout.xticks.forEach(tick => {
261 if (tick.label === undefined) return; // this tick only has a grid line.
bd6ee5dc 262 x = area.x + tick.pos * area.w;
f8540c66
DV
263 y = area.y + area.h;
264
265 /* Tick marks are currently clipped, so don't bother drawing them.
266 context.beginPath();
267 context.moveTo(halfUp(x), halfDown(y));
268 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
269 context.closePath();
270 context.stroke();
271 */
272
bd6ee5dc 273 label = makeDiv(tick.label, 'x');
e0269a3d
DV
274 label.style.textAlign = 'center';
275 label.style.top = (y + getAxisOption('axisTickSize')) + 'px';
f8540c66 276
e0269a3d
DV
277 var left = (x - getAxisOption('axisLabelWidth')/2);
278 if (left + getAxisOption('axisLabelWidth') > canvasWidth) {
279 left = canvasWidth - getAxisOption('axisLabelWidth');
280 label.style.textAlign = 'right';
f8540c66
DV
281 }
282 if (left < 0) {
283 left = 0;
e0269a3d 284 label.style.textAlign = 'left';
f8540c66
DV
285 }
286
e0269a3d
DV
287 label.style.left = left + 'px';
288 label.style.width = getAxisOption('axisLabelWidth') + 'px';
f8540c66
DV
289 containerDiv.appendChild(label);
290 this.xlabels_.push(label);
fd6b8dad 291 });
f8540c66
DV
292 }
293
b67b868c
RK
294 context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
295 context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x');
f8540c66
DV
296 context.beginPath();
297 var axisY;
298 if (g.getOption('drawAxesAtZero')) {
299 var r = g.toPercentYCoord(0, 0);
300 if (r > 1 || r < 0) r = 1;
301 axisY = halfDown(area.y + r * area.h);
302 } else {
303 axisY = halfDown(area.y + area.h);
304 }
305 context.moveTo(halfUp(area.x), axisY);
306 context.lineTo(halfUp(area.x + area.w), axisY);
307 context.closePath();
308 context.stroke();
309 }
310
311 context.restore();
42a9ebb8 312};
f8540c66 313
6ecc0739 314export default axes;