Skip to content

Commit 05608b0

Browse files
authored
Fix time series scale to have each data point is spread equidistant (#11388)
* Fix time series scale to have each data point is spread equidistant * remove tabs * remove casting and add/update test cases
1 parent c392a7c commit 05608b0

File tree

8 files changed

+70
-4
lines changed

8 files changed

+70
-4
lines changed

src/scales/scale.time.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export default class TimeScale extends Scale {
445445
* `minor` unit using the given scale time `options`.
446446
* Important: this method can return ticks outside the min and max range, it's the
447447
* responsibility of the calling code to clamp values if needed.
448-
* @private
448+
* @protected
449449
*/
450450
_generate() {
451451
const adapter = this._adapter;
@@ -485,7 +485,7 @@ export default class TimeScale extends Scale {
485485
}
486486

487487
// @ts-ignore
488-
return Object.keys(ticks).sort((a, b) => a - b).map(x => +x);
488+
return Object.keys(ticks).sort(sorter).map(x => +x);
489489
}
490490

491491
/**

src/scales/scale.timeseries.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ class TimeSeriesScale extends TimeScale {
110110
return table;
111111
}
112112

113+
/**
114+
* Generates all timestamps defined in the data.
115+
* Important: this method can return ticks outside the min and max range, it's the
116+
* responsibility of the calling code to clamp values if needed.
117+
* @protected
118+
*/
119+
_generate() {
120+
const min = this.min;
121+
const max = this.max;
122+
let timestamps = super.getDataTimestamps();
123+
if (!timestamps.includes(min) || !timestamps.length) {
124+
timestamps.splice(0, 0, min);
125+
}
126+
if (!timestamps.includes(max) || timestamps.length === 1) {
127+
timestamps.push(max);
128+
}
129+
return timestamps.sort((a, b) => a - b);
130+
}
131+
113132
/**
114133
* Returns all timestamps
115134
* @return {number[]}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
threshold: 0.01,
3+
tolerance: 0.0015,
4+
config: {
5+
type: 'line',
6+
data: {
7+
datasets: [{data: [
8+
{x: 1687849697000, y: 904},
9+
{x: 1687817063000, y: 905},
10+
{x: 1687694268000, y: 913},
11+
{x: 1687609438000, y: 914},
12+
{x: 1687561387000, y: 916},
13+
{x: 1686875127000, y: 918},
14+
{x: 1686873138000, y: 920},
15+
{x: 1686872777000, y: 928},
16+
{x: 1686081641000, y: 915}
17+
], fill: false}, {data: [
18+
{x: 1687816803000, y: 1105},
19+
{x: 1686869490000, y: 1114},
20+
{x: 1686869397000, y: 1103},
21+
{x: 1686869225000, y: 1091},
22+
{x: 1686556516000, y: 1078}
23+
]}]
24+
},
25+
options: {
26+
scales: {
27+
x: {
28+
type: 'timeseries',
29+
bounds: 'data',
30+
time: {
31+
unit: 'day'
32+
},
33+
ticks: {
34+
source: 'auto'
35+
}
36+
},
37+
y: {
38+
display: false
39+
}
40+
}
41+
}
42+
},
43+
options: {
44+
spriteText: true
45+
}
46+
};
36 KB
Loading

test/fixtures/scale.timeseries/financial-daily.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
3.55 KB
Loading
-1.69 KB
Loading
114 Bytes
Loading

0 commit comments

Comments
 (0)