Reputation: 31
I create some statistics data on server side. It is calculated once from data stored in MongoDB. It isn't a reactive data.
It is stored in JSON format and should be send to client to show on screen by nvd3. What is the best method to do that?
Maybe this calculation should be done on client side?
For example: Create one array with data:
d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
and second array:
d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];
Both arrays visualize on one chart.
What way use to generate (Meteor.methods, publish) data on server side and how to send it from server to client (Meteor.call, subscribe, HTTP.call)?
I would be grateful for any ideas and suggestions.
Upvotes: 3
Views: 983
Reputation: 75945
There are 3 ways to do this. You can generate your data using a Publish/Subscribe, use a Method/Call or calculate it on the client instead. A Method is good at delivering short sets of data. The publish can handle larger sets of data better.
I'd advise on calculating it on the client side since you can have live changes (changes in sorting, the field types/rescaling, etc). You could use a Transform to calculate your data.
This is best when you have larger sets of data since the data can be streamed to the client instead of sent in one big chunk
Server Side
Meteor.publish("data", function(){
var d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
var d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];
this.added("data", "1", {data: d1 });
this.added("data", "2", {data: d2 });
this.ready();
});
Client side:
Chartdata = new Meteor.Collection("data");
Meteor.subscribe("data");
data = Chartdata.find().fetch()
//Data is stored in an array
Perhaps simpler, but its not very good for larger sets of data since the data comes all at once and isn't streamed:
Server:
Meteor.methods({
getData:function() {
var d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
var d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];
return [d1, d2]
}
});
Client side:
Meteor.call("getData", function(err, result) {
console.log(result)
//Gives back an array with d1 in position 0 and d2 in position 1
});
This depends on your preferences. If you prefer it to do it this way its the best of the lot. The thing is that you have to publish the raw data to the client for it to calculate.
var Data = new Meteor.Collection("data");
//Use a transform to calculate your data
var transform = function(doc) {
//Calculate your value ? Not sure how you're doing this just an example with static data
var data = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
return data;
}
//result should now have the calculated data
var result = Data.find({}, { transform: transform }).fetch();
Upvotes: 8