Chart.js配置问题:如何同时禁用图例、关闭响应式及设置Y轴最大值
解决Chart.js三项配置同时生效的问题
你的问题出在配置结构嵌套错误:把plugins(图例配置)放到了scales.y的内部,导致整个options的JSON结构语法错误,进而引发其他配置失效。
只需调整配置层级,将plugins与scales、responsive同级放置,即可同时实现三个需求:
修正后的完整代码:
<script> const ctx = document.getElementById('myChart'); new Chart(ctx, { type: 'bar', data: { labels: ['Listening', 'Reading', 'Writing', 'Speaking', 'Overall'], datasets: [{ {% for result in regular_test_1 %} label: 'Band Score', data: [{{ result["listening"] }}, {{ result["reading"] }}, {{ result["writing"] }}, {{ result["speaking"] }}, {{ result["overall"] }}], {% endfor %} backgroundColor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)' ], borderWidth: 1 }] }, options: { responsive: false, // 关闭响应式 scales: { y: { max: 9, // Y轴最大值设为9 min: 1, beginAtZero: true } }, plugins: { // 禁用图例,与scales同级 legend: { display: false } } } }); </script>
关键调整说明:
- 将
plugins配置从scales.y的内部移出,放到options的根层级,和scales、responsive平级 - 确保每个配置块的括号闭合正确,避免语法错误
内容的提问来源于stack exchange,提问作者BritishJonny888
相关产品推荐
相关产品推荐

