[Chartjs]-Chart.js automatic scaling

1👍

This may not be the cleanest way but you can play with it as you see fit.

In short, you need to create a function that finds the max value out of all your dataSets arrays and in yAxes.ticks set max to that functions return value.

More details below

Referencing here first:

data:{
    labels: labels.data,
    datasets: dataSets //talking about this first
}

Create a function that will go through these arrays and get the max of all arrays. I did it using this but you can do it better I’m sure:

function getMax(){
    var max = 0;
    dataSets.forEach(function(x, i){        
        max = Math.max(max, Math.max.apply(null, x.data));
    });

    return max + 2;
}

Then in your return in yAxes you should be able to have:

ticks: {
    beginAtZero: false,
    max: getMax()
},

1👍

You can find the min and max of your dataset, then add/subtract a percentage of the difference (range).

For example, say you use 1% of the range.

[0.1, 0.2, 0.5] gives (1%)x(0.5-0.1)=0.004, so [min,max]=[0.096,0.504].

[1500, 1800, 3500, 3600] gives (1%)x(3600-1500)=21, so [min,max]=[1479,3621].

Leave a comment