为什么我的addSeries(在高图表中)只显示系列的名称,而不显示实际的饼图本身

Why does my addSeries (in highcharts) only display names of the series but not the actual pie chart itself

本文关键字:显示 高图表 为什么 系列 我的 addSeries      更新时间:2023-09-26

当尝试将系列添加到Highcharts piechart时,只显示系列的名称,而不显示图形本身。

如果我手动输入序列,图表会正确显示,但当它通过.每个循环完成时,它只显示添加的序列名称。

这是我的代码:

function my_pie(){
        // Build the chart
   $('#pie_chart').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            marginBottom: 0,
            spacingTop: 0,
            marginTop:0,
            marginLeft:50,
            marginRight:50
        },
        credits:{
            enabled:false
        },
        title: {
            text: ''
        },
        tooltip: {
            pointFormat: '{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: false
            }
        },
        series: [{
            type: 'pie',
            name: 'Innehav',
            data: [ ] 
        }]
    });
    var chart = $('#pie_chart').highcharts();
    $.each(data_array, function( index, value ) {
        chart.addSeries({                        
            name: index,
            y: parseInt(value)
        }, false);
    });
    chart.redraw();
};

这是console.log(data_array)的输出

Object {Ja: "272", Nej: "30", Vet ej: "1"}

更新:以下是data_array的生成方式(我添加了num_check以使值为整数)

var data_array = <?php echo json_encode($data_pie, JSON_NUMERIC_CHECK ) ?>;

现在console.log(data_array)看起来是这样的:

 Object {Ja: 272, Nej: 30, Vet ej: 1}
饼图只是一个系列,而不是像其他类型那样是多个系列。您应该添加值:
$.each(data_array, function( index, value ) {
    chart.series[0].addPoint({                        
        name: index,
        y: parseInt(value)
    }, false);
});
chart.redraw();

或者更好,使用setData:

var data = []
$.each(data_array, function( index, value ) {
    data.push( [index, parseInt(value) ]);
});
chart.series[0].setData(data);