Sfoglia il codice sorgente

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

Torkel Ödegaard 11 anni fa
parent
commit
37176fa42d
2 ha cambiato i file con 46 aggiunte e 9 eliminazioni
  1. 5 3
      src/app/components/kbn.js
  2. 41 6
      src/app/panels/stats/module.js

+ 5 - 3
src/app/components/kbn.js

@@ -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);
 
 

+ 41 - 6
src/app/panels/stats/module.js

@@ -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) {
       $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) {
       }
       }
       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;
       }
       }