2017-03-07 117 views
-1

您好我得到我的数据与阿贾克斯,他们是使用JSON来了,所以我的代码是这样如何在这里使用for循环?

$.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} 
    ]; 

这将创建一个饼图有三个部分,但它是静态的我需要把A(for循环),以使数据全自动创建..

如何在donutData代码中创建for循环?任何循环都没有必要for循环..我只需要一个循环,它允许我自动填充donutData。由于

+0

'数据类型: “HTML”'< - 什么? – Phil

回答

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); 
});