嗨我用ajax获取数据,他们随json一起来,所以我的代码就是这个
$.ajax({
async:true,
type: "POST",
dataType: "html",
contentType: "application/x-www-form-urlencoded",
url:"http://sosacelulares.com/index.php/chart/buygeneralreport",
success: function(response) {
var returnedData = JSON.parse(response);
数据看起来像这样
[{
"id_buy_report":"1",
"month":"1",
"year":"2016",
"total":"10"
},{
"id_buy_report":"2",
"month":"1",
"year":"2017",
"total":"20"
}]
然后我尝试使用该数据创建饼图,完整的代码是这样的:
$.ajax({
async:true,
type: "POST",
dataType: "html",
contentType: "application/x-www-form-urlencoded",
url:"http://sosacelulares.com/index.php/chart/buygeneralreport",
success: function(response) {
var returnedData = JSON.parse(response);
var donutData = [
{label: "Series2", data: 10},
{label: "Series3", data: 40},
{label: "Series4", data: 50}
];
$.plot("#donut-chart", donutData, {
series: {
pie: {
show: true,
radius: 1,
innerRadius: 0.5,
label: {
show: true,
radius: 2 / 3,
formatter: labelFormatter,
threshold: 0.1
}
}
},
legend: {
show: false
}
});
问题是我的数据将如何从数据库中看到,如果我想创建饼图,我需要将数据放在这里..
var donutData = [
{label: "Series2", data: 10},
{label: "Series3", data: 40},
{label: "Series4", data: 50}
];
它会创建一个包含三个部分的饼图,但它是静态的我需要放一个(for循环)来自动创建数据..
如何在donutData代码中创建for循环?任何循环它都没有必要for循环..我只需要一个循环,它允许我自动填充donutData。感谢
答案 0 :(得分:0)
只需使用forEach循环遍历它,就像这样:
var donutData = [
{label: "Series2", data: 10},
{label: "Series3", data: 40},
{label: "Series4", data: 50}
];
donutData.forEach(function(data) {
console.log(data.label);
console.log(data.data);
});