在x轴上显示所有标签在图表中。

发布于 2025-02-08 11:03:47 字数 970 浏览 0 评论 0原文

我是Chart.js的新手,我一直在寻找一种在图表中显示所有标签的方法,无论标签是否有数据,我都尝试了最大和最小值,但它不起作用,所以我遇到了壁虱回调:

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周

I am new to Chart.js , I have been looking for a way to display all labels in chart.js regardless of whether there is a data for the label or not, I have tried the max and min but it does not work so I came across the ticks callback:

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);

but it didnt work.
is there a way to all display a fixed label like always show 12 month, 31 days, 12 weeks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2025-02-15 11:03:47

您可以定义data.labels,并确保data.datasets.data具有相同数量的条目,null对于缺失值。

let config = {
  type: "bar",
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "october", "November", "December"],
    datasets: [{
      label: "cost of goods sold",
      data: [0, 20, 40, 50, null, null, null, null, null, null, null, null]
    }]
  }
};

new Chart('chart', config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<canvas id="chart" height="80"></canvas>

You can define data.labels and make sure that data.datasets.data has the same number of entries, null for missing values.

let config = {
  type: "bar",
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "october", "November", "December"],
    datasets: [{
      label: "cost of goods sold",
      data: [0, 20, 40, 50, null, null, null, null, null, null, null, null]
    }]
  }
};

new Chart('chart', config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<canvas id="chart" height="80"></canvas>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文