Rui Alves Rui Alves - 8 months ago 57
Javascript Question

how to make a div match a dynamically created google chart height

I have a page that dynamically loads a google timeline chart onto a div. The timeline increases in height as you add items on to it. If the chart is smaller than the div, it will be visible with empty space at the bottom. If the chart is larger, it will show scrollbars on the side.

What I would like is for the container div to always show the full chart or, another way, I would like the container div height to dynamically match the height of the chart.

I have tried to approximate the average size of each chart line and then adjust the div height when I load the chart with:


But, although that somehow works, it's far from perfect because the approximation on height starts to accumulate with each additional line and the empty space at the bottom of the div increases which is not an elegant solution at all.

How would I ensure that the container div always shows the full chart?

I hope this is clear enough.

Many thanks.


The chart determines its height by either checking the height option passed into the draw call, or by checking the container height if the height option is not specified. I would suggest using the option instead of changing the div height via jQuery. I find the best method for dynamically calculating the chart height is to take the height of a row (typically 41px) times the number of rows, plus some padding for the top and bottom:

var height = data.getNumberOfRows() * 41 + 30;