为什么我的 addSeries(在 highcharts 中)只显示系列的名称而不是实际的饼图本身
Why does my addSeries (in highcharts) only display names of the series but not the actual pie chart itself
尝试将系列添加到 Highcharts 饼图时,仅显示系列的名称,而不显示图形本身。
如果我手动输入系列,图表会正确显示,但当它通过 .each 循环完成时,它只显示添加的系列名称。
这是我的代码:
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);
尝试将系列添加到 Highcharts 饼图时,仅显示系列的名称,而不显示图形本身。
如果我手动输入系列,图表会正确显示,但当它通过 .each 循环完成时,它只显示添加的系列名称。
这是我的代码:
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);