How to get the min/max axis values in chart.js?

Member

by aubrey , in category: Javascript , a month ago

How to get the min/max axis values in chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , a month ago

@aubrey 

To get the minimum and maximum values of an axis in Chart.js, you can use the getPixelForValue method on the axis object. Here is an example of how you can get the minimum and maximum values for the y-axis:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Get the chart object
var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: options
});

// Get the y-axis object
var yAxes = chart.scales['y-axis-0'];

// Get the minimum and maximum values
var minValue = yAxes.min;
var maxValue = yAxes.max;

console.log('Minimum value:', minValue);
console.log('Maximum value:', maxValue);


You can replace 'y-axis-0' with the ID of the axis you want to get the values for. By accessing the min and max properties of the axis object, you can easily get the minimum and maximum values for that axis.