I am building a miniapp with meteor to plot a pair of data series in a nvd3 chart. Those data series are arrays of 201 (x, y) points, which are calculated from only some user input values, which I am storing into session variables, something like this:
HTML:
<input type="number" name="my-value" min="0" step="0.01">
JS:
Template.body.onCreated(function(){
// Initialization
Session.set("my_value_1", 0);
Session.set("my_value_2", 0);
Dataseries = new Mongo.Collection(null);
Dataseries.insert({_id: "x_values", data: range(0, 20, 201)})
Dataseries.insert({_id: "y1", data: Array.from({length: 201}, () => 0)})
Dataseries.insert({_id: "y2", data: Array.from({length: 201}, () => 0)})
});
In Template.body.events I use the change event on my parameters form, and use Session.set to store new Session variable values when Session.equals is false. That is ok.
The problem is the Minimongo dataseries. Once all the Session variables are set, I update the series like this (range is a function I wrote to generate a range, just like in Python):
Dataseries.update("x_values", {$set: {data: range(my_value1*0.85, my_value2*1.15, 201)}})
And my chart is built from that data like this:
Template.chart.onRendered(function() {
var myData = [
{
values: [{x: Dataseries.find("x_values"), y: Dataseries.find("y1")}],
key: 'Chart1',
color: '#ff7f0e'
},
{
values: [{x: Dataseries.find("x_values"), y: Dataseries.find("y2")}],
key: 'Chart2',
color: '#2ca02c'
}
];
// ... more code to run the chart
});
The chart starts empty, with values between -1 and 1 in both X and Y axis, and of course it is not being updated automatically.
I supposed that, since I am defining the data as the result from a Minimongo query, a change in the collection would imply updating the chart itself, but it does not seem so. I tried looking for information on the publish-suscribe model, but it does not seem to apply here, since I am not suscribing to a collection created in the server: it is all happening at the client.
How can I achieve reactivity on my MiniMongo data?
Since I am defining Dataseries variable into Template.body.onCreated, is the variable also available into Template.chart.onRendered?
Thanks