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.

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:closed
  • Created 9 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
greldinardcommented, Nov 10, 2015

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):

// initialising variable for charts
var myChart = null;
//setting options
var options = {
    datasetFill : false,
    };

// funciton executed on clicking button "10"
function twenty() {
$.getJSON( "ltwen", function(data) {
var my_data = data;
var ctx = document.getElementById("myChart").getContext("2d");
// --> this is the key to solution - no hover effects of old charts anymore
if(myChart != null){
        myChart.destroy();
    }
myChart = new Chart(ctx).Line(my_data, options);
});
}

// funciton executed on pressing button "20"
function ten() {
$.getJSON( "lten", function(data) {
var my_data = data;
var ctx = document.getElementById("myChart").getContext("2d");
// --> this is the key to solution - no hover effects of old charts anymore
if(myChart != null){
        myChart.destroy();
    }
myChart = new Chart(ctx).Line(my_data, options);
});
}

I tried something with typeof myChart != 'undefined' - but it didnt work out. I hope it will work for you aswell.

3reactions
mtvbriankingcommented, May 28, 2018

You can use update() for v2.x

var chart_ctx = document.getElementById("chart").getContext("2d");

var chart = new Chart(chart_ctx, {
	type: "pie",
	data: {}
	options: {}
}

$.ajax({
	...
}).done(function (response) {
    chart.data = response;
    chart.update();
});
Read more comments on GitHub >

github_iconTop 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 >

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