简体   繁体   English

流星收集到ChartJs数据

[英]Meteor Collection to ChartJs Data

I have a Meteor Collection which I want to be presented into a graph using ChartJS. 我有一个流星集合,我希望使用ChartJS将其呈现为图形。 I was able to follow how ChartJS documentation. 我能够按照ChartJS文档进行操作。

My problem now is how to transform my collection and pass it on to ChartJS. 我现在的问题是如何转换我的集合并将其传递给ChartJS。

ChartJs Data format : ChartJs数据格式:

function drawChart() {
    var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
        data: [28, 48, 40, 19, 86, 27, 90]
    }]
};

This is how my collection was saved : 这是我的收藏保存方式:

Categories.insert({
    categoryname : $('#categoryname').val(),
    value : $('#categoryvalue').val()
});

I wanted to use the categoryname as the chart labels and the value as the data. 我想使用categoryname作为图表标签,将值作为数据。 How am I going to do this? 我怎么会这样做?

This is how I made it work after another try after I posted my question. 这是我在发布问题后再次尝试使其工作的方式。

function drawChart() {
    var cur = Categories.find();

    collData = [];
    cur.forEach(function(cat){
        collData.push([cat.value]);
    });

    collLabel = [];
    cur.forEach(function(cat){
        collLabel.push([cat.categoryname]);
    });

    var data = {
        labels: collLabel,
        datasets: [{
            data: collData
        }]
    };
};

I am not sure if this the right way to do it but it works for now. 我不确定这是否是正确的方法,但它现在有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM