From: Jukka Kurkela Date: Wed, 2 Sep 2020 12:09:39 +0000 (+0300) Subject: Reduce code duplication and sort generated ticks (#7747) X-Git-Tag: v3.0.0-beta.2~1^2~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=effe988bbf491512a16e76bb1233b12e8aaf3c30;p=thirdparty%2FChart.js.git Reduce code duplication and sort generated ticks (#7747) * Reduce code duplication and sort generated ticks * Add test --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 99f5b9412..2df061883 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -135,17 +135,18 @@ function determineMajorUnit(unit) { } /** - * @param {number[]} timestamps * @param {object} ticks * @param {number} time + * @param {number[]} [timestamps] - if defined, snap to these timestamps */ -function addTick(timestamps, ticks, time) { - if (!timestamps.length) { - return; +function addTick(ticks, time, timestamps) { + if (!timestamps) { + ticks[time] = true; + } else if (timestamps.length) { + const {lo, hi} = _lookup(timestamps, time); + const timestamp = timestamps[lo] >= time ? timestamps[lo] : timestamps[hi]; + ticks[timestamp] = true; } - const {lo, hi} = _lookup(timestamps, time); - const timestamp = timestamps[lo] >= time ? timestamps[lo] : timestamps[hi]; - ticks[timestamp] = true; } /** @@ -416,28 +417,17 @@ export default class TimeScale extends Scale { throw new Error(min + ' and ' + max + ' are too far apart with stepSize of ' + stepSize + ' ' + minor); } - if (me.options.ticks.source === 'data') { - // need to make sure ticks are in data in this case - const timestamps = me.getDataTimestamps(); - - for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) { - addTick(timestamps, ticks, time); - } - - if (time === max || options.bounds === 'ticks') { - addTick(timestamps, ticks, time); - } - } else { - for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) { - ticks[time] = true; - } + const timestamps = options.ticks.source === 'data' && me.getDataTimestamps(); + for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) { + addTick(ticks, time, timestamps); + } - if (time === max || options.bounds === 'ticks') { - ticks[time] = true; - } + if (time === max || options.bounds === 'ticks') { + addTick(ticks, time, timestamps); } - return Object.keys(ticks).map(x => +x); + // @ts-ignore + return Object.keys(ticks).sort((a, b) => a - b).map(x => +x); } /** diff --git a/test/fixtures/scale.time/negative-times.js b/test/fixtures/scale.time/negative-times.js new file mode 100644 index 000000000..a1863336d --- /dev/null +++ b/test/fixtures/scale.time/negative-times.js @@ -0,0 +1,35 @@ +module.exports = { + config: { + type: 'line', + data: { + datasets: [{ + data: [ + {x: -1000000, y: 1}, + {x: 1000000000, y: 2} + ] + }] + }, + options: { + scales: { + x: { + type: 'time', + time: { + unit: 'day' + }, + ticks: { + display: false + } + }, + y: { + ticks: { + display: false + } + } + }, + legend: false + } + }, + options: { + canvas: {width: 1000, height: 200} + } +}; diff --git a/test/fixtures/scale.time/negative-times.png b/test/fixtures/scale.time/negative-times.png new file mode 100644 index 000000000..b7453937b Binary files /dev/null and b/test/fixtures/scale.time/negative-times.png differ