我试图在应用程序(Laravel MVC)中创建一个饼图,方法是在PHP中创建数组结构,然后使用json_encode将其传递给视图以使其兼容,但我的饼图全部被抬起。
我的数据集
gender | total
Female | 20
Male | 17
Other | 3
我有这个:
$chartArray["chart"] = array("plotBackgroundColor" => "null", "plotBorderWidth" => "null", "plotShadow" => "false");
$chartArray["title"] = array("text" => "Pet Intakes By Gender");
$chartArray["tooltip"] = array("pointFormat" => "{series.name}: {point.percentage:.1f}%");
$chartArray['plotOptions'] = array("pie" => array("allowPointSelect"=>"true","cursor"=>"pointer","dataLabels"=>array("enabled" => "false"),"showInLegend" => "true"));
$data = [];
foreach($results as $result){
$data = $this->array_push_assoc($data, $result->gender, $result->total);
}
$chartArray["series"] = array(array("type" => 'pie', "name" => 'Percentage of Gender', "data" =>array($data)));
return $chartArray;
在编码时产生这个:
{
"chart":{
"plotBackgroundColor":"null",
"plotBorderWidth":"null",
"plotShadow":"false"
},
"title":{
"text":"Pet Intakes By Gender"
},
"tooltip":{
"pointFormat":"{series.name}: {point.percentage:.1f}%"
},
"plotOptions":{
"pie":{
"allowPointSelect":"true",
"cursor":"pointer",
"dataLabels":{
"enabled":"false"
},
"showInLegend":"true"
}
},
"series":[
{
"type":"pie",
"name":"Percentage of Gender",
"data":[
{
"Female":17,
"Male":12,
"Other":2
}
]
}
]
}
我需要得到这样的东西(来自网站示例):
{
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Browser market shares at a specific website, 2014'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
}]
});
我错过了什么?谢谢!
答案 0 :(得分:0)
您的数据格式错误:
"data":[
{
"Female":17,
"Male":12,
"Other":2
}
]
这意味着你只有一个切片,没有任何价值..
我想这就是你想要做的事情:
"data":[
["Female", 17],
["Male", 12],
["Other", 2]
]