mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Time: limit offset to sane values, gen >= 2 ticks (#8560)
This commit is contained in:
parent
118cff7cfc
commit
08b76b0efd
@ -1,6 +1,6 @@
|
||||
import adapters from '../core/core.adapters';
|
||||
import {isFinite, isNullOrUndef, mergeIf, valueOrDefault} from '../helpers/helpers.core';
|
||||
import {toRadians, isNumber} from '../helpers/helpers.math';
|
||||
import {toRadians, isNumber, _limitValue} from '../helpers/helpers.math';
|
||||
import Scale from '../core/core.scale';
|
||||
import {_arrayUnique, _filterBetween, _lookup} from '../helpers/helpers.collection';
|
||||
|
||||
@ -379,6 +379,8 @@ export default class TimeScale extends Scale {
|
||||
end = (last - me.getDecimalForValue(timestamps[timestamps.length - 2])) / 2;
|
||||
}
|
||||
}
|
||||
start = _limitValue(start, 0, 0.25);
|
||||
end = _limitValue(end, 0, 0.25);
|
||||
|
||||
me._offsets = {start, end, factor: 1 / (start + 1 + end)};
|
||||
}
|
||||
@ -404,7 +406,7 @@ export default class TimeScale extends Scale {
|
||||
const hasWeekday = isNumber(weekday) || weekday === true;
|
||||
const ticks = {};
|
||||
let first = min;
|
||||
let time;
|
||||
let time, count;
|
||||
|
||||
// For 'week' unit, handle the first day of week option
|
||||
if (hasWeekday) {
|
||||
@ -420,11 +422,11 @@ export default class TimeScale extends Scale {
|
||||
}
|
||||
|
||||
const timestamps = options.ticks.source === 'data' && me.getDataTimestamps();
|
||||
for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) {
|
||||
for (time = first, count = 0; time < max; time = +adapter.add(time, stepSize, minor), count++) {
|
||||
addTick(ticks, time, timestamps);
|
||||
}
|
||||
|
||||
if (time === max || options.bounds === 'ticks') {
|
||||
if (time === max || options.bounds === 'ticks' || count === 1) {
|
||||
addTick(ticks, time, timestamps);
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
81
test/fixtures/scale.time/offset-with-no-ticks.js
vendored
Normal file
81
test/fixtures/scale.time/offset-with-no-ticks.js
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
const data = {
|
||||
datasets: [
|
||||
{
|
||||
data: [
|
||||
{
|
||||
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 55
|
||||
},
|
||||
{
|
||||
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 10
|
||||
},
|
||||
{
|
||||
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 15
|
||||
}
|
||||
],
|
||||
backgroundColor: 'blue'
|
||||
},
|
||||
{
|
||||
data: [
|
||||
{
|
||||
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 6
|
||||
},
|
||||
{
|
||||
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 11
|
||||
},
|
||||
{
|
||||
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 16
|
||||
}
|
||||
],
|
||||
backgroundColor: 'green',
|
||||
},
|
||||
{
|
||||
data: [
|
||||
{
|
||||
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 7
|
||||
},
|
||||
{
|
||||
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 12
|
||||
},
|
||||
{
|
||||
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
|
||||
y: 17
|
||||
}
|
||||
],
|
||||
backgroundColor: 'red',
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
description: 'https://github.com/chartjs/Chart.js/issues/7991',
|
||||
config: {
|
||||
type: 'bar',
|
||||
data,
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
type: 'time',
|
||||
// offset: false,
|
||||
time: {
|
||||
unit: 'month',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
options: {
|
||||
spriteText: true,
|
||||
canvas: {width: 256, height: 128}
|
||||
}
|
||||
};
|
||||
BIN
test/fixtures/scale.time/offset-with-no-ticks.png
vendored
Normal file
BIN
test/fixtures/scale.time/offset-with-no-ticks.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Loading…
x
Reference in New Issue
Block a user