mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-06 08:24:05 +01:00
Reduce code duplication and sort generated ticks (#7747)
* Reduce code duplication and sort generated ticks * Add test
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
if (time === max || options.bounds === 'ticks') {
|
||||
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);
|
||||
}
|
||||
|
||||
return Object.keys(ticks).map(x => +x);
|
||||
if (time === max || options.bounds === 'ticks') {
|
||||
addTick(ticks, time, timestamps);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
return Object.keys(ticks).sort((a, b) => a - b).map(x => +x);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
35
test/fixtures/scale.time/negative-times.js
vendored
Normal file
35
test/fixtures/scale.time/negative-times.js
vendored
Normal file
@@ -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}
|
||||
}
|
||||
};
|
||||
BIN
test/fixtures/scale.time/negative-times.png
vendored
Normal file
BIN
test/fixtures/scale.time/negative-times.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Reference in New Issue
Block a user