-
Notifications
You must be signed in to change notification settings - Fork 572
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
Calling update to refresh the chart. #291
Comments
Well, in my case i just change the values of the data and it updates by itself, however idk if it works the same for those components. |
i try to push values to my dataset and label arrays, but no refresh is triggered.
|
@michaelknoch I am having the same problem, however I can't seem to implement your workaround for it to work for me. Did you set the _chart.ngOnChanges() within some interval? |
in my workaround, _chart.ngOnChanges() is called after data has changed. Think of $scope.$apply from angular 1. @jrmoser |
@michaelknoch's workaround worked for me when adding data to the chart. ...going to test this on the chart point colors later |
@michaelknoch Thanks, I ended up putting it inside of an ngDoCheck... probably not the best for optimization, but it's what is working for now |
hmm, doesn't work when changing point attributes like size and color - seems like calling ngOnChanges resets all points to their default style |
in my case I'm trying to display a realtime chart. So I'd like to update chart whenever a new data point is available. Workaround suggested by @michaelknoch indeed refreshes chart, but it re-draws the whole chart rather than just adding a new data-point making it very hard to understand that it's a realtime data. I know chart.js is capable of this functionality. Even this older example shows that it's possible. I just don't know how to achieve this via this wrapper. |
This is because Angular 2 Change Detection only runs when value or reference changes. So you can manually trigger the change detection like the ways provided in this post, or give a different reference. This similar issue may help you understand: #378 |
For these having an issue with updating chart in realtime, this is actually an issue with charts.js itself. In v2 they removed these helpers that allowed to do this in v1. See issue 1997 |
@DmitryEfimenko cool, glad you find the issue. Thanks for help. I was actually writing you to let you create a new feature request. Now I can close this I guess. |
I think maybe we can provide a more friendly way to refresh the chart? @valorkin |
@hongbo-miao @valorkin Now, if the data change, chart change more troublesome.I suggest make a new API to update chart when datasets/labels changed |
Yeah, the suggested fix works, but it is really hacky. An update method would be nice and clean. I ended up with the following for a one-time async data load:
|
I found that using the public
|
This looks like the closest topics, and I don't want to open a new issue since mine's similar. I was having some serious problems getting the chart to properly initialize, and none of these solutions worked straight forward (data was being loaded async, but the chart would draw before the process finished and wouldn't re-render). A quick, easy, solution is to just wait to draw the canvas until your asyncs are finished. In your component:
and wrap your entire template with:
Spend several hours testing other workarounds, and thought this might help someone (it also seems a lot more straight-forward w/o need for adding any new imports). It's not a solution for later updates, but it's definitely a solution for a lot of the people that seem to have stumbled on this and the similar topics posted above that just want their chart initialized properly. |
@zbagley In my case it did update when i change the data, however the animation restarts so it's like if the animation cuts in the middle of it and starts all over, looks ugly. I did though something like your solution, but i had to revert to previous scenario, in my case, being an Ionic 2 app (mobile) not rendering anything for a few seconds (0.5 - 2 depending on hardware) would make the app look unresponsive, i prefer it to chop the animation instead, at least it looks like it's doing something. |
I handle the issue by implementing: However, i cannot update another chart. I need to update two different line charts as data comes in. Only one of them works. |
@zbagley your solution worked for me. Thanks a ton for that. I've been stuck for 2 days straight because I couldn't asynchronously update value in datasets of my bar chart. Now it works like a charm! |
@zbagley 's solution is a pretty standard way to do this kind of thing. But the library should have some async functionality. |
True, the solution is to modify the current array (labels/values) reference (push/slice, etc...) and then call chart.update(). Then the chart will update properly without triggering the whole animation. On the other hand if you reassign values to the arrays like |
@adadgio, as described above (including example) I experienced the opposite:
|
That's strange, i have the latest version and the behavior is exactly what i described... hmmm. |
@adadgio, yes it is indeed. If I use As several issues deal with change management in |
All right, i'm going to post a bit of raw code because i don't have much time, sorry. So here you have: a working example of pushing data that succesfuly redraws the chart when updated (ie. chart does redraw only last values correctly without triggering the whole animation). My angular2 app is a very simple bare bone app with just an app component and using I created a wrapper component for the chart just to have clean code, this is what i am going to post here. It basically fetches values from a remote file (you can even test with the IP given in the code as such). Note i am not using ngOnChanges here or anything, just pushing into app.component.html
chart.component.html Info: I am using this component 3 times for 3 charts, thus a few
chart.component.ts What this component does is fetch data via
Note: the use of |
@adadgio, thanks for sharing your code. I don't know how and when |
Yep sorry I changed the API this morning ! Refresh is only called when user clicks a button, or programmatically when i'm sure new data is there. Not very relevant either. It works for me when I remove values. I'm going to pseudo code , but basicaly what i did was:
... works
... also works. If you would like to investigate further, here is the full, updated code as up today (look at the same files). You should even be able to view the graph in real time if you run the app. |
No problem - understandable as you are working on this project. 😃 To make it clear: if I constantly add new values and therefore the number of data points increases everything is fine with But if I have a constant number of data values and remove the most left item with Also be aware that the animation for newly added values is different than for a constant number of values which move form the right to the left. See: You can change the |
I see. And what animation did you expect from the |
I expected to get another animation than the white area after the My workaround solves this, but the values do not move from right to left, but instead each value transforms form the old to the new value, which is better than the white area, but not that good for a live chart that constantly gets new values. |
I opened an issue in the charts.js repo: chartjs/Chart.js#4695. |
@katsuragi545 how did you change the color to red if they're clicked? Whenever I click mine, they only stay red for a second before going back to its default.. :( EDIT |
@zbagley This is great, don't know why I didn't think of this! *ngIf="chartData" on the chart wrapper and it's happy days! |
This worked for me after trying a few of the above solutions with no luck: http://plnkr.co/edit/Ra6cfYkctsq3ZnqxTHYf?p=preview |
There is another way to do it: In your HTML you have
and in the component I have a function which update the chart with new data, and then I clone the datasets and re-assign it
this works for me, hope it works for you too |
I am using angular 4, and this is what works for me
at the component.ts
function to populate the lineCharData Array (datasets)
|
With angular 4, I got it to work with the timeout method above:
|
@zbagley Your code block from Mar 18, 2017 is epic. #wearenotworthy |
<canvas baseChart and in the component I have a function which update the chart with new data, and then I clone the datasets and re-assign it drawChart(){ |
you can assing a value to ChartOptions even if it's the original one it dosen't matter |
@zbagley |
not working for me, |
You tired that implementation? |
i somehow collected fixes from here and there I made it work @ViewChild(BaseChartDirective) |
A little late addition but I've bumped into the same issue and for me, the following solution worked like a charm:
|
How do you go about updating the chart? After getting the chart to display, I changed the color of points to red if they are clicked. If the user clicks multiple points, then the older points go back to their original color. However, it only goes back if I hover my mouse over the old point. I would like this behavior to be automatic - I would imagine that calling update on the chart would work, but I get the error:
TypeError: this.chartObject.update is not a function...
Any ideas on how to do this with or without calling update?
The text was updated successfully, but these errors were encountered: