如何添加 % 或 $ 值到 Chart.js 4 数据

问题描述 投票:0回答:1

我已经有了数据的百分比,我只需要将 % 附加到每个数据值即可。

我对 JS 和编程都很陌生,所以请多多关照。

const Votes1 = document.getElementById("Votes1");

new Chart(Votes1, {
  type: "bar",
  reverse: "true",
  data: {
    labels: [
      "African National Congress", "Other", "IFP", "National Party"
    ],
    datasets: [
      {
        label: "Voting results from 1994 South African election.",
        data: [62.25, 6.42, 10.54, 20.39],
        backgroundColor: ["#627b88ff"],
        borderWidth: 1,
      },
    ],
  },
  options: {
    indexAxis: "y",
    scales: {
      y: {
        beginAtZero: true,
        grid: {
          drawOnChartArea: false,
        },
      },
      x: {
        beginAtZero: true,
        grid: {
          drawOnChartArea: false,
        },
      },
    },
  },
});

我尝试使用 Youtube 视频寻求帮助,以及一些文档 - 但我很挣扎,因为这些是为了提取数据然后添加 %。我只需要能够向我的 Chart.js 数据添加特殊字符

如果重要的话,我正在使用快子框架

javascript chart.js tachyons-css
1个回答
0
投票

看我的示例,其中 % 添加到 y 轴。

    scales: {
    y: {
        beginAtZero: true,
        ticks: {
            callback: function (value, index, values) {
                return value + '%'; // Append '%' to the value
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.