在chartjs饼图中绘制json数据

时间:2018-04-28 16:17:24

标签: javascript json charts chart.js

我试图使用chartjs显示多个图表。我有一个工作JSFIDDLE,带有硬编码的多个数据。如何使用来自控制器的检索到的json数据获得相同的结果?如何将检索到的json数据作为图表的数据集推送?

这是从我的控制器返回的json数据:

[{"countAgree":1,"countSomewhatAgree":0,"countDisagree":1},
{"countAgree":0,"countSomewhatAgree":1,"countDisagree":1},
{"countAgree":0,"countSomewhatAgree":1,"countDisagree":1}]

我尝试了以下操作,但未创建图表:

var charts = "myCharts";

var jsonData = [];

$.ajax({
    cache: false,
    async: false,
    type: "GET",
    url: '/Controller/GetData',
    dataType: 'json',
    success: function (response) {
        var obj = JSON.parse(response);
        $.each(obj, function (i, data) {
            jsonData.push({ data: [data[i].countAgree, data[i].countSomewhatAgree, data[i].countDisagree] });
        });
        //create chart
    }
});

我也试过

$.getJSON("/Controller/GetData", function (data) {
    for (var i = 0; i <= data.length - 1; i++) {
        jsonData.push({ data: [data[i].countAgree, data[i].countSomewhatAgree, data[i].countDisagree] });
    }

    //create charts
});

0 个答案:

没有答案