YII2 使用Echart 简易demo

本文详细介绍如何在Yii2框架中集成ECharts图表库,并通过一个具体案例展示数据的动态加载与图表渲染的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步  下载第三方包

使用compose下载 "bower-asset/echarts": "2.2.1";

以及php插件composer require "hisune/echarts-php"

第二步 创建asset bunnle类。

  ①

namespace app\assets;

use yii\web\AssetBundle;

class EchartsAsset extends AssetBundle
{
    public $sourcePath = '@bower/echarts/dist';
    public $js = [
		'echarts.js',
    ];
}

第三步 控制器传递数据

 

//学校经费使用情况
$fees = Fee::find()->asArray()->all();
$typename = ArrayHelper::getColumn($fees,'name');
$typeamount = ArrayHelper::getColumn($fees,'amount');
$typeusedamount = ArrayHelper::getColumn($fees,'userAmount');
return $this->render('index', [             
                'typename' => $typename,
                'typeamount' => $typeamount,
                'typeusedamount' => $typeusedamount
            ]);

第四步 view视图渲染

 

<?php
use Hisune\EchartsPHP\ECharts;
?>
<div id="chart1">
<?php
    $chart = new ECharts();
    $chart->title->text = '学校下批各类经费使用情况';
    $chart->title->left= 'center';
    $chart->tooltip->show = true;
    $chart->legend->data[] = '总金额';
    $chart->legend->data[] = '使用金额';
    $chart->legend->left= 'right';
    $chart->xAxis[] = array(
        'type' => 'category',
        'data' => $typename
    );
    $chart->yAxis[] = array(
        'type' => 'value'
    );
    $chart->series[] = array(
        'name' => '总金额',
        'type' => 'bar',
        'data' => $typeamount

    );
    $chart->series[] = array(
        'name' => '使用金额',
        'type' => 'bar',
        'data' => $typeusedamount

    );
    echo $chart->render('simple-custom-1');

?>
</div>


 

 

 

 

 

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值