如何将 y 轴设置为 Chartjs 中的最大数据集值?

How to set y axis to the max dataset value in Chartjs?

这是一个非常快速的问题,但无论出于何种原因,我的图表将其最大 y 轴值设置为 100,即使数据在 50 左右结束。我不想为 y- 设置静态最大值轴,而是只让它停在最高数据点。

我是否缺少执行此操作的任何回调或选项?我的一些其他图表似乎按我想要的方式工作,但这个不是。

如有指点,将不胜感激!

编辑: 我看过一些使用 afterDataLimits 回调的帖子,但这似乎没有任何作用

我对此图表的选择是:

     responsive: isResponsive.value,
      maintainAspectRatio: false,
      plugins: {
        legend: {
          display: legendIsShown.value,
          position: 'top',
          labels: {
            font: {
              size: responsiveFontsize.value - 2,
              family: 'Inter',
              weight: 'semibold',
            },
            color: '#4771FA',
            pointStyle: 'circle',
            usePointStyle: true,
            boxWidth: 10,
            boxHeight: 10,
          },
        },
        tooltip: tooltipWhite,
        datalabels: {
          display: false,
        },
      },
      layout: {
        padding: {
          left: 10,
          right: 5,
          top: 5,
          bottom: 5,
        },
      },
      scales: {
        x: {
          title: {
            display: true,
            text: 'Time (Seconds)',
            color: '#4859AF',
            font: {
              size: responsiveFontsize.value + 2,
              weight: 'semibold',
            },
          },
          grid: {
            color: '#ecedf6',
            borderColor: '#4859AF',
            borderWidth: 0,
            display: true,
          },
          ticks: {
            color: '#4859AF',
            // maxTicksLimit: 0,
            font: {
              size: responsiveFontsize.value,
              family: 'Inter',
            },
          },
        },
        y: {
          // display: false
          title: {
            display: true,
            text: 'Current (Ampere)',
            color: '#4859AF',
            font: {
              size: responsiveFontsize.value + 2,
              weight: 'semibold',
            },
          },
          grid: {
            color: '#ecedf6',
            borderColor: '#4859AF',
            borderWidth: 0,
            display: true,
          },
          ticks: {
            color: '#4859AF',
            font: {
              size: responsiveFontsize.value,
              family: 'Inter',
            },
          },
        },
      },

您可以使用可编写脚本的函数来获得最大值 属性:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'orange'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'pink'
      }
    ]
  },
  options: {
    scales: {
      y: {
        max: (scale) => (
          scale.chart.data.datasets.reduce((acc, curr) => {
            const max = Math.max(...curr.data);
            acc = max > acc ? max : acc;
            return acc;
          }, Number.MIN_SAFE_INTEGER))
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
</body>