From 1e4eb0105942debe27079174bd284bad5b1824f4 Mon Sep 17 00:00:00 2001 From: bigbenhur Date: Fri, 3 Jun 2016 15:54:14 +0200 Subject: [PATCH] Support auto grid min/max when using log scale, Issue #3090 --- public/app/plugins/panel/graph/graph.js | 58 ++++++++++--------- .../plugins/panel/graph/specs/graph_specs.ts | 6 +- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 86ad3b5f025..8019a51354b 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -373,42 +373,46 @@ function (angular, $, moment, _, kbn, GraphTooltip) { if (axis.logBase === 1) { return; } + if (axis.min < Number.MIN_VALUE) { + axis.min = null; + } var series, i; - var max = axis.max; + var max = axis.max, min = axis.min; - if (max === null) { - for (i = 0; i < data.length; i++) { - series = data[i]; - if (series.yaxis === axis.index) { - if (max < series.stats.max) { - max = series.stats.max; - } + for (i = 0; i < data.length; i++) { + series = data[i]; + if (series.yaxis === axis.index) { + if (max === null || max < series.stats.max) { + max = series.stats.max; + } + if (min === null || min > series.stats.min) { + min = series.stats.min; } } - if (max === void 0) { - max = Number.MAX_VALUE; - } + } + if (max === null && min === null) { + max = Math.pow(axis.logBase,+2); + min = Math.pow(axis.logBase,-2); + } else if (max === null) { + max = min*Math.pow(axis.logBase,+4); + } else if (min === null) { + min = max*Math.pow(axis.logBase,-4); } - axis.min = axis.min !== null ? axis.min : 0; - axis.ticks = [0, 1]; - var nextTick = 1; + axis.transform = function(v) { return Math.log(v) / Math.log(axis.logBase); }; + axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; - while (true) { - nextTick = nextTick * axis.logBase; + min = axis.inverseTransform(Math.floor(axis.transform(min))); + max = axis.inverseTransform(Math.ceil(axis.transform(max))); + + axis.min = axis.min !== null ? axis.min : min; + axis.max = axis.max !== null ? axis.max : max; + + axis.ticks = []; + var nextTick; + for (nextTick = min; nextTick <= max; nextTick *= axis.logBase) { axis.ticks.push(nextTick); - if (nextTick > max) { - break; - } - } - - if (axis.logBase === 10) { - axis.transform = function(v) { return Math.log(v+0.1); }; - axis.inverseTransform = function (v) { return Math.pow(10,v); }; - } else { - axis.transform = function(v) { return Math.log(v+0.1) / Math.log(axis.logBase); }; - axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; } } diff --git a/public/app/plugins/panel/graph/specs/graph_specs.ts b/public/app/plugins/panel/graph/specs/graph_specs.ts index b9c9362e5de..b383a31a976 100644 --- a/public/app/plugins/panel/graph/specs/graph_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_specs.ts @@ -172,9 +172,9 @@ describe('grafanaGraph', function() { it('should apply axis transform and ticks', function() { var axis = ctx.plotOptions.yaxes[0]; - expect(axis.transform(100)).to.be(Math.log(100+0.1)); - expect(axis.ticks[0]).to.be(0); - expect(axis.ticks[1]).to.be(1); + expect(axis.transform(100)).to.be(Math.log(100)/Math.log(10)); + expect(axis.ticks[0]).to.be(0.01); + expect(axis.ticks[1]).to.be(0.1); }); });