X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=plugins%2Faxes.js;h=aa142ce41c02deb34791d4ac3acb2114d0569d7a;hb=4a401b38137b4c4bd09dff59dafb4665874274f4;hp=40b826c99550e29c6b73270f2d8b93e2e6f7c2e9;hpb=d43d407b8605f0a2ddb4fda2af32deb6f28beed0;p=dygraphs.git diff --git a/plugins/axes.js b/plugins/axes.js index 40b826c..aa142ce 100644 --- a/plugins/axes.js +++ b/plugins/axes.js @@ -4,17 +4,21 @@ * MIT-licensed (http://opensource.org/licenses/MIT) */ +/*global Dygraph:false */ + Dygraph.Plugins.Axes = (function() { -"use strict"; +'use strict'; /* - Bits of jankiness: - Direct layout access - Direct area access - Should include calculation of ticks, not just the drawing. +Options left to make axis-friendly. + ('drawAxesAtZero') + ('xAxisHeight') */ /** @@ -28,7 +32,7 @@ var axes = function() { }; axes.prototype.toString = function() { - return "Axes Plugin"; + return 'Axes Plugin'; }; axes.prototype.activate = function(g) { @@ -42,28 +46,32 @@ axes.prototype.activate = function(g) { axes.prototype.layout = function(e) { var g = e.dygraph; - if (g.getOption('drawYAxis')) { - var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize'); - var y_axis_rect = e.reserveSpaceLeft(w); + if (g.getOptionForAxis('drawAxis', 'y')) { + var w = g.getOptionForAxis('axisLabelWidth', 'y') + 2 * g.getOptionForAxis('axisTickSize', 'y'); + e.reserveSpaceLeft(w); } - if (g.getOption('drawXAxis')) { + if (g.getOptionForAxis('drawAxis', 'x')) { var h; + // NOTE: I think this is probably broken now, since g.getOption() now + // hits the dictionary. (That is, g.getOption('xAxisHeight') now always + // has a value.) if (g.getOption('xAxisHeight')) { h = g.getOption('xAxisHeight'); } else { - h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOption('axisTickSize'); + h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOptionForAxis('axisTickSize', 'x'); } - var x_axis_rect = e.reserveSpaceBottom(h); + e.reserveSpaceBottom(h); } if (g.numAxes() == 2) { - // TODO(danvk): per-axis setting. - var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize'); - var y2_axis_rect = e.reserveSpaceRight(w); + if (g.getOptionForAxis('drawAxis', 'y2')) { + var w = g.getOptionForAxis('axisLabelWidth', 'y2') + 2 * g.getOptionForAxis('axisTickSize', 'y2'); + e.reserveSpaceRight(w); + } } else if (g.numAxes() > 2) { - g.error("Only two y-axes are supported at this time. (Trying " + - "to use " + g.numAxes() + ")"); + g.error('Only two y-axes are supported at this time. (Trying ' + + 'to use ' + g.numAxes() + ')'); } }; @@ -82,13 +90,17 @@ axes.prototype.detachLabels = function() { }; axes.prototype.clearChart = function(e) { - var g = e.dygraph; this.detachLabels(); }; axes.prototype.willDrawChart = function(e) { var g = e.dygraph; - if (!g.getOption('drawXAxis') && !g.getOption('drawYAxis')) return; + + if (!g.getOptionForAxis('drawAxis', 'x') && + !g.getOptionForAxis('drawAxis', 'y') && + !g.getOptionForAxis('drawAxis', 'y2')) { + return; + } // Round pixels to half-integer boundaries for crisper drawing. function halfUp(x) { return Math.round(x) + 0.5; } @@ -96,45 +108,45 @@ axes.prototype.willDrawChart = function(e) { var context = e.drawingContext; var containerDiv = e.canvas.parentNode; - var canvasWidth = e.canvas.width; - var canvasHeight = e.canvas.height; + var canvasWidth = g.width_; // e.canvas.width is affected by pixel ratio. + var canvasHeight = g.height_; var label, x, y, tick, i; var makeLabelStyle = function(axis) { return { - position: "absolute", - fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px", + position: 'absolute', + fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + 'px', zIndex: 10, - color: g.getOption('axisLabelColor'), - width: g.getOption('axisLabelWidth') + "px", + color: g.getOptionForAxis('axisLabelColor', axis), + width: g.getOptionForAxis('axisLabelWidth', axis) + 'px', // height: g.getOptionForAxis('axisLabelFontSize', 'x') + 2 + "px", - lineHeight: "normal", // Something other than "normal" line-height screws up label positioning. - overflow: "hidden" + lineHeight: 'normal', // Something other than "normal" line-height screws up label positioning. + overflow: 'hidden' }; - } + }; var labelStyles = { x : makeLabelStyle('x'), y : makeLabelStyle('y'), - y2 : makeLabelStyle('y2'), + y2 : makeLabelStyle('y2') }; var makeDiv = function(txt, axis, prec_axis) { /* - * This seems to be called with the following three sets of axis/perc_axis: + * This seems to be called with the following three sets of axis/prec_axis: * x: undefined * y: y1 * y: y2 */ - var div = document.createElement("div"); + var div = document.createElement('div'); var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis]; for (var name in labelStyle) { if (labelStyle.hasOwnProperty(name)) { div.style[name] = labelStyle[name]; } } - var inner_div = document.createElement("div"); + var inner_div = document.createElement('div'); inner_div.className = 'dygraph-axis-label' + ' dygraph-axis-label-' + axis + (prec_axis ? ' dygraph-axis-label-' + prec_axis : ''); @@ -145,27 +157,35 @@ axes.prototype.willDrawChart = function(e) { // axis lines context.save(); - context.strokeStyle = g.getOption('axisLineColor'); - context.lineWidth = g.getOption('axisLineWidth'); var layout = g.layout_; var area = e.dygraph.plotter_.area; - if (g.getOption('drawYAxis')) { + // Helper for repeated axis-option accesses. + var makeOptionGetter = function(axis) { + return function(option) { + return g.getOptionForAxis(option, axis); + }; + }; + + if (g.getOptionForAxis('drawAxis', 'y')) { if (layout.yticks && layout.yticks.length > 0) { var num_axes = g.numAxes(); + var getOptions = [makeOptionGetter('y'), makeOptionGetter('y2')]; for (i = 0; i < layout.yticks.length; i++) { tick = layout.yticks[i]; - if (typeof(tick) == "function") return; + if (typeof(tick) == 'function') return; // <-- when would this happen? x = area.x; var sgn = 1; var prec_axis = 'y1'; + var getAxisOption = getOptions[0]; if (tick[0] == 1) { // right-side y-axis x = area.x + area.w; sgn = -1; prec_axis = 'y2'; + getAxisOption = getOptions[1]; } - var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis); + var fontSize = getAxisOption('axisLabelFontSize'); y = area.y + tick[1] * area.h; /* Tick marks are currently clipped, so don't bother drawing them. @@ -181,19 +201,19 @@ axes.prototype.willDrawChart = function(e) { if (top < 0) top = 0; if (top + fontSize + 3 > canvasHeight) { - label.style.bottom = "0px"; + label.style.bottom = '0'; } else { - label.style.top = top + "px"; + label.style.top = top + 'px'; } if (tick[0] === 0) { - label.style.left = (area.x - g.getOption('yAxisLabelWidth') - g.getOption('axisTickSize')) + "px"; - label.style.textAlign = "right"; + label.style.left = (area.x - getAxisOption('axisLabelWidth') - getAxisOption('axisTickSize')) + 'px'; + label.style.textAlign = 'right'; } else if (tick[0] == 1) { label.style.left = (area.x + area.w + - g.getOption('axisTickSize')) + "px"; - label.style.textAlign = "left"; + getAxisOption('axisTickSize')) + 'px'; + label.style.textAlign = 'left'; } - label.style.width = g.getOption('yAxisLabelWidth') + "px"; + label.style.width = getAxisOption('axisLabelWidth') + 'px'; containerDiv.appendChild(label); this.ylabels_.push(label); } @@ -203,11 +223,11 @@ axes.prototype.willDrawChart = function(e) { // compensate if necessary. var bottomTick = this.ylabels_[0]; // Interested in the y2 axis also? - var fontSize = g.getOptionForAxis('axisLabelFontSize', "y"); + var fontSize = g.getOptionForAxis('axisLabelFontSize', 'y'); var bottom = parseInt(bottomTick.style.top, 10) + fontSize; if (bottom > canvasHeight - fontSize) { bottomTick.style.top = (parseInt(bottomTick.style.top, 10) - - fontSize / 2) + "px"; + fontSize / 2) + 'px'; } } @@ -215,11 +235,15 @@ axes.prototype.willDrawChart = function(e) { var axisX; if (g.getOption('drawAxesAtZero')) { var r = g.toPercentXCoord(0); - if (r > 1 || r < 0) r = 0; + if (r > 1 || r < 0 || isNaN(r)) r = 0; axisX = halfUp(area.x + r * area.w); } else { axisX = halfUp(area.x); } + + context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y'); + context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y'); + context.beginPath(); context.moveTo(axisX, halfDown(area.y)); context.lineTo(axisX, halfDown(area.y + area.h)); @@ -228,6 +252,8 @@ axes.prototype.willDrawChart = function(e) { // if there's a secondary y-axis, draw a vertical line for that, too. if (g.numAxes() == 2) { + context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2'); + context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2'); context.beginPath(); context.moveTo(halfDown(area.x + area.w), halfDown(area.y)); context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h)); @@ -236,8 +262,9 @@ axes.prototype.willDrawChart = function(e) { } } - if (g.getOption('drawXAxis')) { + if (g.getOptionForAxis('drawAxis', 'x')) { if (layout.xticks) { + var getAxisOption = makeOptionGetter('x'); for (i = 0; i < layout.xticks.length; i++) { tick = layout.xticks[i]; x = area.x + tick[0] * area.w; @@ -252,26 +279,28 @@ axes.prototype.willDrawChart = function(e) { */ label = makeDiv(tick[1], 'x'); - label.style.textAlign = "center"; - label.style.top = (y + g.getOption('axisTickSize')) + 'px'; + label.style.textAlign = 'center'; + label.style.top = (y + getAxisOption('axisTickSize')) + 'px'; - var left = (x - g.getOption('axisLabelWidth')/2); - if (left + g.getOption('axisLabelWidth') > canvasWidth) { - left = canvasWidth - g.getOption('xAxisLabelWidth'); - label.style.textAlign = "right"; + var left = (x - getAxisOption('axisLabelWidth')/2); + if (left + getAxisOption('axisLabelWidth') > canvasWidth) { + left = canvasWidth - getAxisOption('axisLabelWidth'); + label.style.textAlign = 'right'; } if (left < 0) { left = 0; - label.style.textAlign = "left"; + label.style.textAlign = 'left'; } - label.style.left = left + "px"; - label.style.width = g.getOption('xAxisLabelWidth') + "px"; + label.style.left = left + 'px'; + label.style.width = getAxisOption('axisLabelWidth') + 'px'; containerDiv.appendChild(label); this.xlabels_.push(label); } } + context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x'); + context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x'); context.beginPath(); var axisY; if (g.getOption('drawAxesAtZero')) {