X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph.js;h=6bdb1433bea3233da61508d146a0af0e1351f979;hb=c0cef1fa7418206e16229e4ae201af16c3dc8305;hp=eff3052e20ae26f19df9e82423aa668bb566372d;hpb=d4b89d5690719c1d6e77b61b2afb132dd0cad92d;p=dygraphs.git diff --git a/dygraph.js b/dygraph.js index eff3052..6bdb143 100644 --- a/dygraph.js +++ b/dygraph.js @@ -441,7 +441,8 @@ Dygraph.prototype.toDataYCoord = function(y, axis) { var area = this.plotter_.area; var yRange = this.yAxisRange(axis); - if (!axis.logscale) { + if (typeof(axis) == "undefined") axis = 0; + if (!this.axes_[axis].logscale) { return yRange[0] + (area.h - y) / area.h * (yRange[1] - yRange[0]); } else { // Computing the inverse of toDomCoord. @@ -921,9 +922,17 @@ Dygraph.startPan = function(event, g, context) { var axis = g.axes_[i]; var yRange = g.yAxisRange(i); // TODO(konigsberg): These values should be in |context|. - axis.dragValueRange = yRange[1] - yRange[0]; - axis.initialTopValue = yRange[1]; + // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale. + if (axis.logscale) { + axis.initialTopValue = Dygraph.log10(yRange[1]); + axis.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]); + } else { + axis.initialTopValue = yRange[1]; + axis.dragValueRange = yRange[1] - yRange[0]; + } axis.unitsPerPixel = axis.dragValueRange / (g.plotter_.area.h - 1); + + // While calculating axes, set 2dpan. if (axis.valueWindow || axis.valueRange) context.is2DPan = true; } }; @@ -949,10 +958,19 @@ Dygraph.movePan = function(event, g, context) { // Adjust each axis appropriately. for (var i = 0; i < g.axes_.length; i++) { var axis = g.axes_[i]; - var maxValue = axis.initialTopValue + - (context.dragEndY - context.dragStartY) * axis.unitsPerPixel; + + var pixelsDragged = context.dragEndY - context.dragStartY; + var unitsDragged = pixelsDragged * axis.unitsPerPixel; + + // In log scale, maxValue and minValue are the logs of those values. + var maxValue = axis.initialTopValue + unitsDragged; var minValue = maxValue - axis.dragValueRange; - axis.valueWindow = [ minValue, maxValue ]; + if (axis.logscale) { + axis.valueWindow = [ Math.pow(Dygraph.LOG_SCALE, minValue), + Math.pow(Dygraph.LOG_SCALE, maxValue) ]; + } else { + axis.valueWindow = [ minValue, maxValue ]; + } } } @@ -1393,10 +1411,6 @@ Dygraph.prototype.mouseMove_ = function(event) { idx = i; } if (idx >= 0) lastx = points[idx].xval; - // Check that you can really highlight the last day's data - var last = points[points.length-1]; - if (last != null && canvasx > last.canvasx) - lastx = points[points.length-1].xval; // Extract the points we've selected this.selPoints_ = []; @@ -1904,6 +1918,10 @@ Dygraph.dateTicker = function(startDate, endDate, self) { } }; +// This is a list of human-friendly values at which to show tick marks on a log +// scale. It is k * 10^n, where k=1..9 and n=-39..+39, so: +// ..., 1, 2, 3, 4, 5, ..., 9, 10, 20, 30, ..., 90, 100, 200, 300, ... +// NOTE: this assumes that Dygraph.LOG_SCALE = 10. Dygraph.PREFERRED_LOG_TICK_VALUES = function() { var vals = []; for (var power = -39; power <= 39; power++) { @@ -1949,7 +1967,7 @@ Dygraph.binarySearch = function(val, arry, abs, low, high) { return mid; } } - return Dygraph.binarySearch(val, arry, abs, low, mid - 1); + return Dygraph.binarySearch(val, arry, abs, low, mid - 1); } if (element < val) { if (abs < 0) { @@ -1961,7 +1979,7 @@ Dygraph.binarySearch = function(val, arry, abs, low, high) { } return Dygraph.binarySearch(val, arry, abs, mid + 1, high); } -} +}; /** * Add ticks when the x axis has numbers on it (instead of dates) @@ -1998,18 +2016,37 @@ Dygraph.numericTicks = function(minV, maxV, self, axis_props, vals) { if (maxIdx == -1) { maxIdx = Dygraph.PREFERRED_LOG_TICK_VALUES.length - 1; } - console.log(minIdx, maxIdx); // Count the number of tick values would appear, if we can get at least // nTicks / 4 accept them. + var lastDisplayed = null; if (maxIdx - minIdx >= nTicks / 4) { + var axisId = axis_props.yAxisId; for (var idx = maxIdx; idx >= minIdx; idx--) { var tickValue = Dygraph.PREFERRED_LOG_TICK_VALUES[idx]; - ticks.push({ v: tickValue }); + var domCoord = axis_props.g.toDomYCoord(tickValue, axisId); + var tick = { v: tickValue }; + if (lastDisplayed == null) { + lastDisplayed = { + tickValue : tickValue, + domCoord : domCoord + }; + } else { + if (domCoord - lastDisplayed.domCoord >= pixelsPerTick) { + lastDisplayed = { + tickValue : tickValue, + domCoord : domCoord + }; + } else { + tick.label = ""; + } + } + ticks.push(tick); } // Since we went in backwards order. ticks.reverse(); } } + // ticks.length won't be 0 if the log scale function finds values to insert. if (ticks.length == 0) { // Basic idea: @@ -2142,7 +2179,7 @@ Dygraph.prototype.extremeValues_ = function(series) { * number of axes, rolling averages, etc. */ Dygraph.prototype.predraw_ = function() { - // TODO(danvk): movabilitye more computations out of drawGraph_ and into here. + // TODO(danvk): move more computations out of drawGraph_ and into here. this.computeYAxes_(); // Create a new plotter. @@ -2327,7 +2364,7 @@ Dygraph.prototype.drawGraph_ = function() { * indices are into the axes_ array. */ Dygraph.prototype.computeYAxes_ = function() { - this.axes_ = [{}]; // always have at least one y-axis. + this.axes_ = [{ yAxisId : 0, g : this }]; // always have at least one y-axis. this.seriesToAxisMap_ = {}; // Get a list of series names. @@ -2368,9 +2405,12 @@ Dygraph.prototype.computeYAxes_ = function() { var opts = {}; Dygraph.update(opts, this.axes_[0]); Dygraph.update(opts, { valueRange: null }); // shouldn't inherit this. + var yAxisId = this.axes_.length; + opts.yAxisId = yAxisId; + opts.g = this; Dygraph.update(opts, axis); this.axes_.push(opts); - this.seriesToAxisMap_[seriesName] = this.axes_.length - 1; + this.seriesToAxisMap_[seriesName] = yAxisId; } }