我想制作如下图所示的图表。我通过手动输入数据制作了下面的图表。
图片1
但是,我想从数据库中获取数据,但我不知道如何对xAxis进行分类。
我有这样的代码:
控制器
$faculty = (new \yii\db\Query())
->select(['Faculty'])
->from('facultyin2011')
->limit(10)
->column();
$age1 = (new \yii\db\Query())
->select(['lessthan25'])
->from('facultyin2011')
->limit(10)
->column();
$age1 = array_map('floatval', $age1);
foreach ($age1 as $key => $age1_value) {
$age1[$key] = [
'name' => 'Fakultas',
'y' => $age1_value,
'drilldown' => 'department1'
];
};
$age2 = (new \yii\db\Query())
->select(['btween25to29'])
->from('facultyin2011')
->limit(10)
->column();
$age2 = array_map('floatval', $age2);
foreach ($age2 as $key => $age2_value) {
$age2[$key] = [
'name' => 'Fakultas',
'y' => $age2_value,
'drilldown' => 'department2'
];
};
$data['ageforfacultystudent'] = json_encode($faculty);
$data['age1'] = json_encode($age1);
$data['age2'] = json_encode($age2);
// next
$department = (new \yii\db\Query())
->select(['Department'])
->from('department2011')
->limit(10)
->column();
$agedepartment1 = (new \yii\db\Query())
->select(['lessthan25'])
->from('department2011')
->limit(10)
->column();
$agedepartment1 = array_map('floatval', $agedepartment1);
foreach ($agedepartment1 as $key => $agedepartment1_value) {
$agedepartment1[$key] = [
'name' => 'A'.($key+1),
'y' => $agedepartment1_value,
'drilldown' => 'major1'
];
};
$agedepartment2 = (new \yii\db\Query())
->select(['btween25to29'])
->from('department2011')
->limit(10)
->column();
$agedepartment2 = array_map('floatval', $agedepartment2);
foreach ($agedepartment2 as $key => $agedepartment2_value) {
$agedepartment2[$key] = [
'name' => 'A'.($key+1),
'y' => $agedepartment2_value,
'drilldown' => 'major2'
];
};
$data['agefordepartmentstudent'] = json_encode($department);
$data['agedepartment1'] = json_encode($agedepartment1);
$data['agedepartment2'] = json_encode($agedepartment2);
索引视图
$(function () {
// Create the chart
$('#containers').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
xAxis: [{
id: 0,
categories: $ageforfacultystudent
},
{
id: 1,
categories: $agefordepartmentstudent
}],
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
}
}
},
series: [
{
name: 'lessthan25',
data: $age1,
},
{
name: 'btween25to29',
data: $age2,
}],
drilldown: {
series: [{
id: 'department1',
name: 'lessthan25',
xAxis: 1,
data: $agedepartment1,
},
{
id: 'department2',
name: 'btween25to29',
xAxis: 1,
data: $agedepartment2,
}]
}
})
});
")?>
这是表格:
facultyin2011
department2011
这些代码的结果如下图所示:
我希望结果如图1所示。如何将xAx分类为图片1?我的意思是对从数据库中获取的数据进行分类的方法
提前致谢