admin管理员组文章数量:1287887
I have the following code leveraging the ChartJS library.
/*assume the tags in the right place */
<canvas id="graph1" width="300" height="300"></canvas>
var ctx = $("#graph1").get(0).getContext("2d");
var myChart = new Chart(ctx).Line(graph1Generator("day"));
... everything works fine, but after adding the following event handler to clear and repaint the same chart with different data, a glitch occurs.
weekButton.addEventListener("click", function(){
ctx.clearRect (0, 0, 300, 300);
ctx.canvas.width = 300;
ctx.canvas.height = 300;
myChart = new Chart(ctx).Line(graph1Generator("week"));
This code does successfully redraw the chart with the new data, but when I hover over it, it does some very strange "flashbacks" to the old chart that it was supposed to clear. This makes me believe that it didn't clear the old one.
I have the following code leveraging the ChartJS library.
/*assume the tags in the right place */
<canvas id="graph1" width="300" height="300"></canvas>
var ctx = $("#graph1").get(0).getContext("2d");
var myChart = new Chart(ctx).Line(graph1Generator("day"));
... everything works fine, but after adding the following event handler to clear and repaint the same chart with different data, a glitch occurs.
weekButton.addEventListener("click", function(){
ctx.clearRect (0, 0, 300, 300);
ctx.canvas.width = 300;
ctx.canvas.height = 300;
myChart = new Chart(ctx).Line(graph1Generator("week"));
This code does successfully redraw the chart with the new data, but when I hover over it, it does some very strange "flashbacks" to the old chart that it was supposed to clear. This makes me believe that it didn't clear the old one.
Share Improve this question asked May 5, 2015 at 16:26 ApathyBearApathyBear 9,61515 gold badges59 silver badges90 bronze badges 9-
I think it'll work if you just re-use the old chart object instead of creating a new one in the event handler. You can also call
myChart.destroy();
right before you create a new one, as an alternative. – Pointy Commented May 5, 2015 at 16:39 -
How would I reuse the old chart object with new data? While the charts are the same, they have different types of data. Also,
myChar.destroy();
seems to be duplicating my chart size after every event. Why is that happening? – ApathyBear Commented May 5, 2015 at 16:47 -
I think just
myChart.Line(graph1generator("week"));
(or whatever you need to do for the new data). And instead of clearing the canvas with your code, let Chart do it:myChart.clear();
. (That might work better than.destroy()
; I'm not sure.) – Pointy Commented May 5, 2015 at 16:49 -
While myChart.clear() doesn't duplicate the size, it still doesn't solve the glitch. Also, when change the object like
myChart.Line(graph1generator("week"));
, I get aReferenceError: Can't find variable: graph1generator
. That is strange since its a function defined 1 scope above (global scope). – ApathyBear Commented May 5, 2015 at 17:01 - Without seeing more code it's hard to know what's going on. Have you tried duplicating the issue in jsfiddle or CodePen or something? – Pointy Commented May 5, 2015 at 17:05
1 Answer
Reset to default 11Here is an update to your fiddle. The primary change (other than fixing the function name typo) is to add
myChart.destroy();
before lines like
myChart = new Chart(ctx).Line(...);
The .destroy()
method gets rid of the event handler registrations etc, so you shouldn't see those weird "ghost charts" when you mouse over the graphics.
本文标签: javascriptChartJs line chart repaint glitch while hovering overStack Overflow
版权声明:本文标题:javascript - ChartJs line chart repaint glitch while hovering over - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329938a2372710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论