3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 /*global Dygraph:false */
9 Dygraph
.Plugins
.Axes
= (function() {
15 - Direct layout access
17 - Should include calculation of ticks, not just the drawing.
19 Options left to make axis-friendly.
24 These too. What is the difference between axisLablelWidth and {x,y}AxisLabelWidth?
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.
35 var axes
= function() {
40 axes
.prototype.toString
= function() {
44 axes
.prototype.activate
= function(g
) {
47 clearChart
: this.clearChart
,
48 willDrawChart
: this.willDrawChart
52 axes
.prototype.layout
= function(e
) {
55 if (g
.getOption('drawYAxis')) {
56 var w
= g
.getOption('yAxisLabelWidth') + 2 * g
.getOption('axisTickSize');
57 e
.reserveSpaceLeft(w
);
60 if (g
.getOption('drawXAxis')) {
62 // NOTE: I think this is probably broken now, since g.getOption() now
63 // hits the dictionary. (That is, g.getOption('xAxisHeight') now always
65 if (g
.getOption('xAxisHeight')) {
66 h
= g
.getOption('xAxisHeight');
68 h
= g
.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g
.getOption('axisTickSize');
70 e
.reserveSpaceBottom(h
);
73 if (g
.numAxes() == 2) {
74 // TODO(danvk): per-axis setting.
75 var w
= g
.getOption('yAxisLabelWidth') + 2 * g
.getOption('axisTickSize');
76 e
.reserveSpaceRight(w
);
77 } else if (g
.numAxes() > 2) {
78 g
.error("Only two y-axes are supported at this time. (Trying " +
79 "to use " + g
.numAxes() + ")");
83 axes
.prototype.detachLabels
= function() {
84 function removeArray(ary
) {
85 for (var i
= 0; i
< ary
.length
; i
++) {
87 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
91 removeArray(this.xlabels_
);
92 removeArray(this.ylabels_
);
97 axes
.prototype.clearChart
= function(e
) {
101 axes
.prototype.willDrawChart
= function(e
) {
103 if (!g
.getOption('drawXAxis') && !g
.getOption('drawYAxis')) return;
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; }
109 var context
= e
.drawingContext
;
110 var containerDiv
= e
.canvas
.parentNode
;
111 var canvasWidth
= e
.canvas
.width
;
112 var canvasHeight
= e
.canvas
.height
;
114 var label
, x
, y
, tick
, i
;
116 var makeLabelStyle
= function(axis
) {
118 position
: "absolute",
119 fontSize
: g
.getOptionForAxis('axisLabelFontSize', axis
) + "px",
121 color
: g
.getOptionForAxis('axisLabelColor', axis
),
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.
130 x
: makeLabelStyle('x'),
131 y
: makeLabelStyle('y'),
132 y2
: makeLabelStyle('y2')
135 var makeDiv
= function(txt
, axis
, prec_axis
) {
137 * This seems to be called with the following three sets of axis/prec_axis:
142 var div
= document
.createElement("div");
143 var labelStyle
= labelStyles
[prec_axis
== 'y2' ? 'y2' : axis
];
144 for (var name
in labelStyle
) {
145 if (labelStyle
.hasOwnProperty(name
)) {
146 div
.style
[name
] = labelStyle
[name
];
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
);
161 var layout
= g
.layout_
;
162 var area
= e
.dygraph
.plotter_
.area
;
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;
172 var prec_axis
= 'y1';
173 if (tick
[0] == 1) { // right-side y-axis
178 var fontSize
= g
.getOptionForAxis('axisLabelFontSize', prec_axis
);
179 y
= area
.y
+ tick
[1] * area
.h
;
181 /* Tick marks are currently clipped, so don't bother drawing them.
183 context.moveTo(halfUp(x), halfDown(y));
184 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
189 label
= makeDiv(tick
[2], 'y', num_axes
== 2 ? prec_axis
: null);
190 var top
= (y
- fontSize
/ 2);
191 if (top
< 0) top
= 0;
193 if (top
+ fontSize
+ 3 > canvasHeight
) {
194 label
.style
.bottom
= "0px";
196 label
.style
.top
= top
+ "px";
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";
206 label
.style
.width
= g
.getOption('yAxisLabelWidth') + "px";
207 containerDiv
.appendChild(label
);
208 this.ylabels_
.push(label
);
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];
215 // Interested in the y2 axis also?
216 var fontSize
= g
.getOptionForAxis('axisLabelFontSize', "y");
217 var bottom
= parseInt(bottomTick
.style
.top
, 10) + fontSize
;
218 if (bottom
> canvasHeight
- fontSize
) {
219 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
, 10) -
220 fontSize
/ 2) + "px";
224 // draw a vertical line on the left to separate the chart from the labels.
226 if (g
.getOption('drawAxesAtZero')) {
227 var r
= g
.toPercentXCoord(0);
228 if (r
> 1 || r
< 0 || isNaN(r
)) r
= 0;
229 axisX
= halfUp(area
.x
+ r
* area
.w
);
231 axisX
= halfUp(area
.x
);
234 context
.strokeStyle
= g
.getOptionForAxis('axisLineColor', 'y');
235 context
.lineWidth
= g
.getOptionForAxis('axisLineWidth', 'y');
238 context
.moveTo(axisX
, halfDown(area
.y
));
239 context
.lineTo(axisX
, halfDown(area
.y
+ area
.h
));
243 // if there's a secondary y-axis, draw a vertical line for that, too.
244 if (g
.numAxes() == 2) {
245 context
.strokeStyle
= g
.getOptionForAxis('axisLineColor', 'y2');
246 context
.lineWidth
= g
.getOptionForAxis('axisLineWidth', 'y2');
248 context
.moveTo(halfDown(area
.x
+ area
.w
), halfDown(area
.y
));
249 context
.lineTo(halfDown(area
.x
+ area
.w
), halfDown(area
.y
+ area
.h
));
255 if (g
.getOption('drawXAxis')) {
257 for (i
= 0; i
< layout
.xticks
.length
; i
++) {
258 tick
= layout
.xticks
[i
];
259 x
= area
.x
+ tick
[0] * area
.w
;
262 /* Tick marks are currently clipped, so don't bother drawing them.
264 context.moveTo(halfUp(x), halfDown(y));
265 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
270 label
= makeDiv(tick
[1], 'x');
271 label
.style
.textAlign
= "center";
272 label
.style
.top
= (y
+ g
.getOption('axisTickSize')) + 'px';
274 var left
= (x
- g
.getOption('axisLabelWidth')/2);
275 if (left
+ g
.getOption('axisLabelWidth') > canvasWidth
) {
276 left
= canvasWidth
- g
.getOption('xAxisLabelWidth');
277 label
.style
.textAlign
= "right";
281 label
.style
.textAlign
= "left";
284 label
.style
.left
= left
+ "px";
285 label
.style
.width
= g
.getOption('xAxisLabelWidth') + "px";
286 containerDiv
.appendChild(label
);
287 this.xlabels_
.push(label
);
291 context
.strokeStyle
= g
.getOptionForAxis('axisLineColor', 'x');
292 context
.lineWidth
= g
.getOptionForAxis('axisLineWidth', 'x');
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
);
300 axisY
= halfDown(area
.y
+ area
.h
);
302 context
.moveTo(halfUp(area
.x
), axisY
);
303 context
.lineTo(halfUp(area
.x
+ area
.w
), axisY
);