| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2012 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /*global Dygraph:false */ |
| 8 | |
| 9 | Dygraph.Plugins.Axes = (function() { |
| 10 | |
| 11 | "use strict"; |
| 12 | |
| 13 | /* |
| 14 | Bits of jankiness: |
| 15 | - Direct layout access |
| 16 | - Direct area access |
| 17 | - Should include calculation of ticks, not just the drawing. |
| 18 | |
| 19 | Options left to make axis-friendly. |
| 20 | ('axisTickSize') |
| 21 | ('drawAxesAtZero') |
| 22 | ('xAxisHeight') |
| 23 | |
| 24 | These too. What is the difference between axisLablelWidth and {x,y}AxisLabelWidth? |
| 25 | ('axisLabelWidth') |
| 26 | ('xAxisLabelWidth') |
| 27 | ('yAxisLabelWidth') |
| 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 | */ |
| 35 | var axes = function() { |
| 36 | this.xlabels_ = []; |
| 37 | this.ylabels_ = []; |
| 38 | }; |
| 39 | |
| 40 | axes.prototype.toString = function() { |
| 41 | return "Axes Plugin"; |
| 42 | }; |
| 43 | |
| 44 | axes.prototype.activate = function(g) { |
| 45 | return { |
| 46 | layout: this.layout, |
| 47 | clearChart: this.clearChart, |
| 48 | willDrawChart: this.willDrawChart |
| 49 | }; |
| 50 | }; |
| 51 | |
| 52 | axes.prototype.layout = function(e) { |
| 53 | var g = e.dygraph; |
| 54 | |
| 55 | if (g.getOption('drawYAxis')) { |
| 56 | var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize'); |
| 57 | e.reserveSpaceLeft(w); |
| 58 | } |
| 59 | |
| 60 | if (g.getOption('drawXAxis')) { |
| 61 | var h; |
| 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.) |
| 65 | if (g.getOption('xAxisHeight')) { |
| 66 | h = g.getOption('xAxisHeight'); |
| 67 | } else { |
| 68 | h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOption('axisTickSize'); |
| 69 | } |
| 70 | e.reserveSpaceBottom(h); |
| 71 | } |
| 72 | |
| 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() + ")"); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | axes.prototype.detachLabels = function() { |
| 84 | function removeArray(ary) { |
| 85 | for (var i = 0; i < ary.length; i++) { |
| 86 | var el = ary[i]; |
| 87 | if (el.parentNode) el.parentNode.removeChild(el); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | removeArray(this.xlabels_); |
| 92 | removeArray(this.ylabels_); |
| 93 | this.xlabels_ = []; |
| 94 | this.ylabels_ = []; |
| 95 | }; |
| 96 | |
| 97 | axes.prototype.clearChart = function(e) { |
| 98 | this.detachLabels(); |
| 99 | }; |
| 100 | |
| 101 | axes.prototype.willDrawChart = function(e) { |
| 102 | var g = e.dygraph; |
| 103 | if (!g.getOption('drawXAxis') && !g.getOption('drawYAxis')) return; |
| 104 | |
| 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; |
| 111 | var canvasWidth = e.canvas.width; |
| 112 | var canvasHeight = e.canvas.height; |
| 113 | |
| 114 | var label, x, y, tick, i; |
| 115 | |
| 116 | var makeLabelStyle = function(axis) { |
| 117 | return { |
| 118 | position: "absolute", |
| 119 | fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px", |
| 120 | zIndex: 10, |
| 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. |
| 125 | overflow: "hidden" |
| 126 | }; |
| 127 | }; |
| 128 | |
| 129 | var labelStyles = { |
| 130 | x : makeLabelStyle('x'), |
| 131 | y : makeLabelStyle('y'), |
| 132 | y2 : makeLabelStyle('y2') |
| 133 | }; |
| 134 | |
| 135 | var makeDiv = function(txt, axis, prec_axis) { |
| 136 | /* |
| 137 | * This seems to be called with the following three sets of axis/prec_axis: |
| 138 | * x: undefined |
| 139 | * y: y1 |
| 140 | * y: y2 |
| 141 | */ |
| 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]; |
| 147 | } |
| 148 | } |
| 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); |
| 155 | return div; |
| 156 | }; |
| 157 | |
| 158 | // axis lines |
| 159 | context.save(); |
| 160 | |
| 161 | var layout = g.layout_; |
| 162 | var area = e.dygraph.plotter_.area; |
| 163 | |
| 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; |
| 170 | x = area.x; |
| 171 | var sgn = 1; |
| 172 | var prec_axis = 'y1'; |
| 173 | if (tick[0] == 1) { // right-side y-axis |
| 174 | x = area.x + area.w; |
| 175 | sgn = -1; |
| 176 | prec_axis = 'y2'; |
| 177 | } |
| 178 | var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis); |
| 179 | y = area.y + tick[1] * area.h; |
| 180 | |
| 181 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 182 | context.beginPath(); |
| 183 | context.moveTo(halfUp(x), halfDown(y)); |
| 184 | context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y)); |
| 185 | context.closePath(); |
| 186 | context.stroke(); |
| 187 | */ |
| 188 | |
| 189 | label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null); |
| 190 | var top = (y - fontSize / 2); |
| 191 | if (top < 0) top = 0; |
| 192 | |
| 193 | if (top + fontSize + 3 > canvasHeight) { |
| 194 | label.style.bottom = "0px"; |
| 195 | } else { |
| 196 | label.style.top = top + "px"; |
| 197 | } |
| 198 | if (tick[0] === 0) { |
| 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"; |
| 205 | } |
| 206 | label.style.width = g.getOption('yAxisLabelWidth') + "px"; |
| 207 | containerDiv.appendChild(label); |
| 208 | this.ylabels_.push(label); |
| 209 | } |
| 210 | |
| 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"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // draw a vertical line on the left to separate the chart from the labels. |
| 225 | var axisX; |
| 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); |
| 230 | } else { |
| 231 | axisX = halfUp(area.x); |
| 232 | } |
| 233 | |
| 234 | context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y'); |
| 235 | context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y'); |
| 236 | |
| 237 | context.beginPath(); |
| 238 | context.moveTo(axisX, halfDown(area.y)); |
| 239 | context.lineTo(axisX, halfDown(area.y + area.h)); |
| 240 | context.closePath(); |
| 241 | context.stroke(); |
| 242 | |
| 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'); |
| 247 | context.beginPath(); |
| 248 | context.moveTo(halfDown(area.x + area.w), halfDown(area.y)); |
| 249 | context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h)); |
| 250 | context.closePath(); |
| 251 | context.stroke(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (g.getOption('drawXAxis')) { |
| 256 | if (layout.xticks) { |
| 257 | for (i = 0; i < layout.xticks.length; i++) { |
| 258 | tick = layout.xticks[i]; |
| 259 | x = area.x + tick[0] * area.w; |
| 260 | y = area.y + area.h; |
| 261 | |
| 262 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 263 | context.beginPath(); |
| 264 | context.moveTo(halfUp(x), halfDown(y)); |
| 265 | context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize'))); |
| 266 | context.closePath(); |
| 267 | context.stroke(); |
| 268 | */ |
| 269 | |
| 270 | label = makeDiv(tick[1], 'x'); |
| 271 | label.style.textAlign = "center"; |
| 272 | label.style.top = (y + g.getOption('axisTickSize')) + 'px'; |
| 273 | |
| 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"; |
| 278 | } |
| 279 | if (left < 0) { |
| 280 | left = 0; |
| 281 | label.style.textAlign = "left"; |
| 282 | } |
| 283 | |
| 284 | label.style.left = left + "px"; |
| 285 | label.style.width = g.getOption('xAxisLabelWidth') + "px"; |
| 286 | containerDiv.appendChild(label); |
| 287 | this.xlabels_.push(label); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x'); |
| 292 | context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x'); |
| 293 | context.beginPath(); |
| 294 | var axisY; |
| 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); |
| 299 | } else { |
| 300 | axisY = halfDown(area.y + area.h); |
| 301 | } |
| 302 | context.moveTo(halfUp(area.x), axisY); |
| 303 | context.lineTo(halfUp(area.x + area.w), axisY); |
| 304 | context.closePath(); |
| 305 | context.stroke(); |
| 306 | } |
| 307 | |
| 308 | context.restore(); |
| 309 | }; |
| 310 | |
| 311 | return axes; |
| 312 | })(); |