Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用json数据创建chartjs饼图_Javascript_Json_Chart.js - Fatal编程技术网

Javascript 使用json数据创建chartjs饼图

Javascript 使用json数据创建chartjs饼图,javascript,json,chart.js,Javascript,Json,Chart.js,我正在尝试使用chartjs显示多个图表。我有一个工作与硬编码的多数据。如何使用从控制器检索到的json数据实现相同的结果?如何将检索到的json数据作为图表的数据集推送 这是从我的控制器返回的json数据: [{"countAgree":1,"countSomewhatAgree":0,"countDisagree":1}, {"countAgree":0,"countSomewhatAgree":1,"countDisagree":1}, {"countAgree":0,"countSome

我正在尝试使用chartjs显示多个图表。我有一个工作与硬编码的多数据。如何使用从控制器检索到的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
});
$.getJSON(“/Controller/GetData”),函数(数据){

对于(var i=0;i而言,问题在于在循环中处理数据后的数据结构

将循环更改为以下内容:

$.each(obj, function (i, data) {
   jsonData.push({ A: data.countAgree, B: data.countSomewhatAgree, C: data.countDisagree, Q: 'Question' + (i + 1) });
});