weixin_33712881 2017-03-07 03:14 采纳率: 0%
浏览 18

我如何在这里使用for循环?

Hi I'm getting my data with ajax, they are coming with json so my code is this

$.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);

The data looks like this

[{
  "id_buy_report":"1",
  "month":"1",
  "year":"2016",
  "total":"10"
},{
  "id_buy_report":"2",
  "month":"1",
  "year":"2017",
  "total":"20"
}]

Then I am trying to use that data to create a Pie Chart and the complete code is this:

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

The problem is that my data are coming how you can see from a database and if i want to create the Pie Chart I need to put the data here..

var donutData = [
    {label: "Series2", data: 10},
    {label: "Series3", data: 40},
    {label: "Series4", data: 50}
  ];

It will create a pie chart with three portions, but it is static i need to put a (for loop) to allow the data create automaticly..

How can I make a for loop in that donutData code? any loop it's no necessary a for loop..I need just a loop that it allows me to fill donutData automaticly. Thanks

  • 写回答

1条回答 默认 最新

  • ℡Wang Yan 2017-03-07 03:32
    关注

    Just use a forEach to loop through it, like this:

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

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中