mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Fix autoskip logic (#10663)
* fix autoskip logic * add test * fix lint erro * Update variable name
This commit is contained in:
parent
1c9e536de2
commit
cc65c2bac2
@ -28,6 +28,7 @@ A number of changes were made to the configuration options passed to the `Chart`
|
||||
* If the tooltip callback returns `undefined`, then the default callback will be used.
|
||||
* `maintainAspectRatio` respects container height.
|
||||
* Time and timeseries scales use `ticks.stepSize` instead of `time.stepSize`, which has been removed.
|
||||
* `maxTickslimit` wont be used for the ticks in `autoSkip` if the determined max ticks is less then the `maxTicksLimit`.
|
||||
|
||||
#### Type changes
|
||||
* The order of the `ChartMeta` parameters have been changed from `<Element, DatasetElement, Type>` to `<Type, Element, DatasetElement>`.
|
||||
|
||||
@ -16,7 +16,8 @@ import {_factorize} from '../helpers/helpers.math';
|
||||
*/
|
||||
export function autoSkip(scale, ticks) {
|
||||
const tickOpts = scale.options.ticks;
|
||||
const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale);
|
||||
const determinedMaxTicks = determineMaxTicks(scale);
|
||||
const ticksLimit = Math.min(tickOpts.maxTicksLimit || determinedMaxTicks, determinedMaxTicks);
|
||||
const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
|
||||
const numMajorIndices = majorIndices.length;
|
||||
const first = majorIndices[0];
|
||||
|
||||
@ -34,12 +34,44 @@ describe('Core.scale', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function getChartBigData(maxTicksLimit) {
|
||||
return window.acquireChart({
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: new Array(300).fill('red'),
|
||||
datasets: [{
|
||||
data: new Array(300).fill(5),
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
ticks: {
|
||||
autoSkip: true,
|
||||
maxTicksLimit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function lastTick(chart) {
|
||||
var xAxis = chart.scales.x;
|
||||
var ticks = xAxis.getTicks();
|
||||
return ticks[ticks.length - 1];
|
||||
}
|
||||
|
||||
it('should use autoSkip amount of ticks when maxTicksLimit is set to a larger number as autoSkip calculation', function() {
|
||||
var chart = getChartBigData(300);
|
||||
expect(chart.scales.x.ticks.length).toEqual(20);
|
||||
});
|
||||
|
||||
it('should use maxTicksLimit amount of ticks when maxTicksLimit is set to a smaller number as autoSkip calculation', function() {
|
||||
var chart = getChartBigData(3);
|
||||
expect(chart.scales.x.ticks.length).toEqual(3);
|
||||
});
|
||||
|
||||
it('should display the last tick if it fits evenly with other ticks', function() {
|
||||
var chart = getChart({
|
||||
labels: [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user