mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
beginAtZero support for logarithmic (#7862)
This commit is contained in:
parent
06b5325778
commit
f1ed2ee932
@ -64,6 +64,7 @@ export default class LogarithmicScale extends Scale {
|
||||
parse(raw, index) {
|
||||
const value = LinearScaleBase.prototype.parse.apply(this, [raw, index]);
|
||||
if (value === 0) {
|
||||
this._zero = true;
|
||||
return undefined;
|
||||
}
|
||||
return isFinite(value) && value > 0 ? value : NaN;
|
||||
@ -101,6 +102,11 @@ export default class LogarithmicScale extends Scale {
|
||||
if (max <= 0) {
|
||||
max = Math.pow(10, Math.floor(log10(min)) + 1);
|
||||
}
|
||||
// if data has `0` in it or `beginAtZero` is true, and min (non zero) value is at bottom
|
||||
// of scale, lower the min bound by one exp.
|
||||
if (!me._userMin && me._zero && min === Math.pow(10, Math.floor(log10(me.min)))) {
|
||||
min = Math.pow(10, Math.floor(log10(min)) - 1);
|
||||
}
|
||||
me.min = min;
|
||||
me.max = max;
|
||||
}
|
||||
@ -153,6 +159,7 @@ export default class LogarithmicScale extends Scale {
|
||||
|
||||
me._startValue = log10(start);
|
||||
me._valueRange = log10(me.max) - log10(start);
|
||||
me._zero = me.options.beginAtZero;
|
||||
}
|
||||
|
||||
getPixelForValue(value) {
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 14 KiB |
@ -478,7 +478,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
});
|
||||
|
||||
var y = chart.scales.y;
|
||||
expect(y.min).toBe(1);
|
||||
expect(y.min).toBe(0.1);
|
||||
expect(y.max).toBe(2);
|
||||
});
|
||||
|
||||
@ -508,7 +508,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
});
|
||||
|
||||
var y = chart.scales.y;
|
||||
expect(y.min).toBe(1);
|
||||
expect(y.min).toBe(0.1);
|
||||
expect(y.max).toBe(2);
|
||||
});
|
||||
|
||||
@ -638,7 +638,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
type: 'line',
|
||||
data: {
|
||||
datasets: [{
|
||||
data: [10, 5, 1, 25, 0, 78]
|
||||
data: [10, 5, 1.1, 25, 0, 78]
|
||||
}],
|
||||
labels: []
|
||||
},
|
||||
@ -751,7 +751,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
min: 0
|
||||
}
|
||||
},
|
||||
firstTick: 1,
|
||||
firstTick: 0.1,
|
||||
describe: 'all stacks are defined and min: 0'
|
||||
},
|
||||
{
|
||||
@ -762,7 +762,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
min: 0
|
||||
}
|
||||
},
|
||||
firstTick: 1,
|
||||
firstTick: 0.1,
|
||||
describe: 'not stacks are defined and min: 0'
|
||||
},
|
||||
{
|
||||
@ -783,7 +783,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
min: 0
|
||||
}
|
||||
},
|
||||
firstTick: 1,
|
||||
firstTick: 0.1,
|
||||
describe: 'all stacks are defined and min: 0'
|
||||
},
|
||||
{
|
||||
@ -794,7 +794,7 @@ describe('Logarithmic Scale tests', function() {
|
||||
min: 0
|
||||
}
|
||||
},
|
||||
firstTick: 1,
|
||||
firstTick: 0.1,
|
||||
describe: 'not all stacks are defined and min: 0'
|
||||
},
|
||||
];
|
||||
@ -812,7 +812,8 @@ describe('Logarithmic Scale tests', function() {
|
||||
chartEnd = 'top';
|
||||
}
|
||||
scaleConfig[setup.axis] = {
|
||||
type: 'logarithmic'
|
||||
type: 'logarithmic',
|
||||
beginAtZero: false
|
||||
};
|
||||
Object.assign(scaleConfig, setup.scale);
|
||||
scaleConfig[setup.axis].type = 'logarithmic';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user