如何禁用 chart.js 中的图例

问题描述 投票:0回答:1

我正在为 chartjs 的语法而苦苦挣扎。我想做三件事:

  1. 作出回应:假
  2. 将 Y 轴刻度设置为最大值:9
  3. 使图例显示:false

我已经设法设置轴并做出响应:false。但是,我无法删除图例。如果我玩代码,我可以再次删除图例,但是,这将禁用 responsive: false 和 y 轴刻度。我怎样才能使所有这三件事都在图中实现?感谢您提供的任何帮助!

这是我当前的代码:

 <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,
            min: 1,
            beginAtZero: true,
        plugins: {
          legend: {
            display: false
              }
            }
          }
        }
      }
    });
  </script>  
javascript chart.js
1个回答
0
投票

error in your code in brackets at y{}
,这就是它不起作用的原因。这是更新的代码。试试这个:-

 <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,
            min: 1,
            beginAtZero: true,
          }
        },
        plugins: {
          legend: {
            display: false
          }
        }
      }
    });
  </script>  
© www.soinside.com 2019 - 2024. All rights reserved.