Initializing Chart.js multiple times over same canvas changes its size for some devices
See original GitHub issue //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale.
if (window.devicePixelRatio) {
context.canvas.style.width = width + "px";
context.canvas.style.height = height + "px";
context.canvas.height = height * window.devicePixelRatio;
context.canvas.width = width * window.devicePixelRatio;
context.scale(window.devicePixelRatio, window.devicePixelRatio);
}
When devicePixelRation != 1
doing
ctx = canvas.getContext("2d")
new Chart(ctx).drawSomething
//then later drawing another chart on the same canvas with
new Chart(ctx).drawSomething
//
//then later another one
new Chart(ctx).drawSomething
keeps changing (getting smaller or bigger depending on devicePixelRatio) the chart size.
This can be easily reproduced if you zoom-in or zoom-out in firefox, refresh (verify that window.devicePixelRatio
is not 1) and keep doing the mentioned procedure (creting new charts on the same canvas). The charts are getting smaller or bigger with every Chart initialization.
My workaround for now is to reset width/height again to initial values before creating chart again:
ctx = canvas.getContext("2d")
new Chart(ctx).drawSomething
//then later drawing another chart on the same canvas with
canvas.width = 400
canvas.height = 400
ctx = canvas.getContext("2d")
new Chart(ctx).drawSomething
Issue Analytics
- State:
- Created 9 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
Charts.js graph not scaling to canvas size - Stack Overflow
This will create a chart with a fixed size, with the same size on every screen, which in mho is not what you...
Read more >How to Keep the Chart Size Consistent While Changing ...
The canvas tag, javascript, arrays and Chart JS all need to be ... And with some clever tricks and visual adjustment more can...
Read more >Responsive Charts - Chart.js
Chart.js provides a few options to enable responsiveness and control the resize behavior of charts by detecting when the canvas display size ......
Read more >Guide to Creating Charts in JavaScript With Chart.js
In this guide, we will look at how to construct charts in JavaScript using Chart.js, as well as the numerous customization and styles...
Read more >Getting Started - Chart JS Video Guide
The final block that should always be last is the render or initialization block. This block draws the chart in the canvas based...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had the same distressing hover effect using one canvas for changing charts. For me the following was the solution. I found it @ http://stackoverflow.com/questions/24815851/how-do-clear-a-chart-from-a-canvas-so-that-hover-events-cannot-be-triggered - Answer from xchiltonx (modified):
I tried something with
typeof myChart != 'undefined'
- but it didnt work out. I hope it will work for you aswell.You can use update() for v2.x