angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

Chart height getting bigger each time the browser gets resized

Open ejfrias opened this issue 11 years ago • 4 comments

Not sure if this is the same case as others so please forgive me for posting this again but, the issue I'm facing is when I try to switch my phone's orientation (which is like resizing a browser) the chart height gets bigger and bigger.

I needed the chart to be responsive but the height should be static to 300 px so I set:

responsive: true,
maintainAspectRatio: false

That alone does not work so I tried to use the approach below but it doesn't worked either:

<div>
    <canvas id="base" class="chart chart-base" chart-type="type" chart-data="data" chart-labels="labels" chart-series="series" chart-options="options" chart-legend="true" height="300"></canvas>
</div>

After a few hours of debugging, the solution I found is to add a css height to the .chart-container (this element is being added dynamically by angular-chart.js so adding a <div> as one of the solution above wont work either) and to move the <chart-legend> outside chart container so it doesn't float at the top of the elements below the chart if there's any or exceeds the browser window if none.

I guess having the <chart-legend> outside the chart container is the ideal html structure. I had to change the core angular-chart.js code for this solution which is quite troublesome if you have to update the library to the newer version because you have to update the core code again.

Anyways these are the steps I did:

Add the css to .chart-container

.chart-container {
    height: 300px;
}

Move the <chart-legend> outside, replace setLegend function with this

function setLegend (elem, chart) {
        var $parent = elem.parent(),
            $oldLegend = $parent.parent().find('chart-legend'),
            legend = document.createElement('chart-legend');
            legend.innerHTML = chart.generateLegend();

        if( $oldLegend.length != 0 ) $oldLegend.replaceWith(legend);
        else $parent[0].parentNode.insertBefore(legend, $parent[0].nextSibling);
    }

Hope this could help someone.

ejfrias avatar Oct 29 '15 10:10 ejfrias

+1 your fix helped me.

I am not rendering a legend, but setting height of .chart-container did the trick.

calebds avatar Dec 01 '15 04:12 calebds

A more general workaround is to wrap the canvas with a styled element:

<div class="my-chart">
    <canvas ...></canvas>
</div>

and to define

.chart-container {
    width: 100%;
    height: 100%;
}

.my-chart {
    width: 400px;
    height: 300px;
}

This is useful when you have several (different) charts on the page.

guidbona avatar Dec 22 '15 17:12 guidbona

Makes sense.. Cheers @guidbona!

ejfrias avatar Dec 23 '15 02:12 ejfrias

This issue is being closed as part of a general cleanup of stale issues. As we are currently modernizing the library to support Chart.js v4 and TypeScript, we are closing issues that refer to legacy versions or lack sufficient activity. If this is still relevant, please open a new issue.

jtblin avatar Apr 29 '26 10:04 jtblin