Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Clear canvas after drawing a chart (AJAX Request)

Open RobMue opened this issue 12 years ago • 15 comments

Currently I am trying to draw a new chart after a POST request, which transfers the JSON to the "canvas" element. Unfortunately the graph starts jumping around and gets "fuzzy" between the grid lines. This happens every time when the JSON data has changing datasets - which means, that there are different amounts of grid lines zu draw.

So far I have tried

ctx.clearRect(0, 0, canvas.width, canvas.height);

and

myLineChart = new Chart(ctx).Line(jsonData, {
    responsive: true,
    bezierCurveTension : 0.3
});
window.myLine = myLineChart;
//... chart is drawn

// before drawing a new chart
myLine.clear();

Both without success. Do you have any tipps/hints to destroy the canvas before redrawing on it?

RobMue avatar Aug 20 '14 14:08 RobMue

You can destroy the whole chart instance by calling .destroy on the instance.

Should sort your issues there?

nnnick avatar Aug 20 '14 16:08 nnnick

Awesome. :+1: Thank you!

RobMue avatar Aug 20 '14 18:08 RobMue

how and where can i call the destroy instance?

kiga01 avatar Aug 20 '14 20:08 kiga01

As per the example above, you'd call myLine.destroy()

nnnick avatar Aug 20 '14 21:08 nnnick

Awesome. :+1: +1 Thanks!

hezhijun avatar Dec 16 '14 03:12 hezhijun

How would one access any chart instances, say from outside of the invocation scope of the chart?

I can target any canvas elements, but I can't see any reference to the chart instance

waniel avatar Feb 18 '15 11:02 waniel

Hi Waniel,

create a global variable and use that variable while creating chart. e-g

var mychart; function drawChart() { var ctx = document.getElementById("age-wise-chart-line").getContext("2d"); mychart= new Chart(ctx).Line(lineData, lineOptions); }

Now you can use mychart.clear() or mychart.destroy()

Regards, Asad

asadnauman avatar Jun 13 '15 10:06 asadnauman

What if I have multiple bar charts, I cannot use a single global variable for all.

rohandalvi avatar Nov 16 '15 16:11 rohandalvi

i am using chart.js with ionic, when i show and hide the graph the graph shows for the first time but when i hide it and shows again the graph disappears.... Please guide me !

lobobobo avatar Jan 05 '16 12:01 lobobobo

Thanks ! Chart flickered when I update with ajax call. Now its fine !

thethakuri avatar Jan 13 '16 03:01 thethakuri

I have the same problem. I can't access myLineChart variable because it is created inside separate method. But I just want to clear canvas and for me it doesn't matter what kind of chart was drawn. I can't belive that there is no any suitable method in chart.js just to reset canvas. Is there?

ekatherina avatar Mar 28 '16 11:03 ekatherina

create a global variable and use that variable while creating chart. e-g

var mychart; function drawChart() { var ctx = document.getElementById("age-wise-chart-line").getContext("2d"); mychart= new Chart(ctx).Line(lineData, lineOptions); } Now you can use mychart.clear() or mychart.destroy()

I've done that but I get an error: TypeError: myNewChart.destroy is not a function myNewChart.destroy();

I can do ?. Thank you

lproenzav avatar May 09 '16 19:05 lproenzav

declare a global hash and save each chart object in that hash by the div identifier. Much simpler and scalable.

rohandalvi avatar May 10 '16 00:05 rohandalvi

if (typeof myNewChart != 'undefined') {
     myNewChart.destroy();
}

made the example above work for me.

raknjak avatar May 09 '17 18:05 raknjak

lineChart1: any;  //declare 
plotGraph(data:any, label:any .....){
        if(this.lineChart1){
               console.log("destroying",this.lineChart1);
               this.lineChart1.destroy();
         }

         this.lineChart1 = new Chart(this.lineCanvas.nativeElement, {
                // plotting graph
         });
}

Adding the if condition will remove all previous data in graph and will plot the graph with new values

bhavi289 avatar Nov 21 '17 16:11 bhavi289