Okay, so I’m using ChartJS with my meteor project and I have added graphs that accept data. When I click a button, a new item is added to the collection, making its length 1 and the graph should render on the screen with the data that is sent in, but instead, it says “Cannot read property ‘getContext’ of null” and doesn’t render. If I press the button again or refresh the page, the graph shows up.
Here is the code that I’m working with:
Template.dashboard.rendered = function(){
this.autorun(function(){
if(collection.find({}).count()===1){
buildGraph(1,2,3);
}
});
});
The buildGraph function is as such:
buildGraph = function buildgraph(x,y,z) {
// Data and options for charts
var lineData = {
labels: ["Monday", "Tuesday", "Wednesday],
datasets: [
{
label: "Example dataset",
fillColor: "rgba(57,185,253,0.5)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#39B9FD",
pointHighlightFill: "#39B9FD",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [x,y,z]
}
]
};
var lineOptions = {
scaleShowGridLines: true,
scaleGridLineColor: "rgba(0,0,0,.05)",
scaleGridLineWidth: 1,
bezierCurve: true,
bezierCurveTension: 0.4,
pointDot: true,
pointDotRadius: 4,
pointDotStrokeWidth: 1,
pointHitDetectionRadius: 20,
datasetStroke: true,
datasetStrokeWidth: 2,
datasetFill: true,
responsive: true,
};
var ctx = document.getElementById("lineChart").getContext("2d");
var myNewChart = new Chart(ctx).Line(lineData, lineOptions);
The graph template is:
<template name="graphChart">
<div class="wrapper wrapper-content animated fadeInRight">
<div>
<canvas id="lineChart" height="140"></canvas>
</div>
</div>
</template>
and then in my main template, I have added {{>graphChart}}
where I want the graph to appear. It only shows up after refreshing the page.
What could be the problem with this?