使用php数据数组绘制带有highcharts的piechart

Draw piechart with highcharts using php array of data

本文关键字:highcharts piechart 绘制 php 数据 数组 使用      更新时间:2023-09-26

我想用JavaScript变量替换PHP数组$os_array,以使用不同的值发送。与var x =<?php echo $os_array; ?>; drawcharts($x);一样,当警报var x结果为['Internet Explorer 7',1],['Internet Explorer 8',1],['Outlook 2007',2]

var x="<?php echo $os_array;?>";
drawcharts(x);
function drawcharts(x){
    $('#_shared_graphs').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            backgroundColor: '#f1f1f1'
        },
        title: {
            text: 'OS'
        },
        tooltip: {
            pointFormat: '{point.y} Using <b>{point.name}'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
                type: 'pie',
                name: 'Sent Messag stat',
                data: [x]
            }]
    });
}

好吧,定义你的js-fkt:

function drawcharts(x) {
 $('#_shared_graphs').highcharts({
  ...
series: [{
        ...
        data: x
    }]
 });
}

然后

 var x =<?php echo json_encode($os_array); ?>; 
 drawcharts(x);
 // reassign x
 x = [ .... ];
 drawcharts(x);