包含动态 json 数据的莫里斯甜甜圈图

Morris donut chart with dynamic json data

我正在使用 morris donuy 图表,但我无法正确地将我的 json 数组数据传递给 jquery。这是我的 php 文件:

$stmt=$dbh->prepare("Select COUNT(Incasari.id_produs) as cnt,Incasari.id_produs,denumire_produs from Incasari
                    INNER JOIN Produse on Produse.id_produs=Incasari.id_produs Group By Incasari.id_produs order by cnt desc limit 3 ");
$stmt->execute();

while ($row=$stmt->fetch())
{
    $arr[]= array(
        'id'   => ''.$row['id_produs'].'',
        'denumire'   => ''.$row['denumire_produs'].'',
        'cnt'  => ''.$row['cnt'].''
    );

}
echo json_encode($arr);

返回结果为:

[{"id":"3","denumire":"bere","cnt":"5"},
 {"id":"1","denumire":"Suc","cnt":"3"},
 {"id":"2","denumire":"pepsi","cnt":"2"}]

和 javascript 部分:

$(document).ready(function(){
    $.ajax({
        url: 'app/chart_produse.php', // getchart values
        dataType: 'JSON',
        type: 'POST',
        data: {get_values: true},
        success: function(response) {
            var donut = new Morris.Donut({
                element: 'sales-chart',
                resize: true,
                colors: ["#3c8dbc", "#f56954", "#00a65a"],
                data: response,
                hideHover: 'auto'
            });
        }
    });
});

你传递的数据格式不对。http://morrisjs.github.io/morris.js/donuts.html 根据 Moris 甜甜圈文档

The data to plot. This is an array of objects, containing label and value attributes corresponding to the labels and sizes of the segments of the donut chart.