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.

Clear canvas after drawing a chart (AJAX Request)

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
asadnaumancommented, Jun 13, 2015

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

4reactions
rohandalvicommented, Nov 16, 2015

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

Read more comments on GitHub >

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

github_iconTop Related Medium Post

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