我正在使用morris donuy图表,我无法将我的json数组数据传递给jquery corectly。这是我的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'
});
}
});
});
答案 0 :(得分:1)
您传递数据的格式不正确。http://morrisjs.github.io/morris.js/donuts.html 根据Moris donut文件
要绘制的数据。这是一个对象数组,包含
label
和value
属性对应于细分的标签和大小 甜甜圈图表。