You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Chart.js中移除图表图例?初次使用遇问题求助

解决Chart.js无法隐藏图例的问题

你代码里有两个问题导致图例关不掉:

  • 配置层级错误:Chart.js v3及以后版本中,图例的配置必须放在options.plugins.legend下,你之前直接写在options.legend里是不生效的。
  • 冗余无效配置:data对象里的legend: 'options'属于无效配置,完全没用,需要删掉。

修正后的完整代码:

const ctx1 = document.getElementById('yearWise').getContext('2d');
const yearWise = new Chart(ctx1, {
  type: 'bar',
  plugins: [ChartDataLabels],

  data: {
    labels: ['2018-19','2019-20','2020-21','2021-22'],
    datasets: [{
      data: [90,400,5245,5122],
      backgroundColor: [
        'rgba(70, 132, 238, 0.5)',
      ],
      borderColor: [
        'rgba(70, 132, 238, 1)',
      ],
      borderWidth: 1
    }]
  },
  options: {
    responsive: true,
    scales: {
      y: {
          beginAtZero: true,
      }
    },
    plugins: {
      title: {
        display: true,
        text: 'Programs Conducted - Year Wise',
      },
      legend: {
        display: false,
      },
      datalabels: {
          anchor: 'end',
          align: 'end',
          labels: {
            value: {
              color: 'rgba(70, 132, 238, 1)'
            }
          }
      },          
    },
  }
});

内容的提问来源于stack exchange,提问作者Sanjeev shinde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:06:02