Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beginAtZero support for logarithmic #7862

Merged
merged 1 commit into from Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/scales/scale.logarithmic.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
Binary file modified test/fixtures/controller.bar/stacking/logarithmic-strings.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/stacking/logarithmic.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 9 additions & 8 deletions test/specs/scale.logarithmic.tests.js
Expand Up @@ -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);
});

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

Expand Down Expand Up @@ -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: []
},
Expand Down Expand Up @@ -751,7 +751,7 @@ describe('Logarithmic Scale tests', function() {
min: 0
}
},
firstTick: 1,
firstTick: 0.1,
describe: 'all stacks are defined and min: 0'
},
{
Expand All @@ -762,7 +762,7 @@ describe('Logarithmic Scale tests', function() {
min: 0
}
},
firstTick: 1,
firstTick: 0.1,
describe: 'not stacks are defined and min: 0'
},
{
Expand All @@ -783,7 +783,7 @@ describe('Logarithmic Scale tests', function() {
min: 0
}
},
firstTick: 1,
firstTick: 0.1,
describe: 'all stacks are defined and min: 0'
},
{
Expand All @@ -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'
},
];
Expand All @@ -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';
Expand Down