Chartjs-Dynamically Setting Height of Chart

4👍

If the height is a mathematical function of the array length then you can specify it as such. For example use a linear relationship:

ctx.canvas.width = 100;
ctx.canvas.height = 1.727 * array_date.length + 20;

If we graph the old relationship vs. the linear we get this:

Graph that is linear

Not 100% what you had before, but it might be good enough. To get the steps you can use rounding. I played around with Excel and managed to get something like this with rounding (orange are your values):

Graph with rounding

3👍

Maybe try something like:

ctx.canvas.height = array_date.length * 2.5;

It won’t be 100% accurate but close enough I reckon. If that doesn’t help you could maybe tell us what type of charts are you using(libraries etc.) ?

Leave a comment