From 985cccae6a9969d11bd8504c80c73f7394ef2a29 Mon Sep 17 00:00:00 2001 From: David Schnur Date: Thu, 22 Nov 2012 18:30:54 -0500 Subject: [PATCH 1/2] Add tickDecimals to the setting-options example. It's useful to include the tickDecimals option in one of our examples, so it's easier to test across versions. The setting-options example is the logical place to put it. --- examples/setting-options.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/setting-options.html b/examples/setting-options.html index 8d1967e..1e359da 100644 --- a/examples/setting-options.html +++ b/examples/setting-options.html @@ -48,7 +48,8 @@ $(function () { yaxis: { ticks: 10, min: -2, - max: 2 + max: 2, + tickDecimals: 3 }, grid: { backgroundColor: { colors: ["#fff", "#eee"] } From 0e9936051d548e32d609d905cce6ca6aecfe2682 Mon Sep 17 00:00:00 2001 From: David Schnur Date: Thu, 22 Nov 2012 18:31:31 -0500 Subject: [PATCH 2/2] Fixed axis.tickDecimals that were broken by #50. Pull request #50 inadvertently broke the behavior of axis.tickDecimals, which previously added precision up to the given value. The broken code effectively ignored the setting for values with less precision. This fix brings back the old behavior. --- jquery.flot.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/jquery.flot.js b/jquery.flot.js index def82f5..e3d0d00 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -1290,9 +1290,23 @@ return ticks; }; - axis.tickFormatter = function (v, axis) { - var factor = Math.pow(10, axis.tickDecimals); - return "" + Math.round(v * factor) / factor; + axis.tickFormatter = function (value, axis) { + + var factor = Math.pow(10, axis.tickDecimals); + var formatted = "" + Math.round(value * factor) / factor; + + // If tickDecimals was specified, ensure that we have exactly that + // much precision; otherwise default to the value's own precision. + + if (axis.tickDecimals != null) { + var decimal = formatted.indexOf("."); + var precision = decimal == -1 ? 0 : formatted.length - decimal - 1; + if (precision < axis.tickDecimals) { + return (precision ? formatted : formatted + ".") + ("" + factor).substr(1, axis.tickDecimals - precision); + } + } + + return formatted; }; }