使用GoogleChart在yii2中创建动态价值图表?

时间:2016-06-28 11:02:55

标签: yii2

在这里,我想通过动态值绘制折线图。但在我的情况下,为数组的每个值创建不同的不同图...请帮助我,我是第一次执行此任务。谢谢进展

<?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 个答案:

答案 0 :(得分:2)

首先,多个图是因为你在for循环中做回声,所以它只需要一个值并从中创建图。

您必须创建一个值数组并将其传递给图表小部件,如下所示

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

答案 1 :(得分:0)

试试这个

   动作

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

视图

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'),
            )));