diff --git a/CHANGELOG.md b/CHANGELOG.md index db4e180ece1..d91d7704eb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - [Issue #1296](https://github.com/grafana/grafana/issues/1296). InfluxDB: Auto escape column names with special characters. Thanks @steven-aerts - [Issue #1321](https://github.com/grafana/grafana/issues/1321). SingleStatPanel: You can now use template variables in pre & postfix - [Issue #599](https://github.com/grafana/grafana/issues/599). Graph: Added right y axis label setting and graph support +- [Issue #1253](https://github.com/grafana/grafana/issues/1253). Graph & Singlestat: Users can now set decimal precision for legend and tooltips (override auto precision) **Fixes** - [Issue #1298](https://github.com/grafana/grafana/issues/1298). InfluxDB: Fix handling of empty array in templating variable query diff --git a/src/app/components/kbn.js b/src/app/components/kbn.js index 5ccd5b9508b..3c774dcb585 100644 --- a/src/app/components/kbn.js +++ b/src/app/components/kbn.js @@ -343,7 +343,7 @@ function($, _, moment) { if (steps >= limit) { return "NA"; } } - if (steps > 0) { + if (steps > 0 && scaledDecimals !== null) { decimals = scaledDecimals + (3 * steps); } @@ -400,6 +400,14 @@ function($, _, moment) { kbn.valueFormats.velocitymph = function(value, decimals) { return kbn.toFixed(value, decimals) + ' mph'; }; kbn.valueFormats.velocityknot = function(value, decimals) { return kbn.toFixed(value, decimals) + ' kn'; }; + kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) { + if (scaledDecimals === null) { + return kbn.toFixed(value, decimals) + ext; + } else { + return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext; + } + }; + kbn.valueFormats.ms = function(size, decimals, scaledDecimals) { if (size === null) { return ""; } @@ -408,22 +416,22 @@ function($, _, moment) { } // Less than 1 min else if (Math.abs(size) < 60000) { - return kbn.toFixed(size / 1000, scaledDecimals + 3) + " s"; + return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s"); } // Less than 1 hour, devide in minutes else if (Math.abs(size) < 3600000) { - return kbn.toFixed(size / 60000, scaledDecimals + 5) + " min"; + return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min"); } // Less than one day, devide in hours else if (Math.abs(size) < 86400000) { - return kbn.toFixed(size / 3600000, scaledDecimals + 7) + " hour"; + return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour"); } // Less than one year, devide in days else if (Math.abs(size) < 31536000000) { - return kbn.toFixed(size / 86400000, scaledDecimals + 8) + " day"; + return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day"); } - return kbn.toFixed(size / 31536000000, scaledDecimals + 10) + " year"; + return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year"); }; kbn.valueFormats.s = function(size, decimals, scaledDecimals) { @@ -434,22 +442,22 @@ function($, _, moment) { } // Less than 1 hour, devide in minutes else if (Math.abs(size) < 3600) { - return kbn.toFixed(size / 60, scaledDecimals + 1) + " min"; + return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min"); } // Less than one day, devide in hours else if (Math.abs(size) < 86400) { - return kbn.toFixed(size / 3600, scaledDecimals + 4) + " hour"; + return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour"); } // Less than one week, devide in days else if (Math.abs(size) < 604800) { - return kbn.toFixed(size / 86400, scaledDecimals + 5) + " day"; + return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day"); } // Less than one year, devide in week else if (Math.abs(size) < 31536000) { - return kbn.toFixed(size / 604800, scaledDecimals + 6) + " week"; + return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week"); } - return kbn.toFixed(size / 3.15569e7, scaledDecimals + 7) + " year"; + return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year"); }; kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) { @@ -459,10 +467,10 @@ function($, _, moment) { return kbn.toFixed(size, decimals) + " µs"; } else if (Math.abs(size) < 1000000) { - return kbn.toFixed(size / 1000, scaledDecimals + 3) + " ms"; + return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms"); } else { - return kbn.toFixed(size / 1000000, scaledDecimals + 6) + " s"; + return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s"); } }; @@ -473,16 +481,16 @@ function($, _, moment) { return kbn.toFixed(size, decimals) + " ns"; } else if (Math.abs(size) < 1000000) { - return kbn.toFixed(size / 1000, scaledDecimals + 3) + " µs"; + return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs"); } else if (Math.abs(size) < 1000000000) { - return kbn.toFixed(size / 1000000, scaledDecimals + 6) + " ms"; + return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms"); } else if (Math.abs(size) < 60000000000){ - return kbn.toFixed(size / 1000000000, scaledDecimals + 9) + " s"; + return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s"); } else { - return kbn.toFixed(size / 60000000000, scaledDecimals + 12) + " m"; + return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min"); } }; diff --git a/src/app/panels/graph/axisEditor.html b/src/app/panels/graph/axisEditor.html index ff1fbad997d..edf61a04cd3 100644 --- a/src/app/panels/graph/axisEditor.html +++ b/src/app/panels/graph/axisEditor.html @@ -144,7 +144,7 @@
-
+
  • Legend values @@ -214,6 +214,20 @@
+
+
    +
  • + Decimals +
  • +
  • + +
  • +
+
+
+ +
diff --git a/src/app/panels/graph/graph.js b/src/app/panels/graph/graph.js index 5bb028bf3f0..967991e5da8 100755 --- a/src/app/panels/graph/graph.js +++ b/src/app/panels/graph/graph.js @@ -113,11 +113,17 @@ function (angular, $, kbn, moment, _, GraphTooltip) { var axis = yaxis[series.yaxis - 1]; var formater = kbn.valueFormats[scope.panel.y_formats[series.yaxis - 1]]; - // legend and tooltip gets one more decimal precision - // than graph legend ticks - var tickDecimals = (axis.tickDecimals || -1) + 1; + // decimal override + if (scope.panel.decimals) { + series.updateLegendValues(formater, scope.panel.decimals, null); + } else { + // auto decimals + // legend and tooltip gets one more decimal precision + // than graph legend ticks + var tickDecimals = (axis.tickDecimals || -1) + 1; + series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); + } - series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); if(!scope.$$phase) { scope.$digest(); } } diff --git a/src/app/panels/graph/legend.js b/src/app/panels/graph/legend.js index d4a48e2f498..fb07275310d 100644 --- a/src/app/panels/graph/legend.js +++ b/src/app/panels/graph/legend.js @@ -143,13 +143,13 @@ function (angular, app, _, kbn, $) { html += '' + series.label + ''; html += ''; - var avg = series.formatValue(series.stats.avg); - var current = series.formatValue(series.stats.current); - var min = series.formatValue(series.stats.min); - var max = series.formatValue(series.stats.max); - var total = series.formatValue(series.stats.total); - if (panel.legend.values) { + var avg = series.formatValue(series.stats.avg); + var current = series.formatValue(series.stats.current); + var min = series.formatValue(series.stats.min); + var max = series.formatValue(series.stats.max); + var total = series.formatValue(series.stats.total); + if (panel.legend.min) { html += '
' + min + '
'; } if (panel.legend.max) { html += '
' + max + '
'; } if (panel.legend.avg) { html += '
' + avg + '
'; } diff --git a/src/app/panels/singlestat/editor.html b/src/app/panels/singlestat/editor.html index da3892a59f0..2573118588d 100644 --- a/src/app/panels/singlestat/editor.html +++ b/src/app/panels/singlestat/editor.html @@ -59,11 +59,18 @@
  • Unit
  • - +
  • + Decimals +
  • +
  • + +
  • diff --git a/src/app/panels/singlestat/module.js b/src/app/panels/singlestat/module.js index 1b802740017..81167a83a7d 100644 --- a/src/app/panels/singlestat/module.js +++ b/src/app/panels/singlestat/module.js @@ -123,6 +123,9 @@ function (angular, app, _, TimeSeries, kbn, PanelMeta) { }; $scope.getDecimalsForValue = function(value) { + if ($scope.panel.decimals) { + return { decimals: $scope.panel.decimals, scaledDecimals: null }; + } var delta = value / 2; var dec = -Math.floor(Math.log(delta) / Math.LN10); diff --git a/src/test/specs/kbn-format-specs.js b/src/test/specs/kbn-format-specs.js index ecc294d0543..c7b9cf8cee1 100644 --- a/src/test/specs/kbn-format-specs.js +++ b/src/test/specs/kbn-format-specs.js @@ -37,6 +37,22 @@ define([ }); }); + describe('kbn ms format when scaled decimals is null do not use it', function() { + it('should use specified decimals', function() { + var str = kbn.valueFormats['ms'](10000086.123, 1, null); + expect(str).to.be('2.8 hour'); + }); + }); + + describe('kbn kbytes format when scaled decimals is null do not use it', function() { + it('should use specified decimals', function() { + var str = kbn.valueFormats['kbytes'](10000000, 3, null); + expect(str).to.be('9.537 GiB'); + }); + }); + + + describe('calculateInterval', function() { it('1h 100 resultion', function() { var range = { from: kbn.parseDate('now-1h'), to: kbn.parseDate('now') };