Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear canvas after drawing a chart (AJAX Request) #559

Closed
RobMue opened this issue Aug 20, 2014 · 15 comments
Closed

Clear canvas after drawing a chart (AJAX Request) #559

RobMue opened this issue Aug 20, 2014 · 15 comments

Comments

@RobMue
Copy link

RobMue commented Aug 20, 2014

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?

@nnnick
Copy link
Member

nnnick commented Aug 20, 2014

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

Should sort your issues there?

@RobMue
Copy link
Author

RobMue commented Aug 20, 2014

Awesome. 👍
Thank you!

@RobMue RobMue closed this as completed Aug 20, 2014
@kiga01
Copy link

kiga01 commented Aug 20, 2014

how and where can i call the destroy instance?

@nnnick
Copy link
Member

nnnick commented Aug 20, 2014

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

@hezhijun
Copy link

Awesome. 👍 +1
Thanks!

@waniel
Copy link

waniel commented Feb 18, 2015

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

@asadnauman
Copy link

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

@rohandalvi
Copy link

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

@lobobobo
Copy link

lobobobo commented Jan 5, 2016

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 !

@thethakuri
Copy link

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

@ekatherina
Copy link

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?

@lproenzav
Copy link

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

@rohandalvi
Copy link

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

@raknjak
Copy link

raknjak commented May 9, 2017

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

made the example above work for me.

@bhavi289
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests