question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart is not rendered when maintainAspectRatio is set to false and chart container is hidden

See original GitHub issue

Hi,

First of all, thanks for creating this package as vue wrapper for chartjs. I have a use case where my chart is hidden initially then when the user will click the button, the chart will show.

Before I added the option maintainAspectRatio: false, everything works fine. The chart will be rendered even it is being rendered in a hidden div. But once I added the option mentioned above, only charts that are visible is rendered. The hidden charts are not rendered.

Expected Behavior

Chart should still render in a hidden div with maintainAspectRatio: false

Actual Behavior

Chart is not rendered in a hidden div with maintainAspectRatio: false

Environment

  • vue.js version: ^2.1.10
  • vue-chart.js version: ^3.0.0
  • npm version: 5.3.0

UPDATE

I found out that the bug when the container is hidden is the canvas height is 0. It seems that the height props was not properly propagated if container is hidden upon rendering.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
desaikalpesh34commented, Jan 3, 2020

Just Defining parent height it will work like charm.

<div class="renderChart" style="height:350px;">
   <canvas id="pieChart"> </canvas>
</div>

Now show/hide renderChart will work.

3reactions
aperturelesscommented, Jan 8, 2018

Well thats a common thing with chart.js. I guess the tabs are working with display: none and if the component gets mounted the parent div has a height and width of 0. Because of the display: none .

Thats why you can’t see anything. Chart.js is grabbing the height and width and applying it to the canvas. You can try to pass a fix height or remove the responsive: true.

Because responsive: true will get the parent element width and height and apply it to the canvas. So it grows and shrinks while resizing. So responsive: true is causing the real issue.

maintainAspectRatio: true however fixes this because

Maintain the original canvas aspect ratio (width / height) when resizing.

So I guess setting the tab to display: block triggers the resize listener and recalculating the canvas size.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Responsive Charts - Chart.js
Chart.js uses its parent container to update the canvas render and display ... the maintainAspectRatio option must also be set to false ....
Read more >
How to Resize the Chart and maintainAspectRatio in Chart JS
How to Resize the Chart and maintainAspectRatio in Chart JSThe resizing of the chart in Chart JS is luckily an easy to grasp...
Read more >
chart.js - Canvas static height Chartjs - Stack Overflow
I'm having a problem with this. I set the height of the parent element, I added the responsive: true, maintainAspectRatio: false.
Read more >
Chartjs responsive and maintainAspectRatio not working
The first chart don't have width and height setted it is being rendered by the parent container, but the second chart has a...
Read more >
JavaScript: chart.js 2.0 - Claris Community
Mike, when doing charts on the web, I frequently have a way to 'save' the chart (as jpg likely) and then it was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found