0

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances

<?php

$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
            'sales'=>array(),
            'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
    $arr['id'] = $modEm[$i]['id'];
    $arr['sales'] = $modEm[$i]['sales'];
    $arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
    echo GoogleChart::widget(array('visualization' => 'LineChart',
                 'data' => array(
            array('Year', 'Sales', 'Expenses'),
            array($arr['id'],$arr['sales'],$arr['expenses']),

    ),
                'options' => array(
                    'title' => 'My Company Performance2',
                    'titleTextStyle' => array('color' => '#FF0000'),
                    'vAxis' => array(
                        'title' => 'Scott vAxis',
                        'gridlines' => array(
                            'color' => 'transparent'  //set grid line transparent
                        )),
                    'hAxis' => array('title' => 'Scott hAixs'),
                    'curveType' => 'function', //smooth curve or not
                    'legend' => array('position' => 'bottom'),
                )));

?> 

enter image description here

2 Answers 2

2

first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.

you have to create an array of values and pass it to the graph widget as following

$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses'); 

for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
 $arr['id'] = $modEm[$i]['id'];
 $arr['sales'] = $modEm[$i]['sales'];
 $arr['expenses'] = $modEm[$i]['expenses'];
 $graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
             'data' => $graph_data,
            'options' => array(
                'title' => 'My Company Performance2',
                'titleTextStyle' => array('color' => '#FF0000'),
                'vAxis' => array(
                    'title' => 'Scott vAxis',
                    'gridlines' => array(
                        'color' => 'transparent'  //set grid line transparent
                    )),
                'hAxis' => array('title' => 'Scott hAixs'),
                'curveType' => 'function', //smooth curve or not
                'legend' => array('position' => 'bottom'),
            )));
Sign up to request clarification or add additional context in comments.

3 Comments

that is what i did in the answer
after passing this $graph_data to 'data' can not be display on page empty page
Thanks ..It's Work perfectly ...Thanks
0

Try this

action

$model=Employee::find()->select(['id','sales','expenses'])->all();
    $data[]=["id","sales","expenses"];
    foreach ($model as $item) {
        $data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
    }
    return $this->render('test',['data'=>$data]);

view

echo GoogleChart::widget(array('visualization' => 'LineChart',
            'data' => $data,
            'options' => array(
                'title' => 'My Company Performance2',
                'titleTextStyle' => array('color' => '#FF0000'),
                'vAxis' => array(
                    'title' => 'Scott vAxis',
                    'gridlines' => array(
                        'color' => 'transparent'  //set grid line  transparent
                    )),
                'hAxis' => array('title' => 'Scott hAixs'),
                'curveType' => 'function', //smooth curve or not
                'legend' => array('position' => 'bottom'),
            )));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.