【问题标题】:Set maximum bar height in chart.js在 chart.js 中设置最大条形高度
【发布时间】:2022-10-12 22:31:21
【问题描述】:

我使用下面的代码在角度使用chart.js,

  const myChart = new Chart('myChart', {
          type: 'bar',
          data: {
              labels: ['Mathematics', 'English', 'Physics', 'Chemistry' ],
              datasets: [{
                  label: 'Score',
                  data: [this.mathematics_score,this.english_score,this.physics_score,this.chemistry_score],
                  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)',
                    'rgba(255, 159, 64, 0.2)'
                  ],
                  borderColor: [
                    'rgba(255, 99, 132, 1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                  ],
                  borderWidth: 1,
                 
              }]
          },
          options: {
            indexAxis: 'x',
            responsive: true,
              scales: {
                  x: {
                      beginAtZero: true,
                      min: 0,
                    max: 90
                  }
    
              }
          }
      });

这里的科目分数是 50 分,但是在图表中的高度是根据科目的最高分来设置的是40,但我想要50。

任何解决方案,谢谢

【问题讨论】:

    标签: javascript charts chart.js


    【解决方案1】:

    您尝试在 x 轴上设置最大值,而您需要在 y 轴上设置它。如果你这样做,它会按预期工作:

    options: {
      scales: {
        y: {
          max: 50,
        }
      }
    }
    

    【讨论】: