3

我当前在网页上的图表由 Chart.js v2.5.3 提供支持。不久前配置的图表工作得很好。这是代码:

var ctw = document.getElementById("chart-budget");
var myChart = new Chart(ctw, {
  type: 'bar',
  data: {
    labels: ["budget", "costs"],
    datasets: [{
      label: "Amount",
      data: [520980, 23397.81],
      backgroundColor: [
        'rgba(143, 211, 91, 0.2)',
        'rgba(255, 99, 132, 0.2)',
      ],
      borderColor: [
        'rgba(143, 211, 91,1)',
        'rgba(255, 99, 132, 1)',
      ],
      borderWidth: 1
    }]
  },
  options: {
    legend: {
      display: false
    },
    responsive: true,
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        },
        gridLines: {
          drawBorder: false,
        }
      }],
      xAxes: [{
        gridLines: {
          display: false,
        }
      }]
    }
  }
});         

            

由于 v3 现在可用,我正在考虑切换到它。快速测试揭示了一些我需要克服的问题。我正在努力解决的问题之一是无法隐藏数据集图例。在 V2.5 中,它是通过options.legend.display设置为false来完成的,但不再是。在文档中,我没有遇到任何可以帮助我的东西。

有没有人可以建议如何在 Chart.js V3 中隐藏图例?

/库巴

4

1 回答 1

5

像下面这样配置选项将解决 V3 版本中的问题。

options: { plugins: { legend: { display: false }, } }
于 2021-10-26T10:13:49.357 回答