3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 Dygraph
.Plugins
.Axes
= (function() {
14 - Direct layout access
16 - Should include calculation of ticks, not just the drawing.
21 * Draws the axes. This includes the labels on the x- and y-axes, as well
22 * as the tick marks on the axes.
23 * It does _not_ draw the grid lines which span the entire chart.
25 var axes
= function() {
30 axes
.prototype.toString
= function() {
34 axes
.prototype.activate
= function(g
) {
37 clearChart
: this.clearChart
,
38 willDrawChart
: this.willDrawChart
42 axes
.prototype.layout
= function(e
) {
45 if (g
.getOption('drawYAxis')) {
46 var w
= g
.getOption('yAxisLabelWidth') + 2 * g
.getOption('axisTickSize');
47 var y_axis_rect
= e
.reserveSpaceLeft(w
);
50 if (g
.getOption('drawXAxis')) {
52 if (g
.getOption('xAxisHeight')) {
53 h
= g
.getOption('xAxisHeight');
55 h
= g
.getOption('axisLabelFontSize') + 2 * g
.getOption('axisTickSize');
57 var x_axis_rect
= e
.reserveSpaceBottom(h
);
60 if (g
.numAxes() == 2) {
61 // TODO(danvk): per-axis setting.
62 var w
= g
.getOption('yAxisLabelWidth') + 2 * g
.getOption('axisTickSize');
63 var y2_axis_rect
= e
.reserveSpaceRight(w
);
64 } else if (g
.numAxes() > 2) {
65 g
.error("Only two y-axes are supported at this time. (Trying " +
66 "to use " + g
.numAxes() + ")");
70 axes
.prototype.detachLabels
= function() {
71 function removeArray(ary
) {
72 for (var i
= 0; i
< ary
.length
; i
++) {
74 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
78 removeArray(this.xlabels_
);
79 removeArray(this.ylabels_
);
84 axes
.prototype.clearChart
= function(e
) {
89 axes
.prototype.willDrawChart
= function(e
) {
91 if (!g
.getOption('drawXAxis') && !g
.getOption('drawYAxis')) return;
93 // Round pixels to half-integer boundaries for crisper drawing.
94 function halfUp(x
) { return Math
.round(x
) + 0.5; }
95 function halfDown(y
){ return Math
.round(y
) - 0.5; }
97 var context
= e
.drawingContext
;
98 var containerDiv
= e
.canvas
.parentNode
;
99 var canvasWidth
= e
.canvas
.width
;
100 var canvasHeight
= e
.canvas
.height
;
102 var label
, x
, y
, tick
, i
;
105 position
: "absolute",
106 fontSize
: g
.getOption('axisLabelFontSize') + "px",
108 color
: g
.getOption('axisLabelColor'),
109 width
: g
.getOption('axisLabelWidth') + "px",
110 // height: this.attr_('axisLabelFontSize') + 2 + "px",
111 lineHeight
: "normal", // Something other than "normal" line-height screws up label positioning.
114 var makeDiv
= function(txt
, axis
, prec_axis
) {
115 var div
= document
.createElement("div");
116 for (var name
in labelStyle
) {
117 if (labelStyle
.hasOwnProperty(name
)) {
118 div
.style
[name
] = labelStyle
[name
];
121 var inner_div
= document
.createElement("div");
122 inner_div
.className
= 'dygraph-axis-label' +
123 ' dygraph-axis-label-' + axis
+
124 (prec_axis
? ' dygraph-axis-label-' + prec_axis
: '');
125 inner_div
.innerHTML
= txt
;
126 div
.appendChild(inner_div
);
132 context
.strokeStyle
= g
.getOption('axisLineColor');
133 context
.lineWidth
= g
.getOption('axisLineWidth');
135 var layout
= g
.layout_
;
136 var area
= e
.dygraph
.plotter_
.area
;
138 if (g
.getOption('drawYAxis')) {
139 if (layout
.yticks
&& layout
.yticks
.length
> 0) {
140 var num_axes
= g
.numAxes();
141 for (i
= 0; i
< layout
.yticks
.length
; i
++) {
142 tick
= layout
.yticks
[i
];
143 if (typeof(tick
) == "function") return;
146 var prec_axis
= 'y1';
147 if (tick
[0] == 1) { // right-side y-axis
152 y
= area
.y
+ tick
[1] * area
.h
;
154 /* Tick marks are currently clipped, so don't bother drawing them.
156 context.moveTo(halfUp(x), halfDown(y));
157 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
162 label
= makeDiv(tick
[2], 'y', num_axes
== 2 ? prec_axis
: null);
163 var top
= (y
- g
.getOption('axisLabelFontSize') / 2);
164 if (top
< 0) top
= 0;
166 if (top
+ g
.getOption('axisLabelFontSize') + 3 > canvasHeight
) {
167 label
.style
.bottom
= "0px";
169 label
.style
.top
= top
+ "px";
172 label
.style
.left
= (area
.x
- g
.getOption('yAxisLabelWidth') - g
.getOption('axisTickSize')) + "px";
173 label
.style
.textAlign
= "right";
174 } else if (tick
[0] == 1) {
175 label
.style
.left
= (area
.x
+ area
.w
+
176 g
.getOption('axisTickSize')) + "px";
177 label
.style
.textAlign
= "left";
179 label
.style
.width
= g
.getOption('yAxisLabelWidth') + "px";
180 containerDiv
.appendChild(label
);
181 this.ylabels_
.push(label
);
184 // The lowest tick on the y-axis often overlaps with the leftmost
185 // tick on the x-axis. Shift the bottom tick up a little bit to
186 // compensate if necessary.
187 var bottomTick
= this.ylabels_
[0];
188 var fontSize
= g
.getOption('axisLabelFontSize');
189 var bottom
= parseInt(bottomTick
.style
.top
, 10) + fontSize
;
190 if (bottom
> canvasHeight
- fontSize
) {
191 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
, 10) -
192 fontSize
/ 2) + "px";
196 // draw a vertical line on the left to separate the chart from the labels.
198 if (g
.getOption('drawAxesAtZero')) {
199 var r
= g
.toPercentXCoord(0);
200 if (r
> 1 || r
< 0) r
= 0;
201 axisX
= halfUp(area
.x
+ r
* area
.w
);
203 axisX
= halfUp(area
.x
);
206 context
.moveTo(axisX
, halfDown(area
.y
));
207 context
.lineTo(axisX
, halfDown(area
.y
+ area
.h
));
211 // if there's a secondary y-axis, draw a vertical line for that, too.
212 if (g
.numAxes() == 2) {
214 context
.moveTo(halfDown(area
.x
+ area
.w
), halfDown(area
.y
));
215 context
.lineTo(halfDown(area
.x
+ area
.w
), halfDown(area
.y
+ area
.h
));
221 if (g
.getOption('drawXAxis')) {
223 for (i
= 0; i
< layout
.xticks
.length
; i
++) {
224 tick
= layout
.xticks
[i
];
225 x
= area
.x
+ tick
[0] * area
.w
;
228 /* Tick marks are currently clipped, so don't bother drawing them.
230 context.moveTo(halfUp(x), halfDown(y));
231 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
236 label
= makeDiv(tick
[1], 'x');
237 label
.style
.textAlign
= "center";
238 label
.style
.top
= (y
+ g
.getOption('axisTickSize')) + 'px';
240 var left
= (x
- g
.getOption('axisLabelWidth')/2);
241 if (left
+ g
.getOption('axisLabelWidth') > canvasWidth
) {
242 left
= canvasWidth
- g
.getOption('xAxisLabelWidth');
243 label
.style
.textAlign
= "right";
247 label
.style
.textAlign
= "left";
250 label
.style
.left
= left
+ "px";
251 label
.style
.width
= g
.getOption('xAxisLabelWidth') + "px";
252 containerDiv
.appendChild(label
);
253 this.xlabels_
.push(label
);
259 if (g
.getOption('drawAxesAtZero')) {
260 var r
= g
.toPercentYCoord(0, 0);
261 if (r
> 1 || r
< 0) r
= 1;
262 axisY
= halfDown(area
.y
+ r
* area
.h
);
264 axisY
= halfDown(area
.y
+ area
.h
);
266 context
.moveTo(halfUp(area
.x
), axisY
);
267 context
.lineTo(halfUp(area
.x
+ area
.w
), axisY
);