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
This commit is contained in:
stockiNail
2023-07-13 22:36:08 +02:00
committed by GitHub
parent c392a7cc8c
commit 05608b0ceb
8 changed files with 70 additions and 4 deletions

View File

@@ -445,7 +445,7 @@ export default class TimeScale extends Scale {
* `minor` unit using the given scale time `options`.
* Important: this method can return ticks outside the min and max range, it's the
* responsibility of the calling code to clamp values if needed.
* @private
* @protected
*/
_generate() {
const adapter = this._adapter;
@@ -485,7 +485,7 @@ export default class TimeScale extends Scale {
}
// @ts-ignore
return Object.keys(ticks).sort((a, b) => a - b).map(x => +x);
return Object.keys(ticks).sort(sorter).map(x => +x);
}
/**

View File

@@ -110,6 +110,25 @@ class TimeSeriesScale extends TimeScale {
return table;
}
/**
* Generates all timestamps defined in the data.
* Important: this method can return ticks outside the min and max range, it's the
* responsibility of the calling code to clamp values if needed.
* @protected
*/
_generate() {
const min = this.min;
const max = this.max;
let timestamps = super.getDataTimestamps();
if (!timestamps.includes(min) || !timestamps.length) {
timestamps.splice(0, 0, min);
}
if (!timestamps.includes(max) || timestamps.length === 1) {
timestamps.push(max);
}
return timestamps.sort((a, b) => a - b);
}
/**
* Returns all timestamps
* @return {number[]}

View File

@@ -0,0 +1,46 @@
module.exports = {
threshold: 0.01,
tolerance: 0.0015,
config: {
type: 'line',
data: {
datasets: [{data: [
{x: 1687849697000, y: 904},
{x: 1687817063000, y: 905},
{x: 1687694268000, y: 913},
{x: 1687609438000, y: 914},
{x: 1687561387000, y: 916},
{x: 1686875127000, y: 918},
{x: 1686873138000, y: 920},
{x: 1686872777000, y: 928},
{x: 1686081641000, y: 915}
], fill: false}, {data: [
{x: 1687816803000, y: 1105},
{x: 1686869490000, y: 1114},
{x: 1686869397000, y: 1103},
{x: 1686869225000, y: 1091},
{x: 1686556516000, y: 1078}
]}]
},
options: {
scales: {
x: {
type: 'timeseries',
bounds: 'data',
time: {
unit: 'day'
},
ticks: {
source: 'auto'
}
},
y: {
display: false
}
}
}
},
options: {
spriteText: true
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -33,12 +33,13 @@ module.exports = {
autoSkip: true,
autoSkipPadding: 75,
maxRotation: 0,
sampleSize: 100
sampleSize: 100,
maxTicksLimit: 3
},
// manually set major ticks so that test passes in all time zones with moment adapter
afterBuildTicks: function(scale) {
const major = [0, 12, 24];
const ticks = scale.ticks;
const major = [0, 264, 522];
for (let i = 0; i < ticks.length; i++) {
ticks[i].major = major.indexOf(i) >= 0;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB