Chartjs 隐藏数据集图例 v3

Kub*_*uba 8 javascript jquery chart.js chart.js3

我当前网页上的图表由 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,
        }
      }]
    }
  }
});         

            
Run Code Online (Sandbox Code Playgroud)

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

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

/库巴

Rav*_*mar 23

配置如下选项可以解决 V3 版本中的问题。

options: { plugins: { legend: { display: false }, } }
Run Code Online (Sandbox Code Playgroud)