Skip to content

Commit 64593ed

Browse files
authored
Time: Fix offset with low data counts (#8740)
1 parent 0b21578 commit 64593ed

File tree

7 files changed

+152
-4
lines changed

7 files changed

+152
-4
lines changed

src/scales/scale.time.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export default class TimeScale extends Scale {
291291
max = isFinite(max) && !isNaN(max) ? max : +adapter.endOf(Date.now(), unit) + 1;
292292

293293
// Make sure that max is strictly higher than min (required by the timeseries lookup table)
294-
me.min = Math.min(min, max);
294+
me.min = Math.min(min, max - 1);
295295
me.max = Math.max(min + 1, max);
296296
}
297297

@@ -376,8 +376,9 @@ export default class TimeScale extends Scale {
376376
end = (last - me.getDecimalForValue(timestamps[timestamps.length - 2])) / 2;
377377
}
378378
}
379-
start = _limitValue(start, 0, 0.25);
380-
end = _limitValue(end, 0, 0.25);
379+
const limit = timestamps.length < 3 ? 0.5 : 0.25;
380+
start = _limitValue(start, 0, limit);
381+
end = _limitValue(end, 0, limit);
381382

382383
me._offsets = {start, end, factor: 1 / (start + 1 + end)};
383384
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const data = {
2+
datasets: [
3+
{
4+
label: 6,
5+
backgroundColor: 'red',
6+
data: [
7+
{
8+
x: '2021-03-24',
9+
y: 464
10+
}
11+
]
12+
},
13+
{
14+
label: 1,
15+
backgroundColor: 'red',
16+
data: [
17+
{
18+
x: '2021-03-24',
19+
y: 464
20+
}
21+
]
22+
},
23+
{
24+
label: 17,
25+
backgroundColor: 'blue',
26+
data: [
27+
{
28+
x: '2021-03-24',
29+
y: 390
30+
}
31+
]
32+
}
33+
]
34+
};
35+
36+
module.exports = {
37+
description: 'https://github.com/chartjs/Chart.js/issues/8718',
38+
config: {
39+
type: 'bar',
40+
data,
41+
options: {
42+
scales: {
43+
x: {
44+
type: 'time',
45+
time: {
46+
unit: 'day',
47+
},
48+
},
49+
y: {
50+
display: false
51+
}
52+
}
53+
}
54+
},
55+
options: {
56+
spriteText: true,
57+
canvas: {width: 256, height: 128}
58+
}
59+
};
2.34 KB
Loading
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const data = {
2+
datasets: [
3+
{
4+
label: 1,
5+
backgroundColor: 'orange',
6+
data: [
7+
{
8+
x: '2021-03-24',
9+
y: 464
10+
}
11+
]
12+
},
13+
{
14+
label: 2,
15+
backgroundColor: 'red',
16+
data: [
17+
{
18+
x: '2021-03-24',
19+
y: 464
20+
}
21+
]
22+
},
23+
{
24+
label: 3,
25+
backgroundColor: 'blue',
26+
data: [
27+
{
28+
x: '2021-03-24',
29+
y: 390
30+
}
31+
]
32+
},
33+
{
34+
label: 4,
35+
backgroundColor: 'purple',
36+
data: [
37+
{
38+
x: '2021-03-25',
39+
y: 464
40+
}
41+
]
42+
},
43+
{
44+
label: 5,
45+
backgroundColor: 'black',
46+
data: [
47+
{
48+
x: '2021-03-25',
49+
y: 464
50+
}
51+
]
52+
},
53+
{
54+
label: 6,
55+
backgroundColor: 'cyan',
56+
data: [
57+
{
58+
x: '2021-03-25',
59+
y: 390
60+
}
61+
]
62+
}
63+
]
64+
};
65+
66+
module.exports = {
67+
description: 'https://github.com/chartjs/Chart.js/issues/8718',
68+
config: {
69+
type: 'bar',
70+
data,
71+
options: {
72+
scales: {
73+
x: {
74+
type: 'time',
75+
time: {
76+
unit: 'day',
77+
},
78+
},
79+
y: {
80+
display: false
81+
}
82+
}
83+
}
84+
},
85+
options: {
86+
spriteText: true,
87+
canvas: {width: 256, height: 128}
88+
}
89+
};
2.62 KB
Loading

test/fixtures/scale.time/offset-with-no-ticks.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module.exports = {
6363
scales: {
6464
x: {
6565
type: 'time',
66-
// offset: false,
6766
time: {
6867
unit: 'month',
6968
},
-55 Bytes
Loading

0 commit comments

Comments
 (0)