如何在Yii2 Highchart中传递数据库值

时间:2015-12-23 02:26:19

标签: highcharts yii2

我尝试使用来自this存储库的Yii2和highchart小部件创建图表

这是我的Highchart的代码:

echo Highcharts::widget([
        'scripts' => [
            'highcharts-more',   
            'modules/exporting', 
            'themes/grid' 
        ],
        'options' => [
          'title' => ['text' => NULL],
          'xAxis' => ['categories' => $chart_x_axis],
          'yAxis' => ['title' => ['text' => 'Income']],
          'series' => $data_series, 
          'credits' => ['enabled' => false],
        ],
    ]);

首先,我手动创建数据系列:

$data_series = array(
    array(
        'name'=>'February',
        'data'=>array(10000,20000,3000),
    ),array(
        'name'=>'November',
        'data'=>array(20000,10000),
    ),array(
        'name'=>'December',
        'data'=>array(20500),
    )
);

以及结果:

=================Json Format : 
[{"name":"February","data":[10000,20000,3000]},{"name":"November","data":[20000,10000]},{"name":"December","data":[20500]}]
 ================= Array Format : 
Array
(
    [0] => Array
        (
            [name] => February
            [data] => Array
                (
                    [0] => 10000
                    [1] => 20000
                    [2] => 3000
                )

        )

    [1] => Array
        (
            [name] => November
            [data] => Array
                (
                    [0] => 20000
                    [1] => 10000
                )

        )

    [2] => Array
        (
            [name] => December
            [data] => Array
                (
                    [0] => 20500
                )

        )

)

Highchart show 正常
但是当我尝试从具有相同结果的模型加载时:

=================Json Format : 
[{"name":"February","data":["3000","10000","20000"]},{"name":"November","data":["10000","20000"]},{"name":"December","data":["20500"]}]
 ================= Array Format : 
Array
(
    [0] => Array
        (
            [name] => February
            [data] => Array
                (
                    [0] => 3000
                    [1] => 10000
                    [2] => 20000
                )

        )

    [1] => Array
        (
            [name] => November
            [data] => Array
                (
                    [0] => 10000
                    [1] => 20000
                )

        )

    [2] => Array
        (
            [name] => December
            [data] => Array
                (
                    [0] => 20500
                )

        )

)

数据系列未显示。任何人都可以帮我解决错误吗?

2 个答案:

答案 0 :(得分:0)

在您的模型中创建方法:

public method getData(){
   $data_series = array(
    array(
        'name'=>'February',
        'data'=>array(10000,20000,3000),
    ),array(
        'name'=>'November',
        'data'=>array(20000,10000),
    ),array(
        'name'=>'December',
        'data'=>array(20500),
    )
);

return  $data_series;
}

在你看来

use use app\models\highchart;
echo Highcharts::widget([
        'scripts' => [
            'highcharts-more',   
            'modules/exporting', 
            'themes/grid' 
        ],
        'options' => [
          'title' => ['text' => NULL],
          'xAxis' => ['categories' => $chart_x_axis],
          'yAxis' => ['title' => ['text' => 'Income']],
          'series' => $highchar->getData, 
          'credits' => ['enabled' => false],
        ],
    ]);

答案 1 :(得分:0)

对不起,这是我的愚蠢。 此问题已解决here

我不知道highcart接受INTEGER数据类型,因此我更改了我的代码:

foreach ($months as $m)
{
    $_data_series[] = $m["omset_nominal"];
}

进入:

foreach ($months as $m)
{
    $_data_series[] = (int)$m["omset_nominal"];
}