Skip to content

Commit

Permalink
Fix time series scale to have each data point is spread equidistant (#…
Browse files Browse the repository at this point in the history
…11388)

* Fix time series scale to have each data point is spread equidistant

* remove tabs

* remove casting and add/update test cases
  • Loading branch information
stockiNail committed Jul 13, 2023
1 parent c392a7c commit 05608b0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/scales/scale.timeseries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down
46 changes: 46 additions & 0 deletions test/fixtures/scale.timeseries/data-timestamps.js
Original file line number Diff line number Diff line change
@@ -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
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions test/fixtures/scale.timeseries/financial-daily.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified test/fixtures/scale.timeseries/financial-daily.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/scale.timeseries/source-auto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/scale.timeseries/source-data-offset-min-max.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 05608b0

Please sign in to comment.