From 7d0e7a0d407d697cac55d13132e6bae29507f7a1 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Thu, 27 Jan 2011 15:01:42 -0500 Subject: [PATCH] Didn't realize that the numeric tick function was used for x axes. By having to address that, though, I see now that I've managed to make the log scaling axis-specific. At least I hope -- a test that shows two axes, each with its own log scale, would be kind of nice. --- dygraph-canvas.js | 2 +- dygraph.js | 21 +++++++++++---------- tests/logscale.html | 26 +++++++++++++++++++++----- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 245dc6f..b90695b 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -107,7 +107,7 @@ DygraphLayout.prototype._evaluateLineCharts = function() { var item = dataset[j]; var yval; - if (this.dygraph_.attr_("logscale")) { + if (axis.logscale) { yval = 1.0 - ((Dygraph.log10(parseFloat(item[1])) - Dygraph.log10(axis.minyval)) * axis.ylogscale); // really should just be yscale. } else { yval = 1.0 - ((parseFloat(item[1]) - axis.minyval) * axis.yscale); diff --git a/dygraph.js b/dygraph.js index 5b35b8a..ccc79b7 100644 --- a/dygraph.js +++ b/dygraph.js @@ -119,7 +119,6 @@ Dygraph.DEFAULT_ATTRS = { delimiter: ',', - logScale: false, sigma: 2.0, errorBars: false, fractions: false, @@ -443,7 +442,7 @@ Dygraph.prototype.toDataYCoord = function(y, axis) { var area = this.plotter_.area; var yRange = this.yAxisRange(axis); - if (!this.attr_("logscale")) { + if (!axis.logscale) { return yRange[0] + (area.h - y) / area.h * (yRange[1] - yRange[0]); } else { // Computing the inverse of toDomCoord. @@ -489,12 +488,13 @@ Dygraph.prototype.toPercentYCoord = function(y, axis) { if (y == null) { return null; } + if (typeof(axis) == "undefined") axis = 0; var area = this.plotter_.area; var yRange = this.yAxisRange(axis); var pct; - if (!this.attr_("logscale")) { + if (!this.axes_[axis].logscale) { // yrange[1] - y is unit distance from the bottom. // yrange[1] - yrange[0] is the scale of the range. // (yRange[1] - y) / (yRange[1] - yRange[0]) is the % from the bottom. @@ -1918,8 +1918,8 @@ Dygraph.dateTicker = function(startDate, endDate, self) { * Add ticks when the x axis has numbers on it (instead of dates) * TODO(konigsberg): Update comment. * - * @param {Number} startDate Start of the date window (millis since epoch) - * @param {Number} endDate End of the date window (millis since epoch) + * @param {Number} minV minimum value + * @param {Number} maxV maximum value * @param self * @param {function} attribute accessor function. * @return {Array.} Array of {label, value} tuples. @@ -1937,7 +1937,7 @@ Dygraph.numericTicks = function(minV, maxV, self, axis_props, vals) { ticks.push({v: vals[i]}); } } else { - if (self.attr_("logscale")) { + if (axis_props && attr("logscale")) { // As opposed to the other ways for computing ticks, we're just going // for nearby values. There's no reasonable way to scale the values // (unless we want to show strings like "log(" + x + ")") in which case @@ -1945,6 +1945,7 @@ Dygraph.numericTicks = function(minV, maxV, self, axis_props, vals) { // so compute height / pixelsPerTick and move on. var pixelsPerTick = attr('pixelsPerYLabel'); + // NOTE(konigsberg): Dan, should self.height_ be self.plotter_.area.h? var nTicks = Math.floor(self.height_ / pixelsPerTick); var vv = minV; @@ -2081,7 +2082,7 @@ Dygraph.prototype.extremeValues_ = function(series) { * number of axes, rolling averages, etc. */ Dygraph.prototype.predraw_ = function() { - // TODO(danvk): move more computations out of drawGraph_ and into here. + // TODO(danvk): movabilitye more computations out of drawGraph_ and into here. this.computeYAxes_(); // Create a new plotter. @@ -2271,7 +2272,8 @@ Dygraph.prototype.computeYAxes_ = function() { 'pixelsPerYLabel', 'yAxisLabelWidth', 'axisLabelFontSize', - 'axisTickSize' + 'axisTickSize', + 'logscale' ]; // Copy global axis options over to the first axis. @@ -2359,7 +2361,6 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { // Compute extreme values, a span and tick marks for each axis. for (var i = 0; i < this.axes_.length; i++) { - var isLogScale = this.attr_("logscale"); var axis = this.axes_[i]; if (axis.valueWindow) { // This is only set if the user has zoomed on the y-axis. It is never set @@ -2387,7 +2388,7 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { var maxAxisY; var minAxisY; - if (isLogScale) { + if (axis.logscale) { var maxAxisY = maxY + 0.1 * span; var minAxisY = minY; } else { diff --git a/tests/logscale.html b/tests/logscale.html index 30e4a32..2ce0680 100644 --- a/tests/logscale.html +++ b/tests/logscale.html @@ -12,7 +12,10 @@

Log scale demo - work in progress

+

x-axis of dates

+

x-axis of numbers

+
@@ -27,21 +30,34 @@ "20101206,30\n"+ "20101207,80\n"+ "20101208,100\n"+ - "20101209,250\n"+ + "20101209,500\n"+ ""; } var g = new Dygraph(document.getElementById("div_g"), Data, { logscale : true }); - Dygraph.addEvent(g.canvas_, '_mousemove', function(e) { - var y = Dygraph.pageY(e) - Dygraph.findPosY(g.canvas_); - console.log(y, g.toDataYCoord(y)); - }); + function Data2() { + return "X,A\n" + + "1,5\n"+ + "2,10\n"+ + "3,100\n"+ + "4,250\n"+ + "5,1000\n"+ + "6,30\n"+ + "7,80\n"+ + "8,100\n"+ + "9,500\n"+ + ""; + } + var g2 = new Dygraph(document.getElementById("div_g2"), + Data2, { logscale : true }); function logScale() { g.updateOptions({ logscale : true }); + g2.updateOptions({ logscale : true }); } function linearScale() { g.updateOptions({ logscale : false }); + g2.updateOptions({ logscale : false }); } -- 2.7.4