From 37176fa42d978acb3ca530b8b79f0cdf697dfcb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 6 Nov 2014 12:17:46 +0100 Subject: [PATCH] SingleStatPanel: Finnaly solved automatic decimal precision calculation for singlestat panel, #951 --- src/app/components/kbn.js | 8 +++--- src/app/panels/stats/module.js | 47 +++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/app/components/kbn.js b/src/app/components/kbn.js index 567e7c78f23..071b60561d2 100644 --- a/src/app/components/kbn.js +++ b/src/app/components/kbn.js @@ -327,18 +327,20 @@ function($, _, moment) { size /= factor; } if (steps > 0) { - decimals = scaledDecimals + (3 * steps); + scaledDecimals = scaledDecimals + (3 * steps); } - return kbn.toFixed(size, decimals) + extArray[steps]; + return kbn.toFixed(size, scaledDecimals, decimals) + extArray[steps]; }; }; - kbn.toFixed = function(value, decimals) { + kbn.toFixed = function(value, decimals, fallbackDecimals) { if (value === null) { return ""; } + decimals = decimals || fallbackDecimals; + var factor = decimals ? Math.pow(10, decimals) : 1; var formatted = String(Math.round(value * factor) / factor); diff --git a/src/app/panels/stats/module.js b/src/app/panels/stats/module.js index 1705fd0b9bd..31ebf0aeba6 100644 --- a/src/app/panels/stats/module.js +++ b/src/app/panels/stats/module.js @@ -65,10 +65,6 @@ function (angular, app, _, TimeSeries, kbn) { $scope.$on('refresh', $scope.get_data); }; - $scope.formatValue = function(value) { - return kbn.valueFormats[$scope.panel.format](value, 0, -7); - }; - $scope.updateTimeRange = function () { $scope.range = timeSrv.timeRange(); $scope.rangeUnparsed = timeSrv.timeRange(false); @@ -132,6 +128,43 @@ function (angular, app, _, TimeSeries, kbn) { $scope.render(); }; + $scope.getDecimalsForValue = function(value) { + var opts = {}; + + var delta = value / 2; + var dec = -Math.floor(Math.log(delta) / Math.LN10); + + 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) { + 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; + } + + var result = {}; + result.decimals = Math.max(0, dec); + result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10); + return result; + }; + $scope.render = function() { var data = {}; @@ -140,9 +173,11 @@ function (angular, app, _, TimeSeries, kbn) { } else { var series = $scope.series[0]; - series.updateLegendValues(kbn.valueFormats[$scope.panel.format], 2, -7); data.mainValue = series.stats[$scope.panel.valueName]; - data.mainValueFormated = $scope.formatValue(data.mainValue); + var decimalInfo = $scope.getDecimalsForValue(data.mainValue); + var formatFunc = kbn.valueFormats[$scope.panel.format]; + + data.mainValueFormated = formatFunc(data.mainValue, decimalInfo.decimals, decimalInfo.scaledDecimals); data.flotpairs = series.flotpairs; }