0
我想繪製一個圖表使用chart.js。當我使用靜態數字時,我可以使用onRendered顯示圖表,但是當我嘗試通過從集合中獲取數據來創建變量時,該變量返回undefined。我認爲這是一個加載順序問題。findOne返回未定義onRendered
Template.communication.onRendered(function() {
// Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
// get user data for charts
var id = FlowRouter.getParam('id');
var user = ProfessionalOverview.findOne({"candidateUserId": id});
var communicationSkills = user && user.communicationSkills;
// Set the data
var data = {
labels: ["Communication skills"],
datasets: [
{
data: [communicationSkills, 2],
backgroundColor: [
"#FF6384",
"#F2F2F2",
]
}]
};
// And for a doughnut chart
var myDoughnutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
cutoutPercentage: 50,
tooltips: false,
legend: {
labels:{
display: true,
boxWidth: 0
},
onClick: (e) => e.stopPropagation(),
},
animation:{
animateRotate: true,
render: false,
},
}
});
});