Clear canvas after drawing a chart (AJAX Request)
See original GitHub issueCurrently 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?
Issue Analytics
- State:
- Created 9 years ago
- Comments:15 (2 by maintainers)
Top Results From Across the Web
How to clear a chart from a canvas so that hover events ...
This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js.".
Read more >Chart JS canvas dynamic draw chart and clear using AJAX ...
Chart.js : How to Clear canvas after drawing a chart based on the dynamic data received from AJAX based JSON response data ?...
Read more >API | Chart.js
# .clear(). Will clear the chart canvas. Used extensively internally between animation frames, but you might find it useful. // Will clear the ......
Read more >Tutorial on Creating Charts using JavaScript & HTML
An interactive tutorial on creating HTML5 JavaScript Charts using CanvasJS. Build your first chart in 5 minutes!
Read more >Miscellaneous information
To help when debugging your RGraph charts and canvas elements, you can use ... data sources that may be classed as strings, such...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
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
What if I have multiple bar charts, I cannot use a single global variable for all.