From 209fe5336ab6a1f3acdd3f0567104d2ae46f9bde Mon Sep 17 00:00:00 2001 From: David Schnur Date: Sat, 6 Apr 2013 14:37:36 -0400 Subject: [PATCH] Always recalculate tickDecimals and tickSize. Resolves #1000. In Flot 0.7 we only calculated tickDecimals and tickSize once, when creating the tickGenerator for the first time. This meant that calls to setupGrid failed to recalculate the values, as reported in #860. #861 fixed the problem by moving calculation into tickGenerator, but this turned out to cause a new problem, since the function doesn't run when an explicit ticks array is provided. This commit solves both by always recalculating the values outside of the tickGenerator function. As far as I can tell the only reason it wasn't done this way from the beginning was to avoid unnecessary work in the case where tickGenerator is already provided in the options. But the extra work is negligible, and it's actually more consistent for the properties to always be set. --- jquery.flot.js | 74 ++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/jquery.flot.js b/jquery.flot.js index c5f11f9..daf77a2 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -1542,7 +1542,42 @@ Licensed under the MIT license. // some data points that seemed reasonable noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height); - axis.delta = (axis.max - axis.min) / noTicks; + var delta = (axis.max - axis.min) / noTicks, + dec = -Math.floor(Math.log(delta) / Math.LN10), + maxDec = opts.tickDecimals; + + if (maxDec != null && dec > maxDec) { + dec = maxDec; + } + + var magn = Math.pow(10, -dec), + norm = delta / magn, // norm is between 1.0 and 10.0 + size; + + if (norm < 1.5) { + size = 1; + } else if (norm < 3) { + size = 2; + // special case for 2.5, requires an extra decimal + if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) { + size = 2.5; + ++dec; + } + } else if (norm < 7.5) { + size = 5; + } else { + size = 10; + } + + size *= magn; + + if (opts.minTickSize != null && size < opts.minTickSize) { + size = opts.minTickSize; + } + + axis.delta = delta; + axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec); + axis.tickSize = opts.tickSize || size; // Time mode was moved to a plug-in in 0.8, but since so many people use this // we'll add an especially friendly make sure they remembered to include it. @@ -1557,46 +1592,13 @@ Licensed under the MIT license. if (!axis.tickGenerator) { axis.tickGenerator = function (axis) { - var maxDec = opts.tickDecimals, - dec = -Math.floor(Math.log(axis.delta) / Math.LN10); - - if (maxDec != null && dec > maxDec) - dec = maxDec; - - var magn = Math.pow(10, -dec), - norm = axis.delta / magn, // norm is between 1.0 and 10.0 - size, - ticks = [], - start, + var ticks = [], + start = floorInBase(axis.min, axis.tickSize), i = 0, v = Number.NaN, prev; - if (norm < 1.5) - size = 1; - else if (norm < 3) { - size = 2; - // special case for 2.5, requires an extra decimal - if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) { - size = 2.5; - ++dec; - } - } - else if (norm < 7.5) - size = 5; - else size = 10; - - size *= magn; - - if (opts.minTickSize != null && size < opts.minTickSize) - size = opts.minTickSize; - - axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec); - axis.tickSize = opts.tickSize || size; - - start = floorInBase(axis.min, axis.tickSize); - do { prev = v; v = start + i * axis.tickSize;