Render chart after collection completed


#1

I have a collection that is going to hold 50 objects, each from a separate API call. I want to graph the findings in a pie chart after the collection has all of the data OR in real time, either one works for me. I have gotten real-time line chart to work but I can’t seem to make it work for bar charts.


#2

Can you show us some code so we have a better idea of what you’re trying to accomplish, and what the problem is?


#3

I’m using the Highcharts Meteor package and here is the code for the pie charts they are using: https://github.com/jhuenges/highcharts-demo/blob/master/client/demos/pieDemo.js

So, I have a for loop in my server main.js file that inputs the objects into the collection and then, on the client side, I was the pie chart to update automatically.


#4

Maybe this can help you. Take a look at the Tracker.autorun part.


#5

Have you tried to draw this chart feeding it with some static (manually entered within the code) data? Asking this just to be sure that the problem is in getting the data and not chart settings.

Here http://tri.infographer.ru (blaze) I had similar tasks. I used SubsManager there. To easily check if data is finally loaded is to wait for the onReady event:

App.subs = new SubsManager( {
    cacheLimit: 10,
    expireIn: 1440
} )

...

App.startsSub = App.subs.subscribe( 'starts', {
    onReady: () => console.log( `[${ loadTimer.duration() }sec] Starts data loaded.` ) 
})
App.starts = new Mongo.Collection( "starts" )

App here is the global application namespace, it was Meteor 1.2 app. Currently it is better to operate with modules. Pass your drawing function to Tracker and check the value of ready() for your subscription:

Template.templateName.rendered = () => {
    Tracker.autorun( collectFilteredDataAndRedraw )
    ...
    collectFilteredDataAndRedraw()
}

const collectFilteredDataAndRedraw = () => {
    if ( !App.startsSub.ready() ) return
    < collect data and make drawing here >
}

#6

Thanks for the answer jhuenges. I looked into it and made it work with Tracker.autorun.