在scales.x中添加displayFormats之后,现在我的xAx标签根据它包含的数据对象有不同的间隙.如何使我的xAx标签以相等的间距显示? 以下代码如下图所示. 请记住,我仍然需要使用displayFormats来格式化我的数据.

enter image description here

  const levelHistoryOptions = {
    responsive: true,
    maintainAspectRatio: false,
    layout: {
      padding: {
        top: 20,
        bottom: 20,
      },
    },
    plugins: {
      legend: {
        display: false,
      },
      datalabels: {
        color: theme.palette.primary.main,
        anchor: "end",
        align: "top",
        font: {
          size: 16, // Adjust the font size as needed
        },
        formatter: (value, context) => {
          return context.chart.data.datasets[context.datasetIndex].data[
            context.dataIndex
          ];
        },
      },
    },
    scales: {
      x: {
        type: "time",
        time: {
          unit: "day",
          displayFormats: {
            day: "MM월 dd일",
          },
        },

        offset: true,
        ticks: {
          source: "data",
          font: {
            size: 16,
          },
          color: theme.palette.primary.main,
        },
        grid: {
          color: theme.palette.mode === "dark" ? "#555" : "#ccc",
        },
        border: { color: theme.palette.mode === "dark" ? "#555" : "#ccc" },
      },
      y: {
        offset: true,
        display: false,
        beginAtZero: false,
      },
    },
  };

推荐答案

您应该将x轴定义为type: 'timeseries',如下所示:

options: {
  scales: {
    x: {
      type: 'timeseries',
      ...

欲了解更多信息,请查看Time Series AxisChart.js文档中.

Javascript相关问答推荐

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

当在select中 Select 选项时,我必须禁用不匹配的 Select

Redux查询多个数据库Api reducerPath&

如何使用JavaScript将文本插入空div

如何为我的astro页面中的相同组件自动创建不同的内容?

当作为表达式调用时,如何解析方法decorator 的签名?

你怎么看啦啦队的回应?

Html文件和客户端存储的相关问题,有没有可能?

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

为什么123[';toString';].long返回1?

在Matter.js中添加从点A到另一个约束的约束

面对代码中的错误作为前端与后端的集成

将基元传递给THEN处理程序

如何在FiRestore中的事务中使用getCountFromServer

我为什么要使用回调而不是等待?

未找到用于 Select 器的元素:in( puppeteer 师错误)

在单击按钮时生成多个表单时的处理状态

JSX/React -如何在组件props 中循环遍历数组

如果我将高度设置为其内容的100%,则在Java脚本中拖动以调整面板大小时会冻结

JavaScript structuredClone在Chrome/Edge中获得了非法调用,但在NodeJS中没有