diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 03a9714b047..6bcef6c03e6 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 f347cd341c1..2be73493407 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 00000000000..b1df5c0ef30 --- /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 00000000000..31d9a3f4717 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 9f13f02d195..d529be4ff44 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 659c0a1dae2..1537512dd84 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 532509c394b..aa6255b6c83 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 824bf36bd51..fe7165cfef2 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