我希望从 Chartjs 文档中重现此示例的结果:https://www.chartjs.org/docs/latest/samples/scales/time-line.html https://www.chartjs.org/docs/latest/samples/scales/time-line.html
What I am looking for is the scale result on x :
我将 React 与react-chartjs-2 和示例的简化版本一起使用,但我设法为 x 轴保留相同的配置和数据:
import { DateTime } from 'luxon';
import { Line } from 'react-chartjs-2';
import { Chart as ChartJS, registerables } from 'chart.js';
import 'chartjs-adapter-luxon';
ChartJS.register(...registerables);
function newDate(days: number) {
return DateTime.now().plus({ days }).toJSDate();
}
function newDateString(days: number) {
return DateTime.now().plus({ days }).toISO();
}
const options = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
},
scale: {
x: {
type: 'time',
time: {
tooltipFormat: 'DD T',
},
},
y: {
type: 'linear',
suggestedMin: 0,
suggestedMax: 100,
ticks: {
stepSize: 20,
},
},
},
};
const labels = [newDate(0), newDate(1), newDate(2), newDate(3), newDate(4), newDate(5), newDate(6)];
const Data = [
{
x: newDateString(0),
y: 41,
},
{
x: newDateString(5),
y: 21,
},
{
x: newDateString(7),
y: 12,
},
{
x: newDateString(15),
y: 38,
},
];
/*...*/
<Line
options={options}
data={{
datasets: [{ data: data }],
labels: labels,
}}
/>
I got the following result (not even close from sample)
感谢您