angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

Hovering over line chart shows old chart data

Open martellaj opened this issue 11 years ago • 65 comments

This is my scenario (using a line chart):

  1. Make HTTP request to get chart data from API.
  2. Add chart data to chart by assigning to a bound variable called chartData.
  3. User clicks a button to fetch different data, so another HTTP request is made to get new data.
  4. I clear the chartData array and then add the new data.

Expected outcome: Chart no longer has any trace of original data, but instead shows new data and user can interact with it without issues.

Actual outcome: Chart shows new data, but if user hovers over the chart, it'll flicker back and forth between original and new data.

olddata

martellaj avatar Aug 25 '15 16:08 martellaj

How do you clear the data? Passing an empty array does not clear the data, it's a chart.js issue. Otherwise can you post a repro case, see jsbin template in readme? It may be an issue with chart.js itself btw.

jtblin avatar Aug 26 '15 10:08 jtblin

So it looks like it's not a Chart.js "issue", but you just have to clear data using .clear() or .destroy(). Do you have a suggestion on how to do this with the Angular directive? According to README, this should be handled automatically. I'll work on setting up a repo for you.

martellaj avatar Aug 26 '15 15:08 martellaj

I have the same problem, I need to draw different data sets in the same chart and the old data keeps keeps showing. Is there a way to call .clear or .destroy on the chart?

jbmartinez avatar Aug 26 '15 18:08 jbmartinez

Found a way:

$scope.$on('create', function (event, chart) {
  ctrl.chart = chart; // ctrl is a controller reference
});

Then I could use ctrl.chart.destroy(). Surprisingly, that didn't solve the problem. I had to disable the tooltips as workaround. I would like to enable the tooltips, though.

jbmartinez avatar Aug 26 '15 18:08 jbmartinez

any news regarding this?

andre-gois avatar Sep 15 '15 16:09 andre-gois

I was having the same issue. At first I tried to reproduce it using the jsbin template but there everything I tried worked. So I tried changing the values in my app the same way I was doing in the example and it also worked (basically onClick changed the values in the chart).

In the end what fixed for me was this:

In my app when a date range filter value changed I would set the values for labels and data in a service like this:

vm.labels = newLabels;
vm.data = newData;

when I changed it to this the bug stopped appearing:

$timeout(function() {
    vm.labels = newLabels;
    vm.data = newData;
});

Hope this helps others.

pedrocatre avatar Sep 15 '15 18:09 pedrocatre

@pedrocatre , that works, thanks for the workaround!

thomasduchatelle avatar Sep 18 '15 14:09 thomasduchatelle

Pie chart has the same problem. But this problem only occurs if the new data has less items than the old data.

And the workaround does not work for me. So I have disabled the tooltips.

yaojingguo avatar Oct 04 '15 08:10 yaojingguo

The workaround did not work for me when I have multiple instances of charts being displayed. I read through some discussion on the Chart.js forum and they mention this problem occurring when there are multiple instances of the chart pointing to the same canvas. You can read the discussion here: https://github.com/nnnick/Chart.js/issues/920

I looked through the angular-charts source code and I see a number of places where the chart is being destroyed and recreated in other spots and some of them involving timeouts when the element is hidden. I'm wondering if there is a loose reference somewhere that is being retained in a closure that is causing this behavior when a hover event is occurring. I have to get back to my work items but I hope this initial investigation can lead to some solutions.

snielson-dyn avatar Oct 08 '15 22:10 snielson-dyn

The work around does not work for Line chart. some data aren't destroyed

yayiekim avatar Oct 31 '15 18:10 yayiekim

I think I finally found where is bug. For me it was duplicated initialization of chart. On page load I do ajax request and get data but also I do the same on datapicker change (it was triggered on page load too). After I remove one of them everything seams to work fine.

psmarcin avatar Dec 23 '15 12:12 psmarcin

The $timeout workaround didn't work for me. However, what worked is the following code inside my chart directive:

var $chart;
$scope.$on("create", function (event, chart) {
  if (typeof $chart !== "undefined") {
    $chart.destroy();
  }

  $chart = chart;
});

There are more than one chart instances... I just destroy every instance except the last one.

tobiastheil avatar Jan 07 '16 14:01 tobiastheil

@tobiastheil Thanks it worked for me.

sandy240287 avatar Feb 03 '16 08:02 sandy240287

@tobiastheil Thanks so much, sweet workaround.

kr4ng avatar Feb 03 '16 18:02 kr4ng

Very smart @tobiastheil, I'll try and include that in the lib directly :+1:

jtblin avatar Feb 03 '16 21:02 jtblin

Bugfix is inbound, should have have a PR by the weekend.

gkampjes avatar Feb 17 '16 21:02 gkampjes

:+1:

jtblin avatar Feb 18 '16 07:02 jtblin

+1

kdomagal avatar Feb 18 '16 11:02 kdomagal

+1

netmonster01 avatar Feb 19 '16 17:02 netmonster01

I was facing the same issue, refreshing the page using $window.location.reload() after rendering new data stopped this flickering bug in my case. Check if it helps anyone.

shubhampatil17 avatar Feb 21 '16 08:02 shubhampatil17