【问题标题】:How to pass the database data and display it in yii2 highCharts?如何传递数据库数据并在yii2 highCharts中显示?
【发布时间】:2020-02-28 01:43:04
【问题描述】:

我已经安装了 miloschuman\highcharts\Highcharts,网址:https://www.yiiframework.com/extension/yii2-highcharts-widget。我有一个包含表列 lme_pricelme_name 的数据库,我想在 highcharts 中显示铜的价格。我正在使用 PHP。

以下是我完成的代码。这是我模型中的代码。我创建了一个带有查询的静态函数,以从数据库中查找我想要的数据。

public static function getCopperPrice()
{
    $getCopperPrices = Lme::find()
                    ->select('lme_price')
                    ->where(['lme_name' => 'LME Copper'])
                    ->all();
    return $getCopperPrices;
}

这是我在视图中的代码。我在视图中显示了一个表格,其中显示了数据库中的每个数据。

<div class="lme_index">
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    //  'searchModel'=> $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label'=> 'DATE',
            'attribute' => 'lme_title',
        ],
        [
            'label'=> 'MATERIAL',
            'attribute'=> 'lme_name',
        ],   
        [
            'label'=> 'PRICE (US$)',
            'attribute'=> 'lme_price',
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

这是 highcharts 图表的代码。

<div class = "graph">
    <?= Highcharts::widget([
        'options' => [
            'title' => ['text' => 'Copper Price'],
            'xAxis' => [
                'categories' => []
            ],
            'yAxis' => [
                'title' => ['text' => 'Price $US']
            ],
            'plotOption' => [
            'series' => [
                ['name' => 'Copper', 
                'data' => [lme::getCopperPrices()]],
             ]
            ]
        ]
    ]);?>

我只想在 highcharts 中显示 1 个材料价格。我从系列内的模型中调用该函数,但图表上没有显示任何内容。那里的编码没有向我显示任何错误。

有人可以帮助我或告诉我如何解决这个问题吗?谢谢。

【问题讨论】:

  • lme::getCopperPrices() 返回ActiveRecord 对象。尝试使用Lme::find()-&gt;select('lme_price')-&gt;where(['lme_name' =&gt; 'LME Copper'])-&gt;column();
  • @InsaneSkull 在那个视图页面的输出也一样,图中没有显示。
  • 图表或网格视图不起作用? lme::getCopperPrices() 的输出是什么?
  • @InsaneSkull 当我更改 ->all(); 时,我的图表起初没有显示任何内容;给你建议的那个。 lme::getCopperPrices() 的输出显示了数组字符串,但图表数据只接受整数。我设法通过使用 foreach 循环遍历数组并将字符串转换为整数来获取图表中显示的数据。谢谢你的帮助。我真的很感激你给我的意见。 :D

标签: php highcharts yii2


【解决方案1】:

图表需要integer 值才能显示

public static function getCopperPrice()
{
     $cooperPrices = [];

     $copperPriceList = Lme::find()
         ->select('lme_price')
         ->where(['lme_name' => 'LME Copper'])
         ->column();

    foreach($copperPriceList as $price) {
        $cooperPrices[] = (int) price;
    }

     return $cooperPrices;
}

【讨论】:

  • 我明白了。感谢您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2018-10-30
相关资源
最近更新 更多