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

This is a really quick question, but for whatever reason my chart is setting it's max y axis value to 100 even though the data ends around 50. I don't want to set a static max value for the y-axis, but rather only have it stop at the highest data point.

enter image description here

Is there any callback or option that I am missing to do this? Some of my other charts seem to work the way I want, but this one does not.

Any pointers would be greatly appreciated!

Edit: I've seen a few posts using the afterDataLimits callback, but this doesn't seem to do anything How to add afterDataLimits callback to chart.js

My options for this chart are:

     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',
            },
          },
        },
      },


Solution 1:[1]

You can use a scriptable function for the max property:

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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LeeLenalee