简体   繁体   中英

Charts in yii using highcharts

This is my controller

public function actionStudentChart()
        {
            $stu_track_id = Yii::app()->user->getState('stu_track_id');
            $model = MarkDetails::model()->findAllByAttributes(array('mark_stud_track_id' => $stu_track_id ));
            $stu_inst_id = Yii::app()->user->getState('stu_inst_id');
            $stu_class = Yii::app()->user->getState('stu_class');
            if(SchoolSubjectDetails::model()->findByAttributes(array('school_subj_inst_id' => $stu_inst_id , 'school_subj_class' => $stu_class)))
            {
                $schoolsubmodel = SchoolSubjectDetails::model()->findByAttributes(array('school_subj_inst_id' => $stu_inst_id , 'school_subj_class' => $stu_class));
            }
            if(!empty($schoolsubmodel->school_subject) && is_array(explode(',',$schoolsubmodel->school_subject)))
                            $schoolsubmodel->school_subject=explode(',',$schoolsubmodel->school_subject);
            $submodel = array();
            $sub_label = array();
            $sub_name = array();
            $marks[0] = array(90,80,80);
            $marks[1] = array(90,80,80);
            $marks[2] = array(90,80,80);
            $marks[3] = array(90,80,80);
            $marks[4] = array(90,80,80);
            for($i=0 ; $i<count($schoolsubmodel->school_subject) ; $i++)
            {
                $submodel[$i] = SubjectDetails::model()->findByAttributes(array('subject_id' =>$schoolsubmodel->school_subject[$i]));
                $sub_label[$i] = $submodel[$i]['subject_label'];
                $sub_name[$i] = $submodel[$i]['subject_name'];
            }
            foreach($model as $val)
            {
                for($i=0;$i<count($sub_label);$i++)
                {
                    $mrk[$i][]=$val[$sub_label[$i]];
                }
            }
            $this->render('studentchart',array(
                'model'=>$model,'sub_label'=>$sub_label,'sub_name'=>$sub_name,'marks'=>$marks,'mrk'=>$mrk,
            ));
        }

This is my view page

if(!empty($model))
        {
            for($i=0 ; $i<count($model) ; $i++)
            {
                $exam_type[] = $this->_exam_type[$model[$i]->mark_exam_type];
            }
            for($i=0 ; $i<count($sub_label) ; $i++)
            {
                $chartData1[] = array( "name"=>$sub_name[$i] , "data"=>$marks[$i]);
                $chartData2[] = array( "name"=>$sub_name[$i] , "data"=>$mrk[$i]);
            }
            $this->Widget('ext.highcharts.HighchartsWidget', array(
               'options' => array(
                  'chart' => array('type' => 'bar'),
                  'title' => array('text' => false),
                  'xAxis' => array(
                     'categories' => $exam_type
                  ),
                  'yAxis' => array(
                     'title' => array('text' => 'Marks Obtain')
                  ),
                  'series' => $chartData1
               )
            ));
            $this->Widget('ext.highcharts.HighchartsWidget', array(
               'options' => array(
                  'chart' => array('type' => 'bar'),
                  'title' => array('text' => false),
                  'xAxis' => array(
                     'categories' => $exam_type
                  ),
                  'yAxis' => array(
                     'title' => array('text' => 'Marks Obtain')
                  ),
                  'series' => $chartData2
               )
            ));
        }

here am trying to display chart from my markdetails table i have taken tha marks in data and subject in names and passing tha name and data array to the chart widget's series now the problem is if i give the valu in static displaying the chart if i get the value from data base graph is not plotted.

Here i have print and check both chartData1 and chartData2 both has same values but when i pass this values to my widget chartData1 has ploted the graph where as for chartData2 "data" array not passed so graph not plotted

Array
(
    [0] => Array
        (
            [name] => English
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [1] => Array
        (
            [name] => Language1
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [2] => Array
        (
            [name] => Science
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [3] => Array
        (
            [name] => Social Science
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [4] => Array
        (
            [name] => Mathematics
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

)
Array
(
    [0] => Array
        (
            [name] => English
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [1] => Array
        (
            [name] => Language1
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [2] => Array
        (
            [name] => Science
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [3] => Array
        (
            [name] => Social Science
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

    [4] => Array
        (
            [name] => Mathematics
            [data] => Array
                (
                    [0] => 90
                    [1] => 80
                    [2] => 80
                )

        )

)

In controller change foreach as

foreach($model as $val)
   {
        for($i=0;$i<count($sub_label);$i++)
        {
             $mrk[$i][]=intval($val[$sub_label[$i]]);
         }
   }

This is working,finally got....................

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM