graph: merge fixes for #5278

This is an old PR so had some problems after merging in master.

- Fix for min and max that were not getting passed into the
applyLogScale function
- Fix for when min/max were undefined rather than null
- Fix for decimal ticks, as this PR populates the ticks itself then
it also needs to calculate the number of decimal places too. Flot
was showing the wrong number of decimal places sometimes otherwise
on the y-axis.
This commit is contained in:
Daniel Lee 2017-03-10 18:46:36 +01:00
parent 28cf229707
commit e19b48840f
2 changed files with 26 additions and 12 deletions

View File

@ -460,7 +460,8 @@ coreModule.directive('grafanaGraph', function($rootScope, timeSrv) {
show: panel.yaxes[0].show, show: panel.yaxes[0].show,
index: 1, index: 1,
logBase: panel.yaxes[0].logBase || 1, logBase: panel.yaxes[0].logBase || 1,
max: null min: panel.yaxes[0].min ? _.toNumber(panel.yaxes[0].min) : null,
max: panel.yaxes[0].max ? _.toNumber(panel.yaxes[0].max) : null,
}; };
options.yaxes.push(defaults); options.yaxes.push(defaults);
@ -471,12 +472,13 @@ coreModule.directive('grafanaGraph', function($rootScope, timeSrv) {
secondY.show = panel.yaxes[1].show; secondY.show = panel.yaxes[1].show;
secondY.logBase = panel.yaxes[1].logBase || 1; secondY.logBase = panel.yaxes[1].logBase || 1;
secondY.position = 'right'; secondY.position = 'right';
secondY.min = panel.yaxes[1].min ? _.toNumber(panel.yaxes[1].min) : null;
secondY.max = panel.yaxes[1].max ? _.toNumber(panel.yaxes[1].max) : null;
options.yaxes.push(secondY); options.yaxes.push(secondY);
applyLogScale(options.yaxes[1], data); applyLogScale(options.yaxes[1], data);
configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format);
} }
applyLogScale(options.yaxes[0], data); applyLogScale(options.yaxes[0], data);
configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format);
} }
@ -498,10 +500,10 @@ coreModule.directive('grafanaGraph', function($rootScope, timeSrv) {
for (i = 0; i < data.length; i++) { for (i = 0; i < data.length; i++) {
series = data[i]; series = data[i];
if (series.yaxis === axis.index) { if (series.yaxis === axis.index) {
if (max === null || max < series.stats.max) { if (!max || max < series.stats.max) {
max = series.stats.max; max = series.stats.max;
} }
if (min === null || min > series.stats.logmin) { if (!min || min > series.stats.logmin) {
min = series.stats.logmin; min = series.stats.logmin;
} }
} }
@ -510,27 +512,27 @@ coreModule.directive('grafanaGraph', function($rootScope, timeSrv) {
axis.transform = function(v) { return (v < Number.MIN_VALUE) ? null : Math.log(v) / Math.log(axis.logBase); }; 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); }; axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); };
if (max === null && min === null) { if (!max && !min) {
max = axis.inverseTransform(+2); max = axis.inverseTransform(+2);
min = axis.inverseTransform(-2); min = axis.inverseTransform(-2);
} else if (max === null) { } else if (!max) {
max = min*axis.inverseTransform(+4); max = min*axis.inverseTransform(+4);
} else if (min === null) { } else if (!min) {
min = max*axis.inverseTransform(-4); min = max*axis.inverseTransform(-4);
} }
if (axis.min !== null) { if (axis.min) {
min = axis.inverseTransform(Math.ceil(axis.transform(axis.min))); min = axis.inverseTransform(Math.ceil(axis.transform(axis.min)));
} else { } else {
min = axis.min = axis.inverseTransform(Math.floor(axis.transform(min))); min = axis.min = axis.inverseTransform(Math.floor(axis.transform(min)));
} }
if (axis.max !== null) { if (axis.max) {
max = axis.inverseTransform(Math.floor(axis.transform(axis.max))); max = axis.inverseTransform(Math.floor(axis.transform(axis.max)));
} else { } else {
max = axis.max = axis.inverseTransform(Math.ceil(axis.transform(max))); max = axis.max = axis.inverseTransform(Math.ceil(axis.transform(max)));
} }
if (min < Number.MIN_VALUE || max < Number.MIN_VALUE) { if (!min || min < Number.MIN_VALUE || !max || max < Number.MIN_VALUE) {
return; return;
} }
@ -539,6 +541,13 @@ coreModule.directive('grafanaGraph', function($rootScope, timeSrv) {
for (nextTick = min; nextTick <= max; nextTick *= axis.logBase) { for (nextTick = min; nextTick <= max; nextTick *= axis.logBase) {
axis.ticks.push(nextTick); axis.ticks.push(nextTick);
} }
axis.tickDecimals = decimalPlaces(min);
}
function decimalPlaces(num) {
if (!num) { return 0; }
return (num.toString().split('.')[1] || []).length;
} }
function configureAxisMode(axis, format) { function configureAxisMode(axis, format) {

View File

@ -121,8 +121,8 @@ describe('grafanaGraph', function() {
}); });
data[0].yaxis = 1; data[0].yaxis = 1;
ctrl.panel.yaxes[1].logBase = 10; ctrl.panel.yaxes[1].logBase = 10;
ctrl.panel.yaxes[1].min = 0.05; ctrl.panel.yaxes[1].min = '0.05';
ctrl.panel.yaxes[1].max = 1500; ctrl.panel.yaxes[1].max = '1500';
data[1] = new TimeSeries({ data[1] = new TimeSeries({
datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]], datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]],
alias: 'seriesFixedscale', alias: 'seriesFixedscale',
@ -139,12 +139,17 @@ describe('grafanaGraph', function() {
expect(axisAutoscale.ticks.length).to.be(8); expect(axisAutoscale.ticks.length).to.be(8);
expect(axisAutoscale.ticks[0]).to.be(0.001); expect(axisAutoscale.ticks[0]).to.be(0.001);
expect(axisAutoscale.ticks[7]).to.be(10000); expect(axisAutoscale.ticks[7]).to.be(10000);
expect(axisAutoscale.tickDecimals).to.be(3);
var axisFixedscale = ctx.plotOptions.yaxes[1]; var axisFixedscale = ctx.plotOptions.yaxes[1];
expect(axisFixedscale.min).to.be(0.05); expect(axisFixedscale.min).to.be(0.05);
expect(axisFixedscale.max).to.be(1500); expect(axisFixedscale.max).to.be(1500);
expect(axisFixedscale.ticks.length).to.be(5); expect(axisFixedscale.ticks.length).to.be(5);
expect(axisFixedscale.ticks[0]).to.be(0.1); expect(axisFixedscale.ticks[0]).to.be(0.1);
expect(axisFixedscale.ticks[4]).to.be(1000); expect(axisFixedscale.ticks[4]).to.be(1000);
expect(axisFixedscale.tickDecimals).to.be(1);
}); });
}); });