further tests for log-autoscale and log-fixedscale; only generate visible ticks; minor changes to prevent crashes due to bad user input

This commit is contained in:
bigbenhur 2016-06-06 10:33:35 +02:00
parent 29b9d17faa
commit ef623846c1
2 changed files with 54 additions and 19 deletions

View File

@ -376,6 +376,9 @@ function (angular, $, moment, _, kbn, GraphTooltip) {
if (axis.min < Number.MIN_VALUE) {
axis.min = null;
}
if (axis.max < Number.MIN_VALUE) {
axis.max = null;
}
var series, i;
var max = axis.max, min = axis.min;
@ -392,23 +395,32 @@ function (angular, $, moment, _, kbn, GraphTooltip) {
}
}
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.transform = function(v) { return (v < Number.MIN_VALUE) ? null : Math.log(v) / Math.log(axis.logBase); };
axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); };
min = axis.inverseTransform(Math.floor(axis.transform(min)));
max = axis.inverseTransform(Math.ceil(axis.transform(max)));
if (max === null && min === null) {
max = axis.inverseTransform(+2);
min = axis.inverseTransform(-2);
} else if (max === null) {
max = min*axis.inverseTransform(+4);
} else if (min === null) {
min = max*axis.inverseTransform(-4);
}
axis.min = axis.min !== null ? axis.min : min;
axis.max = axis.max !== null ? axis.max : max;
if (axis.min !== null) {
min = axis.inverseTransform(Math.ceil(axis.transform(axis.min)));
} else {
min = axis.min = axis.inverseTransform(Math.floor(axis.transform(min)));
}
if (axis.max !== null) {
max = axis.inverseTransform(Math.floor(axis.transform(axis.max)));
} else {
max = axis.max = axis.inverseTransform(Math.ceil(axis.transform(max)));
}
if (min < Number.MIN_VALUE || max < Number.MIN_VALUE) {
return;
}
axis.ticks = [];
var nextTick;

View File

@ -166,15 +166,38 @@ describe('grafanaGraph', function() {
});
graphScenario('when logBase is log 10', function(ctx) {
ctx.setup(function(ctrl) {
ctx.setup(function(ctrl, data) {
ctrl.panel.yaxes[0].logBase = 10;
data[0] = new TimeSeries({
datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]],
alias: 'seriesAutoscale',
});
data[0].yaxis = 1;
ctrl.panel.yaxes[1].logBase = 10;
ctrl.panel.yaxes[1].min = 0.05;
ctrl.panel.yaxes[1].max = 1500;
data[1] = new TimeSeries({
datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]],
alias: 'seriesFixedscale',
});
data[1].yaxis = 2;
});
it('should apply axis transform and ticks', function() {
var axis = ctx.plotOptions.yaxes[0];
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);
it('should apply axis transform, autoscaling (if necessary) and ticks', function() {
var axisAutoscale = ctx.plotOptions.yaxes[0];
expect(axisAutoscale.transform(100)).to.be(2);
expect(axisAutoscale.inverseTransform(-3)).to.be(0.001);
expect(axisAutoscale.min).to.be(0.001);
expect(axisAutoscale.max).to.be(10000);
expect(axisAutoscale.ticks.length).to.be(8);
expect(axisAutoscale.ticks[0]).to.be(0.001);
expect(axisAutoscale.ticks[7]).to.be(10000);
var axisFixedscale = ctx.plotOptions.yaxes[1];
expect(axisFixedscale.min).to.be(0.05);
expect(axisFixedscale.max).to.be(1500);
expect(axisFixedscale.ticks.length).to.be(5);
expect(axisFixedscale.ticks[0]).to.be(0.1);
expect(axisFixedscale.ticks[4]).to.be(1000);
});
});