Commit 209fe533 authored by David Schnur's avatar David Schnur

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.
parent 35a16ae7
...@@ -1542,60 +1542,62 @@ Licensed under the MIT license. ...@@ -1542,60 +1542,62 @@ Licensed under the MIT license.
// some data points that seemed reasonable // some data points that seemed reasonable
noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height); 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;
// Time mode was moved to a plug-in in 0.8, but since so many people use this if (maxDec != null && dec > maxDec) {
// we'll add an especially friendly make sure they remembered to include it.
if (opts.mode == "time" && !axis.tickGenerator) {
throw new Error("Time mode requires the flot.time plugin.");
}
// Flot supports base-10 axes; any other mode else is handled by a plug-in,
// like flot.time.js.
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; dec = maxDec;
}
var magn = Math.pow(10, -dec), var magn = Math.pow(10, -dec),
norm = axis.delta / magn, // norm is between 1.0 and 10.0 norm = delta / magn, // norm is between 1.0 and 10.0
size, size;
ticks = [], if (norm < 1.5) {
start,
i = 0,
v = Number.NaN,
prev;
if (norm < 1.5)
size = 1; size = 1;
else if (norm < 3) { } else if (norm < 3) {
size = 2; size = 2;
// special case for 2.5, requires an extra decimal // special case for 2.5, requires an extra decimal
if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) { if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
size = 2.5; size = 2.5;
++dec; ++dec;
} }
} } else if (norm < 7.5) {
else if (norm < 7.5)
size = 5; size = 5;
else size = 10; } else {
size = 10;
}
size *= magn; size *= magn;
if (opts.minTickSize != null && size < opts.minTickSize) if (opts.minTickSize != null && size < opts.minTickSize) {
size = opts.minTickSize; size = opts.minTickSize;
}
axis.delta = delta;
axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec); axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
axis.tickSize = opts.tickSize || size; axis.tickSize = opts.tickSize || size;
start = floorInBase(axis.min, axis.tickSize); // 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.
if (opts.mode == "time" && !axis.tickGenerator) {
throw new Error("Time mode requires the flot.time plugin.");
}
// Flot supports base-10 axes; any other mode else is handled by a plug-in,
// like flot.time.js.
if (!axis.tickGenerator) {
axis.tickGenerator = function (axis) {
var ticks = [],
start = floorInBase(axis.min, axis.tickSize),
i = 0,
v = Number.NaN,
prev;
do { do {
prev = v; prev = v;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment