如何在 chart.js 中禁用图例

huangapple go评论59阅读模式
英文:

How to disable the legend in chart.js

问题

我遇到了 chartjs 的语法问题。我想做三件事:

  1. 将 responsive 设置为 false。
  2. 将 Y 轴刻度设置为最大值:9。
  3. 隐藏图例(legend)显示。

我已经成功设置了坐标轴并将 responsive 设为 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>

希望这能帮助你实现所需的效果!

英文:

I'm struggling with the syntax of chartjs. I want to do three things:

  1. Make responsive:false
  2. Set the Y axis scale to max: 9
  3. Make the legend display: false

I have managed to set the axis and make responsive: false. However, I can't remove the legend. If I play around with code, I can remove the legend again, however, this will disable responsive: false and the y axis scale. How can I make it so that all three of these things are achieved in the graph? Thanks for any help you can provide!

Here is my current code:

 &lt;script&gt;
    const ctx = document.getElementById(&#39;myChart&#39;);
  
    new Chart(ctx, {
      type: &#39;bar&#39;,
      data: {
        labels: [&#39;Listening&#39;, &#39;Reading&#39;, &#39;Writing&#39;, &#39;Speaking&#39;, &#39;Overall&#39;],
        datasets: [{
          {% for result in regular_test_1 %}
          label: &#39;Band Score&#39;,
          data: [{{ result[&quot;listening&quot;] }}, {{ result[&quot;reading&quot;] }}, {{ result[&quot;writing&quot;] }}, {{ result[&quot;speaking&quot;] }}, {{ result[&quot;overall&quot;] }}],
          {% endfor %}
          backgroundColor: [
                &#39;rgba(255, 99, 132, 0.2)&#39;,
                &#39;rgba(54, 162, 235, 0.2)&#39;,
                &#39;rgba(255, 206, 86, 0.2)&#39;,
                &#39;rgba(75, 192, 192, 0.2)&#39;,
                &#39;rgba(153, 102, 255, 0.2)&#39;
        ],
          borderWidth: 1
        }]
      },
      options: {
        responsive: false,
        scales: {
          y: {
            max: 9,
            min: 1,
            beginAtZero: true,
        plugins: {
          legend: {
            display: false
              }
            }
          }
        }
      }
    });
  &lt;/script&gt;  

答案1

得分: 1

这是您提供的代码的翻译:

在您的代码中有一个错误,位于y{}的大括号中,这就是为什么它不起作用的原因。这是更新后的代码,请尝试:

<script>
  const ctx = document.getElementById('myChart');

  new Chart(ctx, {
    type: 'bar',
    data: {
      labels: ['听力', '阅读', '写作', '口语', '总体'],
      datasets: [{
        {% for result in regular_test_1 %}
        label: '分数',
        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>

希望这对您有所帮助。

英文:

There is error in your code in brackets at y{}, that's why its not working. Here is the updated code. Try this:-

 &lt;script&gt;
    const ctx = document.getElementById(&#39;myChart&#39;);
  
    new Chart(ctx, {
      type: &#39;bar&#39;,
      data: {
        labels: [&#39;Listening&#39;, &#39;Reading&#39;, &#39;Writing&#39;, &#39;Speaking&#39;, &#39;Overall&#39;],
        datasets: [{
          {% for result in regular_test_1 %}
          label: &#39;Band Score&#39;,
          data: [{{ result[&quot;listening&quot;] }}, {{ result[&quot;reading&quot;] }}, {{ result[&quot;writing&quot;] }}, {{ result[&quot;speaking&quot;] }}, {{ result[&quot;overall&quot;] }}],
          {% endfor %}
          backgroundColor: [
                &#39;rgba(255, 99, 132, 0.2)&#39;,
                &#39;rgba(54, 162, 235, 0.2)&#39;,
                &#39;rgba(255, 206, 86, 0.2)&#39;,
                &#39;rgba(75, 192, 192, 0.2)&#39;,
                &#39;rgba(153, 102, 255, 0.2)&#39;
        ],
          borderWidth: 1
        }]
      },
      options: {
        responsive: false,
        scales: {
          y: {
            max: 9,
            min: 1,
            beginAtZero: true,
          }
        },
        plugins: {
          legend: {
            display: false
          }
        }
      }
    });
  &lt;/script&gt;  

huangapple
  • 本文由 发表于 2023年5月10日 12:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定