Commit 37176fa4 by Torkel Ödegaard

SingleStatPanel: Finnaly solved automatic decimal precision calculation for singlestat panel, #951

parent 7ff8931d
...@@ -327,18 +327,20 @@ function($, _, moment) { ...@@ -327,18 +327,20 @@ function($, _, moment) {
size /= factor; size /= factor;
} }
if (steps > 0) { 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) { if (value === null) {
return ""; return "";
} }
decimals = decimals || fallbackDecimals;
var factor = decimals ? Math.pow(10, decimals) : 1; var factor = decimals ? Math.pow(10, decimals) : 1;
var formatted = String(Math.round(value * factor) / factor); var formatted = String(Math.round(value * factor) / factor);
......
...@@ -65,10 +65,6 @@ function (angular, app, _, TimeSeries, kbn) { ...@@ -65,10 +65,6 @@ function (angular, app, _, TimeSeries, kbn) {
$scope.$on('refresh', $scope.get_data); $scope.$on('refresh', $scope.get_data);
}; };
$scope.formatValue = function(value) {
return kbn.valueFormats[$scope.panel.format](value, 0, -7);
};
$scope.updateTimeRange = function () { $scope.updateTimeRange = function () {
$scope.range = timeSrv.timeRange(); $scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false); $scope.rangeUnparsed = timeSrv.timeRange(false);
...@@ -132,6 +128,43 @@ function (angular, app, _, TimeSeries, kbn) { ...@@ -132,6 +128,43 @@ function (angular, app, _, TimeSeries, kbn) {
$scope.render(); $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() { $scope.render = function() {
var data = {}; var data = {};
...@@ -140,9 +173,11 @@ function (angular, app, _, TimeSeries, kbn) { ...@@ -140,9 +173,11 @@ function (angular, app, _, TimeSeries, kbn) {
} }
else { else {
var series = $scope.series[0]; var series = $scope.series[0];
series.updateLegendValues(kbn.valueFormats[$scope.panel.format], 2, -7);
data.mainValue = series.stats[$scope.panel.valueName]; 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; data.flotpairs = series.flotpairs;
} }
......
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