简体   繁体   中英

Meteor Collection to ChartJs Data

I have a Meteor Collection which I want to be presented into a graph using ChartJS. I was able to follow how ChartJS documentation.

My problem now is how to transform my collection and pass it on to ChartJS.

ChartJs Data format :

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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