From: Klaus Weidner Date: Tue, 18 Dec 2012 02:32:56 +0000 (-0800) Subject: tick scaling: Added comments, and adjust loop ends X-Git-Tag: v1.0.0~141^2 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=48f5d7627eb78e480b236db6ee6ce2d295bbad47;p=dygraphs.git tick scaling: Added comments, and adjust loop ends Update the mults arrays to cover the range from 1..base^2, and start the loop at a slightly lower value for safety. This should help ensure equivalent behavior to the pre-patch version. --- diff --git a/dygraph-tickers.js b/dygraph-tickers.js index 457d23c..80eb291 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -157,17 +157,31 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { var kmg2 = opts("labelsKMG2"); var mults, base; if (kmg2) { - mults = [1, 2, 4, 8, 16]; + mults = [1, 2, 4, 8, 16, 32, 64, 128, 256]; base = 16; } else { - mults = [1, 2, 5, 10]; + mults = [1, 2, 5, 10, 20, 50, 100]; base = 10; } - var wanted_ticks = pixels / pixels_per_tick; - var units_per_tick = Math.abs(b - a) / wanted_ticks; + // Get the maximum number of permitted ticks based on the + // graph's pixel size and pixels_per_tick setting. + var max_ticks = Math.ceil(pixels / pixels_per_tick); + + // Now calculate the data unit equivalent of this tick spacing. + // Use abs() since graphs may have a reversed Y axis. + var units_per_tick = Math.abs(b - a) / max_ticks; + + // Based on this, get a starting scale which is the largest + // integer power of the chosen base (10 or 16) that still remains + // below the requested pixels_per_tick spacing. var base_power = Math.floor(Math.log(units_per_tick) / Math.log(base)); var base_scale = Math.pow(base, base_power); + + // Now try multiples of the starting scale until we find one + // that results in tick marks spaced sufficiently far apart. + // The "mults" array should cover the range 1 .. base^2 to + // adjust for rounding and edge effects. var scale, low_val, high_val, nTicks, spacing; for (j = 0; j < mults.length; j++) { scale = base_scale * mults[j];