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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 12:25:09