My code should replace the currently chart by a newest, created each time new data is registered in my DB.
I already tried tu update the chart by different ways. For example:
update “chart.data.labels & datasets” each time new data is registered in my DB.
What should I change in my code to have a dynamic chart ?
Why do you need to replace the chart? Shouldn’t the chart update when the date updates? I guess you need to set the new data to the existing chart with the autorun function. Otherwise you will have to remove the d chart from the from the dom which will cause flicker.
Can you explain me how can I do this (update with the autorun function) please ?
I already read some docs and related issues but what i tried didn’t work. That’s why I replace the existing chart.
I think it depends on the chart library on how it’s update. I was assuming it has an update function (like chart.setData(someData) where it takes new data list and then it will update the chart.
Which chart library are you using? the chart library needs to update based on the new data, so there could an update call or refresh or something, otherwise it won’t know that it has new data.
Yeah, so you modify the data using the remove/add functions and then call chart.update() as per the example on the link you provided, did you try that already?
That code looks fine to me…maybe you need to clean the data before adding more or the data is not valid? I did similar code back when I was using blaze and here is what I did before, in case it helps.
var myPieChart = new Chart(ctx).Pie(data,options);
if(this!=undefined && this.data!=undefined) {
Events.find(this.data._id).observeChanges({
added: function (id, fields) {
update();
},
changed: function (id, fields) {
update();
},
removed: function () {
update();
}
});
}
var template=this;
function update(){
if(template!=undefined&&template.data!=undefined){
var event=Events.findOne(template.data._id);
console.log(event.confirmedCount+","+event.waitingCount+","+"0"+event.openSlotsCount);
myPieChart.segments[0].value=event.confirmedCount;
myPieChart.segments[1].value=event.waitingCount;
myPieChart.segments[2].value=0;
myPieChart.segments[3].value=event.openSlotsCount;
myPieChart.update();
}
}