如何在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
相关产品推荐
相关产品推荐

