Skip to content

Commit 08b76b0

Browse files
authored
Time: limit offset to sane values, gen >= 2 ticks (#8560)
1 parent 118cff7 commit 08b76b0

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

src/scales/scale.time.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import adapters from '../core/core.adapters';
22
import {isFinite, isNullOrUndef, mergeIf, valueOrDefault} from '../helpers/helpers.core';
3-
import {toRadians, isNumber} from '../helpers/helpers.math';
3+
import {toRadians, isNumber, _limitValue} from '../helpers/helpers.math';
44
import Scale from '../core/core.scale';
55
import {_arrayUnique, _filterBetween, _lookup} from '../helpers/helpers.collection';
66

@@ -379,6 +379,8 @@ export default class TimeScale extends Scale {
379379
end = (last - me.getDecimalForValue(timestamps[timestamps.length - 2])) / 2;
380380
}
381381
}
382+
start = _limitValue(start, 0, 0.25);
383+
end = _limitValue(end, 0, 0.25);
382384

383385
me._offsets = {start, end, factor: 1 / (start + 1 + end)};
384386
}
@@ -404,7 +406,7 @@ export default class TimeScale extends Scale {
404406
const hasWeekday = isNumber(weekday) || weekday === true;
405407
const ticks = {};
406408
let first = min;
407-
let time;
409+
let time, count;
408410

409411
// For 'week' unit, handle the first day of week option
410412
if (hasWeekday) {
@@ -420,11 +422,11 @@ export default class TimeScale extends Scale {
420422
}
421423

422424
const timestamps = options.ticks.source === 'data' && me.getDataTimestamps();
423-
for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) {
425+
for (time = first, count = 0; time < max; time = +adapter.add(time, stepSize, minor), count++) {
424426
addTick(ticks, time, timestamps);
425427
}
426428

427-
if (time === max || options.bounds === 'ticks') {
429+
if (time === max || options.bounds === 'ticks' || count === 1) {
428430
addTick(ticks, time, timestamps);
429431
}
430432

-22 Bytes
Loading
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const data = {
2+
datasets: [
3+
{
4+
data: [
5+
{
6+
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
7+
y: 55
8+
},
9+
{
10+
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
11+
y: 10
12+
},
13+
{
14+
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
15+
y: 15
16+
}
17+
],
18+
backgroundColor: 'blue'
19+
},
20+
{
21+
data: [
22+
{
23+
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
24+
y: 6
25+
},
26+
{
27+
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
28+
y: 11
29+
},
30+
{
31+
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
32+
y: 16
33+
}
34+
],
35+
backgroundColor: 'green',
36+
},
37+
{
38+
data: [
39+
{
40+
x: moment('15/10/2020', 'DD/MM/YYYY').valueOf(),
41+
y: 7
42+
},
43+
{
44+
x: moment('18/10/2020', 'DD/MM/YYYY').valueOf(),
45+
y: 12
46+
},
47+
{
48+
x: moment('19/10/2020', 'DD/MM/YYYY').valueOf(),
49+
y: 17
50+
}
51+
],
52+
backgroundColor: 'red',
53+
}
54+
]
55+
};
56+
57+
module.exports = {
58+
description: 'https://github.com/chartjs/Chart.js/issues/7991',
59+
config: {
60+
type: 'bar',
61+
data,
62+
options: {
63+
scales: {
64+
x: {
65+
type: 'time',
66+
// offset: false,
67+
time: {
68+
unit: 'month',
69+
},
70+
},
71+
y: {
72+
display: false
73+
}
74+
}
75+
}
76+
},
77+
options: {
78+
spriteText: true,
79+
canvas: {width: 256, height: 128}
80+
}
81+
};
1.47 KB
Loading

0 commit comments

Comments
 (0)