在Chart.js中显示X轴上的所有标签,无论是否有数据

gab6jxml  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(188)

我是Chart.js的新手,我一直在寻找一种方法来显示chart.js中的所有标签,无论是否有标签的数据,我已经尝试了最大值和最小值,但它不工作,所以我遇到了ticks回调:

let ctx = document.getElementById("chart").getContext("2d");
let config = {
  type: "bar",
  data: {
    datasets: [
      {
        label: "cost of goods sold",
        data: [0, 20, 40, 50],
      },
    ],
    //labels: ["January", "February", "March", "April"],
  },
  options: {
    scales: {
      x: {
        ticks: {
          callback: (value, index, ticks) => {
            const x = [
              "January",
              "February",
              "March",
              "April",
              "May",
              "June",
              "July",
              "August",
              "September",
              "october",
              "November",
              "December",
            ];
            return x;
          },
        },
      },
    },
  },
};

let chart = new Chart(ctx, config);

有没有办法让所有人都显示一个固定标签,比如总是显示12个月,31天,12周?

mqkwyuun

mqkwyuun1#

您可以定义data.labels,并确保data.datasets.data具有相同的条目数,null表示缺少的值。
第一个

相关问题