2013-05-28 107 views
0

我从数据库获取值到javascript数组。 Javascript代码,我正在使用静态值创建饼图如下。饼图的动态数据输入

 $(function() { 
     $('#container').highcharts({ 
      chart: { 
       type: 'pie' 
      }, 

      plotOptions: { 
       pie: { 
        dataLabels: { 
         distance: -30, 
         color: 'white' 
        } 
       } 
      }, 

      series: [{ 
       data: [ 
        [test[0], 44.2], 
        [test[1],  26.6], 
        [test[2],  20], 
        [test[3], 3.1], 
        [test[4], 5.4] 
       ] 
      }] 
     }); 
    }); 

我想我的数据是动态的,就像

   series: [{ 
       data: [ 
        for (var i=0;i<count;i++) 
       { 
         [test[i], 44.2] 
       } 
        ] 
      }] 

任何想法?

回答

0

您可以使用立即调用的函数表达式(IIFE),它返回一个数组作为您的数据。例如:

data: (function() { 
    var d = []; 
    for (var i=0; i < count; i++) { 
     d.push([test[i],44.2]);  // it's not clear from your question where the second number is supposed to come from! 
    } 
    return d; 
})()