| 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 | ('drawAxesAtZero') |
| 21 | ('xAxisHeight') |
| 22 | */ |
| 23 | |
| 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 | */ |
| 29 | var axes = function() { |
| 30 | this.xlabels_ = []; |
| 31 | this.ylabels_ = []; |
| 32 | }; |
| 33 | |
| 34 | axes.prototype.toString = function() { |
| 35 | return 'Axes Plugin'; |
| 36 | }; |
| 37 | |
| 38 | axes.prototype.activate = function(g) { |
| 39 | return { |
| 40 | layout: this.layout, |
| 41 | clearChart: this.clearChart, |
| 42 | willDrawChart: this.willDrawChart |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | axes.prototype.layout = function(e) { |
| 47 | var g = e.dygraph; |
| 48 | |
| 49 | if (g.getOptionForAxis('drawAxis', 'y')) { |
| 50 | var w = g.getOptionForAxis('axisLabelWidth', 'y') + 2 * g.getOptionForAxis('axisTickSize', 'y'); |
| 51 | e.reserveSpaceLeft(w); |
| 52 | } |
| 53 | |
| 54 | if (g.getOptionForAxis('drawAxis', 'x')) { |
| 55 | var h; |
| 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.) |
| 59 | if (g.getOption('xAxisHeight')) { |
| 60 | h = g.getOption('xAxisHeight'); |
| 61 | } else { |
| 62 | h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOptionForAxis('axisTickSize', 'x'); |
| 63 | } |
| 64 | e.reserveSpaceBottom(h); |
| 65 | } |
| 66 | |
| 67 | if (g.numAxes() == 2) { |
| 68 | if (g.getOptionForAxis('drawAxis', 'y2')) { |
| 69 | var w = g.getOptionForAxis('axisLabelWidth', 'y2') + 2 * g.getOptionForAxis('axisTickSize', 'y2'); |
| 70 | e.reserveSpaceRight(w); |
| 71 | } |
| 72 | } else if (g.numAxes() > 2) { |
| 73 | g.error('Only two y-axes are supported at this time. (Trying ' + |
| 74 | 'to use ' + g.numAxes() + ')'); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | axes.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 | |
| 92 | axes.prototype.clearChart = function(e) { |
| 93 | this.detachLabels(); |
| 94 | }; |
| 95 | |
| 96 | axes.prototype.willDrawChart = function(e) { |
| 97 | var g = e.dygraph; |
| 98 | |
| 99 | if (!g.getOptionForAxis('drawAxis', 'x') && |
| 100 | !g.getOptionForAxis('drawAxis', 'y') && |
| 101 | !g.getOptionForAxis('drawAxis', 'y2')) { |
| 102 | return; |
| 103 | } |
| 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 = g.width_; // e.canvas.width is affected by pixel ratio. |
| 112 | var canvasHeight = g.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.getOptionForAxis('axisLabelWidth', axis) + '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 | // Helper for repeated axis-option accesses. |
| 165 | var makeOptionGetter = function(axis) { |
| 166 | return function(option) { |
| 167 | return g.getOptionForAxis(option, axis); |
| 168 | }; |
| 169 | }; |
| 170 | |
| 171 | if (g.getOptionForAxis('drawAxis', 'y')) { |
| 172 | if (layout.yticks && layout.yticks.length > 0) { |
| 173 | var num_axes = g.numAxes(); |
| 174 | var getOptions = [makeOptionGetter('y'), makeOptionGetter('y2')]; |
| 175 | for (i = 0; i < layout.yticks.length; i++) { |
| 176 | tick = layout.yticks[i]; |
| 177 | if (typeof(tick) == 'function') return; // <-- when would this happen? |
| 178 | x = area.x; |
| 179 | var sgn = 1; |
| 180 | var prec_axis = 'y1'; |
| 181 | var getAxisOption = getOptions[0]; |
| 182 | if (tick[0] == 1) { // right-side y-axis |
| 183 | x = area.x + area.w; |
| 184 | sgn = -1; |
| 185 | prec_axis = 'y2'; |
| 186 | getAxisOption = getOptions[1]; |
| 187 | } |
| 188 | var fontSize = getAxisOption('axisLabelFontSize'); |
| 189 | y = area.y + tick[1] * area.h; |
| 190 | |
| 191 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 192 | context.beginPath(); |
| 193 | context.moveTo(halfUp(x), halfDown(y)); |
| 194 | context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y)); |
| 195 | context.closePath(); |
| 196 | context.stroke(); |
| 197 | */ |
| 198 | |
| 199 | label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null); |
| 200 | var top = (y - fontSize / 2); |
| 201 | if (top < 0) top = 0; |
| 202 | |
| 203 | if (top + fontSize + 3 > canvasHeight) { |
| 204 | label.style.bottom = '0'; |
| 205 | } else { |
| 206 | label.style.top = top + 'px'; |
| 207 | } |
| 208 | if (tick[0] === 0) { |
| 209 | label.style.left = (area.x - getAxisOption('axisLabelWidth') - getAxisOption('axisTickSize')) + 'px'; |
| 210 | label.style.textAlign = 'right'; |
| 211 | } else if (tick[0] == 1) { |
| 212 | label.style.left = (area.x + area.w + |
| 213 | getAxisOption('axisTickSize')) + 'px'; |
| 214 | label.style.textAlign = 'left'; |
| 215 | } |
| 216 | label.style.width = getAxisOption('axisLabelWidth') + 'px'; |
| 217 | containerDiv.appendChild(label); |
| 218 | this.ylabels_.push(label); |
| 219 | } |
| 220 | |
| 221 | // The lowest tick on the y-axis often overlaps with the leftmost |
| 222 | // tick on the x-axis. Shift the bottom tick up a little bit to |
| 223 | // compensate if necessary. |
| 224 | var bottomTick = this.ylabels_[0]; |
| 225 | // Interested in the y2 axis also? |
| 226 | var fontSize = g.getOptionForAxis('axisLabelFontSize', 'y'); |
| 227 | var bottom = parseInt(bottomTick.style.top, 10) + fontSize; |
| 228 | if (bottom > canvasHeight - fontSize) { |
| 229 | bottomTick.style.top = (parseInt(bottomTick.style.top, 10) - |
| 230 | fontSize / 2) + 'px'; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // draw a vertical line on the left to separate the chart from the labels. |
| 235 | var axisX; |
| 236 | if (g.getOption('drawAxesAtZero')) { |
| 237 | var r = g.toPercentXCoord(0); |
| 238 | if (r > 1 || r < 0 || isNaN(r)) r = 0; |
| 239 | axisX = halfUp(area.x + r * area.w); |
| 240 | } else { |
| 241 | axisX = halfUp(area.x); |
| 242 | } |
| 243 | |
| 244 | context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y'); |
| 245 | context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y'); |
| 246 | |
| 247 | context.beginPath(); |
| 248 | context.moveTo(axisX, halfDown(area.y)); |
| 249 | context.lineTo(axisX, halfDown(area.y + area.h)); |
| 250 | context.closePath(); |
| 251 | context.stroke(); |
| 252 | |
| 253 | // if there's a secondary y-axis, draw a vertical line for that, too. |
| 254 | if (g.numAxes() == 2) { |
| 255 | context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2'); |
| 256 | context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2'); |
| 257 | context.beginPath(); |
| 258 | context.moveTo(halfDown(area.x + area.w), halfDown(area.y)); |
| 259 | context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h)); |
| 260 | context.closePath(); |
| 261 | context.stroke(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (g.getOptionForAxis('drawAxis', 'x')) { |
| 266 | if (layout.xticks) { |
| 267 | var getAxisOption = makeOptionGetter('x'); |
| 268 | for (i = 0; i < layout.xticks.length; i++) { |
| 269 | tick = layout.xticks[i]; |
| 270 | x = area.x + tick[0] * area.w; |
| 271 | y = area.y + area.h; |
| 272 | |
| 273 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 274 | context.beginPath(); |
| 275 | context.moveTo(halfUp(x), halfDown(y)); |
| 276 | context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize'))); |
| 277 | context.closePath(); |
| 278 | context.stroke(); |
| 279 | */ |
| 280 | |
| 281 | label = makeDiv(tick[1], 'x'); |
| 282 | label.style.textAlign = 'center'; |
| 283 | label.style.top = (y + getAxisOption('axisTickSize')) + 'px'; |
| 284 | |
| 285 | var left = (x - getAxisOption('axisLabelWidth')/2); |
| 286 | if (left + getAxisOption('axisLabelWidth') > canvasWidth) { |
| 287 | left = canvasWidth - getAxisOption('axisLabelWidth'); |
| 288 | label.style.textAlign = 'right'; |
| 289 | } |
| 290 | if (left < 0) { |
| 291 | left = 0; |
| 292 | label.style.textAlign = 'left'; |
| 293 | } |
| 294 | |
| 295 | label.style.left = left + 'px'; |
| 296 | label.style.width = getAxisOption('axisLabelWidth') + 'px'; |
| 297 | containerDiv.appendChild(label); |
| 298 | this.xlabels_.push(label); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x'); |
| 303 | context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x'); |
| 304 | context.beginPath(); |
| 305 | var axisY; |
| 306 | if (g.getOption('drawAxesAtZero')) { |
| 307 | var r = g.toPercentYCoord(0, 0); |
| 308 | if (r > 1 || r < 0) r = 1; |
| 309 | axisY = halfDown(area.y + r * area.h); |
| 310 | } else { |
| 311 | axisY = halfDown(area.y + area.h); |
| 312 | } |
| 313 | context.moveTo(halfUp(area.x), axisY); |
| 314 | context.lineTo(halfUp(area.x + area.w), axisY); |
| 315 | context.closePath(); |
| 316 | context.stroke(); |
| 317 | } |
| 318 | |
| 319 | context.restore(); |
| 320 | }; |
| 321 | |
| 322 | return axes; |
| 323 | })(); |