Obtain max value of y axis of line chart rendered with Chart.js
Asked Answered
O

2

11

I use Chart.js to render a scattered line chart, which works pretty well. For the rendering algorithm I need to find out the highest value shown on the y-axis, so let's say my "largest" point in the dataset has y = 248, so the y-axis shows 250 as the largest value. I need to find out that it's 250.

I tried to inspect the chart object at runtime, like so:

lineChart.options.scales[0].ticks.??

but it seems that I can only find out the settings I set myself programmatically.

Also looking at the comprehensive Chart.js docs did not point me to the solution.

Any advice how to figure out this value?

Outdated answered 18/12, 2016 at 17:14 Comment(0)
C
17

There is callback method in which you can get the array of values which will show on yAxes.
The first element of that array will be the highest value for the yAxes. Below is the sample code for the same.

var yAxesticks = [];
var highestVal;
var chartInstanceHoverModeNearest = new Chart(ctx, {
                type: 'bar',
                data: data,
                options:{
                    scales: {
                        yAxes : [{
                            ticks : {
                                beginAtZero : true,
                                callback : function(value,index,values){
                                    yAxesticks = values;
                                    return value;
                                }
                            }
                        }]
                    }
                }
            });

 highestVal = yAxesticks[0];
Chablis answered 19/12, 2016 at 3:16 Comment(0)
L
0

With Chart.js 4.3.0, the min and max can be obtained directly from chart.scales[scaleId]:

let yMax = chartInstance.scales.y.max;
let yMin = chartInstance.scales.y.min;
Lowrance answered 16/7, 2023 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.