X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph.js;h=0b01dd896c61fde477728ad7a70550a0a63c223c;hb=ccecde931c59699a62c0e83651dc28ac680f83ab;hp=43d30115e0de4efcff8f5044a27e0f06a1e60fb4;hpb=946e63b5df4c2fce7af33f24989d4347e0394a1a;p=dygraphs.git diff --git a/dygraph.js b/dygraph.js index 43d3011..0b01dd8 100644 --- a/dygraph.js +++ b/dygraph.js @@ -246,6 +246,8 @@ Dygraph.DEFAULT_ATTRS = { stepPlot: false, avoidMinZero: false, + xRangePad: 0, + yRangePad: null, drawAxesAtZero: false, // Sizes of the various chart labels. @@ -657,8 +659,18 @@ Dygraph.prototype.xAxisRange = function() { * data set. */ Dygraph.prototype.xAxisExtremes = function() { + var pad = this.attr_('xRangePad') / this.plotter_.area.w; + if (this.numRows() == 0) { + return [0 - pad, 1 + pad]; + } var left = this.rawData_[0][0]; var right = this.rawData_[this.rawData_.length - 1][0]; + if (pad) { + // Must keep this in sync with dygraph-layout _evaluateLimits() + var range = right - left; + left -= range * pad; + right += range * pad; + } return [left, right]; }; @@ -874,6 +886,7 @@ Dygraph.prototype.toPercentXCoord = function(x) { * @return { Integer } The number of columns. */ Dygraph.prototype.numColumns = function() { + if (!this.rawData_) return 0; return this.rawData_[0] ? this.rawData_[0].length : this.attr_("labels").length; }; @@ -882,25 +895,11 @@ Dygraph.prototype.numColumns = function() { * @return { Integer } The number of rows, less any header. */ Dygraph.prototype.numRows = function() { + if (!this.rawData_) return 0; return this.rawData_.length; }; /** - * Returns the full range of the x-axis, as determined by the most extreme - * values in the data set. Not affected by zooming, visibility, etc. - * TODO(danvk): merge w/ xAxisExtremes - * @return { Array } A [low, high] pair - * @private - */ -Dygraph.prototype.fullXRange_ = function() { - if (this.numRows() > 0) { - return [this.rawData_[0][0], this.rawData_[this.numRows() - 1][0]]; - } else { - return [0, 1]; - } -}; - -/** * Returns the value in the given row and column. If the row and column exceed * the bounds on the data, returns null. Also returns null if the value is * missing. @@ -956,19 +955,20 @@ Dygraph.prototype.createInterface_ = function() { var dygraph = this; - // Don't recreate and register the handlers on subsequent calls. - // This happens when the graph is resized. - if (!this.mouseMoveHandler_) { - this.mouseMoveHandler_ = function(e) { - dygraph.mouseMove_(e); - }; - this.addEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); + this.mouseMoveHandler_ = function(e) { + dygraph.mouseMove_(e); + }; - this.mouseOutHandler_ = function(e) { - dygraph.mouseOut_(e); - }; - this.addEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler_); + this.mouseOutHandler_ = function(e) { + dygraph.mouseOut_(e); + }; + this.addEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); + this.addEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler_); + + // Don't recreate and register the resize handler on subsequent calls. + // This happens when the graph is resized. + if (!this.resizeHandler_) { this.resizeHandler_ = function(e) { dygraph.resize(); }; @@ -992,10 +992,13 @@ Dygraph.prototype.destroy = function() { } }; - for (var idx = 0; idx < this.registeredEvents_.length; idx++) { - var reg = this.registeredEvents_[idx]; - Dygraph.removeEvent(reg.elem, reg.type, reg.fn); + if (this.registeredEvents_) { + for (var idx = 0; idx < this.registeredEvents_.length; idx++) { + var reg = this.registeredEvents_[idx]; + Dygraph.removeEvent(reg.elem, reg.type, reg.fn); + } } + this.registeredEvents_ = []; // remove mouse event handlers (This may not be necessary anymore) @@ -1277,6 +1280,12 @@ Dygraph.prototype.createDragInterface_ = function() { bindHandler(interactionModel[eventName])); } + // unregister the handler on subsequent calls. + // This happens when the graph is resized. + if (this.mouseUpHandler_) { + Dygraph.removeEvent(document, 'mouseup', this.mouseUpHandler_); + } + // If the user releases the mouse button during a drag, but not over the // canvas, then it doesn't count as a zooming action. this.mouseUpHandler_ = function(event) { @@ -2072,7 +2081,7 @@ Dygraph.prototype.addXTicks_ = function() { if (this.dateWindow_) { range = [this.dateWindow_[0], this.dateWindow_[1]]; } else { - range = this.fullXRange_(); + range = this.xAxisExtremes(); } var xAxisOptionsView = this.optionsViewForAxis_('x'); @@ -2493,6 +2502,10 @@ Dygraph.prototype.axisPropertiesForSeries = function(series) { * This fills in the valueRange and ticks fields in each entry of this.axes_. */ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { + + var isNullUndefinedOrNaN = function(num) { + return isNaN(parseFloat(num)); + }; var series; var numAxes = this.attributes_.numAxes(); @@ -2526,35 +2539,71 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { maxY = Math.max(extremeMaxY, maxY); } } - if (includeZero && minY > 0) minY = 0; + + // Include zero if requested by the user. + if (includeZero && !logscale) { + if (minY > 0) minY = 0; + if (maxY < 0) maxY = 0; + } // Ensure we have a valid scale, otherwise default to [0, 1] for safety. if (minY == Infinity) minY = 0; if (maxY == -Infinity) maxY = 1; - // Add some padding and round up to an integer to be human-friendly. var span = maxY - minY; - // special case: if we have no sense of scale, use +/-10% of the sole value. - if (span === 0) { span = maxY; } + // special case: if we have no sense of scale, center on the sole value. + if (span === 0) { + if (maxY !== 0) { + span = Math.abs(maxY); + } else { + // ... and if the sole value is zero, use range 0-1. + maxY = 1; + span = 1; + } + } + + // Add some padding. This supports two Y padding operation modes: + // + // - backwards compatible (yRangePad not set): + // 10% padding for automatic Y ranges, but not for user-supplied + // ranges, and move a close-to-zero edge to zero except if + // avoidMinZero is set, since drawing at the edge results in + // invisible lines. Unfortunately lines drawn at the edge of a + // user-supplied range will still be invisible. If logscale is + // set, add a variable amount of padding at the top but + // none at the bottom. + // + // - new-style (yRangePad set by the user): + // always add the specified Y padding. + // + var ypadCompat = true; + var ypad = 0.1; // add 10% + if (this.attr_('yRangePad') !== null) { + ypadCompat = false; + // Convert pixel padding to ratio + ypad = this.attr_('yRangePad') / this.plotter_.area.h; + } var maxAxisY, minAxisY; if (logscale) { - maxAxisY = maxY + 0.1 * span; - minAxisY = minY; + if (ypadCompat) { + maxAxisY = maxY + ypad * span; + minAxisY = minY; + } else { + var logpad = Math.exp(Math.log(span) * ypad); + maxAxisY = maxY * logpad; + minAxisY = minY / logpad; + } } else { - maxAxisY = maxY + 0.1 * span; - minAxisY = minY - 0.1 * span; + maxAxisY = maxY + ypad * span; + minAxisY = minY - ypad * span; - // Try to include zero and make it minAxisY (or maxAxisY) if it makes sense. - if (!this.attr_("avoidMinZero")) { + // Backwards-compatible behavior: Move the span to start or end at zero if it's + // close to zero, but not if avoidMinZero is set. + if (ypadCompat && !this.attr_("avoidMinZero")) { if (minAxisY < 0 && minY >= 0) minAxisY = 0; if (maxAxisY > 0 && maxY <= 0) maxAxisY = 0; } - - if (this.attr_("includeZero")) { - if (maxY < 0) maxAxisY = 0; - if (minY > 0) minAxisY = 0; - } } axis.extremeRange = [minAxisY, maxAxisY]; } @@ -2565,7 +2614,20 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { axis.computedValueRange = [axis.valueWindow[0], axis.valueWindow[1]]; } else if (axis.valueRange) { // This is a user-set value range for this axis. - axis.computedValueRange = [axis.valueRange[0], axis.valueRange[1]]; + var y0 = isNullUndefinedOrNaN(axis.valueRange[0]) ? axis.extremeRange[0] : axis.valueRange[0]; + var y1 = isNullUndefinedOrNaN(axis.valueRange[1]) ? axis.extremeRange[1] : axis.valueRange[1]; + if (!ypadCompat) { + if (axis.logscale) { + var logpad = Math.exp(Math.log(span) * ypad); + y0 *= logpad; + y1 /= logpad; + } else { + var span = y1 - y0; + y0 -= span * ypad; + y1 += span * ypad; + } + } + axis.computedValueRange = [y0, y1]; } else { axis.computedValueRange = axis.extremeRange; }