From: stockiNail Date: Thu, 13 Jul 2023 20:36:08 +0000 (+0200) Subject: Fix time series scale to have each data point is spread equidistant (#11388) X-Git-Tag: v4.3.1~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=05608b0ceb13b49aec0fcefa4915c1e498fcb9ee;p=thirdparty%2FChart.js.git 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 --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 03a9714b0..6bcef6c03 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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); } /** diff --git a/src/scales/scale.timeseries.js b/src/scales/scale.timeseries.js index f347cd341..2be734934 100644 --- a/src/scales/scale.timeseries.js +++ b/src/scales/scale.timeseries.js @@ -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[]} diff --git a/test/fixtures/scale.timeseries/data-timestamps.js b/test/fixtures/scale.timeseries/data-timestamps.js new file mode 100644 index 000000000..b1df5c0ef --- /dev/null +++ b/test/fixtures/scale.timeseries/data-timestamps.js @@ -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 + } +}; diff --git a/test/fixtures/scale.timeseries/data-timestamps.png b/test/fixtures/scale.timeseries/data-timestamps.png new file mode 100644 index 000000000..31d9a3f47 Binary files /dev/null and b/test/fixtures/scale.timeseries/data-timestamps.png differ diff --git a/test/fixtures/scale.timeseries/financial-daily.js b/test/fixtures/scale.timeseries/financial-daily.js index 9f13f02d1..d529be4ff 100644 --- a/test/fixtures/scale.timeseries/financial-daily.js +++ b/test/fixtures/scale.timeseries/financial-daily.js @@ -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; } diff --git a/test/fixtures/scale.timeseries/financial-daily.png b/test/fixtures/scale.timeseries/financial-daily.png index 659c0a1da..1537512dd 100644 Binary files a/test/fixtures/scale.timeseries/financial-daily.png and b/test/fixtures/scale.timeseries/financial-daily.png differ diff --git a/test/fixtures/scale.timeseries/source-auto.png b/test/fixtures/scale.timeseries/source-auto.png index 532509c39..aa6255b6c 100644 Binary files a/test/fixtures/scale.timeseries/source-auto.png and b/test/fixtures/scale.timeseries/source-auto.png differ diff --git a/test/fixtures/scale.timeseries/source-data-offset-min-max.png b/test/fixtures/scale.timeseries/source-data-offset-min-max.png index 824bf36bd..fe7165cfe 100644 Binary files a/test/fixtures/scale.timeseries/source-data-offset-min-max.png and b/test/fixtures/scale.timeseries/source-data-offset-min-max.png differ