From 83e9bc48167393699b2ac4a6c9ef35d0e814522f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 14 Aug 2014 12:05:29 +0200 Subject: [PATCH] Graph: Fix for axis format none broken for numbers in exponential notation, Closes #696 --- CHANGELOG.md | 3 +++ src/app/components/kbn.js | 27 ++++++++++++++++++++++-- src/app/controllers/dashLoader.js | 10 --------- src/app/directives/grafanaGraph.js | 4 +--- src/app/directives/grafanaSimplePanel.js | 3 +-- src/test/specs/kbn-format-specs.js | 14 ++++++++++++ 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b900b3245f6..8215e325a1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - [Issue #578](https://github.com/grafana/grafana/issues/578). Dashboard: Row option to display row title even when the row is visible - [Issue #672](https://github.com/grafana/grafana/issues/672). Dashboard: panel fullscreen & edit state is present in url, can now link to graph in edit & fullscreen mode. +**Fixes** +- [Issue #696](https://github.com/grafana/grafana/issues/696). Graph: fix for y-axis format 'none' when values are in scientific notation (ex 2.3e-13) + **Tech** - Upgraded from angularjs 1.1.5 to 1.3 beta 17; - Switch from underscore to lodash diff --git a/src/app/components/kbn.js b/src/app/components/kbn.js index 07d0f21c090..566a2edcde7 100644 --- a/src/app/components/kbn.js +++ b/src/app/components/kbn.js @@ -525,12 +525,35 @@ function($, _, moment) { return kbn.nanosFormat(val, decimals); }; default: - return function(val) { - return val % 1 === 0 ? val : val.toFixed(decimals); + return function(val, axis) { + return kbn.noneFormat(val, axis ? axis.tickDecimals : decimals); }; } }; + kbn.noneFormat = function(value, decimals) { + var factor = decimals ? Math.pow(10, decimals) : 1; + var formatted = String(Math.round(value * factor) / factor); + + // if exponent return directly + if (formatted.indexOf('e') !== -1) { + return formatted; + } + + // If tickDecimals was specified, ensure that we have exactly that + // much precision; otherwise default to the value's own precision. + + if (decimals != null) { + var decimalPos = formatted.indexOf("."); + var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1; + if (precision < decimals) { + return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision); + } + } + + return formatted; + }; + kbn.msFormat = function(size, decimals) { // Less than 1 milli, downscale to micro if (Math.abs(size) < 1) { diff --git a/src/app/controllers/dashLoader.js b/src/app/controllers/dashLoader.js index 837674c9112..10123b9fd80 100644 --- a/src/app/controllers/dashLoader.js +++ b/src/app/controllers/dashLoader.js @@ -23,16 +23,6 @@ function (angular, _, moment, config) { $scope.zoom(2); }); - var count = 0; - $scope.$watch(function() { - console.log(1); - count++; - }, function(n, o) { - }); - - setTimeout(function() { - console.log("Total count:" + count); - }, 4000); }; $scope.set_default = function() { diff --git a/src/app/directives/grafanaGraph.js b/src/app/directives/grafanaGraph.js index 5b474bbbe24..a950032a7a9 100755 --- a/src/app/directives/grafanaGraph.js +++ b/src/app/directives/grafanaGraph.js @@ -303,9 +303,7 @@ function (angular, $, kbn, moment, _) { } function configureAxisMode(axis, format) { - if (format !== 'none') { - axis.tickFormatter = kbn.getFormatFunction(format, 1); - } + axis.tickFormatter = kbn.getFormatFunction(format, 1); } function time_format(interval, ticks, min, max) { diff --git a/src/app/directives/grafanaSimplePanel.js b/src/app/directives/grafanaSimplePanel.js index 72b218ab893..ce34e2841e7 100644 --- a/src/app/directives/grafanaSimplePanel.js +++ b/src/app/directives/grafanaSimplePanel.js @@ -1,8 +1,7 @@ define([ 'angular', - 'lodash' ], -function (angular, _) { +function (angular) { 'use strict'; angular diff --git a/src/test/specs/kbn-format-specs.js b/src/test/specs/kbn-format-specs.js index 0fcc0180f49..7e80d1a91eb 100644 --- a/src/test/specs/kbn-format-specs.js +++ b/src/test/specs/kbn-format-specs.js @@ -22,6 +22,20 @@ define([ }); + describe('high negative exponent, issue #696', function() { + it('should ignore decimal correction if exponent', function() { + var str = kbn.getFormatFunction('')(2.75e-10, { tickDecimals: 12 }); + expect(str).to.be('2.75e-10'); + }); + }); + + describe('none format tests', function() { + it('should translate 2 as 2.0000 if axis decimals is 4', function() { + var str = kbn.getFormatFunction('')(2, { tickDecimals: 4 }); + expect(str).to.be('2.0000'); + }); + }); + describe('nanosecond formatting', function () { it('should translate 25 to 25 ns', function () {