Support auto grid min/max when using log scale, Issue #3090

This commit is contained in:
bigbenhur 2016-06-03 15:54:14 +02:00
parent b08f8bab71
commit 1e4eb01059
2 changed files with 34 additions and 30 deletions

View File

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

View File

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