How to differentiate categories in highcharts drilldown?

Deadly 提交于 2019-12-25 16:43:21

问题


I want to make the chart like the picture below. I made the chart below by typing data manually.

Picture 1

But, I want to take data from database, but I don't know how to categorize the xAxis.

I have codes like this:

controller

$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);

index view

$(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,
            }]
        }
    })
});
")?>

Here is the table:

facultyin2011

department2011

and the result of those codes is like this picture below:

I want the result like picture 1. How to categorize the xAxis like picture 1? I mean the way to categorize data that taken from database

Thanks in advance

来源:https://stackoverflow.com/questions/43167724/how-to-differentiate-categories-in-highcharts-drilldown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!