简体   繁体   English

饼图的动态数据输入

[英]Dynamic data input for pie chart

I am getting values from the database to javascript array. 我正在从数据库中获取值到javascript数组。 Javascript code which I am using to create pie chart with static value is as follows. 我用来创建具有静态值的饼图的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]
                ]
            }]
        });
    });

I want my data to be dynamic like 我希望我的数据像

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

Any idea? 任何想法?

You can use an immediately invoked function expression (IIFE) that returns an array as your data. 您可以使用立即调用的函数表达式(IIFE),该函数表达式将数组作为数据返回。 Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM