Chartjs 示例无法重现

Chartjs sample can't be reproduced

我希望从 Chartjs 文档中重现此示例的结果: https://www.chartjs.org/docs/latest/samples/scales/time-line.html

我要找的是 x 上的缩放结果:

我使用 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,
     }}
/>

我得到了以下结果(甚至离样本还差得很远)

谢谢你

您的问题出在您的选项中,因为您在 scale 键中定义了比例尺的选项,而必须在 scales 键中定义它们,所以从未应用比例配置,注意额外的 s