Highchart Pie聊天不显示Codeigniter

时间:2019-03-15 20:28:15

标签: highcharts codeigniter-3

我正尝试首次将Highchart饼图与Codeigniter结合使用。我无法在下面显示带有我的代码的图表。我无法将json传递给图形数据以显示饼图。 任何帮助将不胜感激。

谢谢

json格式的查询结果显示如下:

  [{"sexo":"Feminino","total":"4"},{"sexo":"Masculino","total":"2"}]

控制器

public  function charCont() 
{
$query = $this->model_admin->getPieChat();
$data['query']=json_encode($query);
$this->load->view('chart',$data); 
}

查看

 <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Test'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
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: [{
name: 'Brands',
colorByPoint: true,
data:<?=$query?>

}]
});

1 个答案:

答案 0 :(得分:1)

您需要将数据预处理为Highcharts所需的格式:

var data = [{
    "sexo": "Feminino",
    "total": "4"
}, {
    "sexo": "Masculino",
    "total": "2"
}];

data = data.map(function(obj) {
    return {
        name: obj.sexo,
        y: Number(obj.total)
    }
});

Highcharts.chart('container', {
    ...,
    series: [{
        ...
        data: data
    }]
});

实时演示: http://jsfiddle.net/BlackLabel/yfLovx8b/

API参考: https://api.highcharts.com/highcharts/series.pie.data