如何使用lavacharts和laravel创建动态图表?

时间:2016-07-27 10:33:07

标签: charts laravel-5

实际上我想在我的laravel web应用程序中添加动态图表 这会对数据库中的变化做出反应。我正在使用lavacharts ..我能够创建处理我通过代码输入的数据的图表。

这是我的控制器

   <?php
   namespace App\Http\Controllers;
   use Illuminate\Http\Request;
   use App\Http\Requests;
   use DB;
   use App\Http\Controllers\Controller;
   use Khill\Lavacharts\Laravel\LavachartsFacade as Lava;

   class ratingscontroller extends Controller{

   public function index(){


      $stocktable = \Lava::DataTable();

      $stocktable->addDateColumn('Day of Month')
           ->addNumberColumn('projected')
           ->addNumberColumn('official');


           for($a = 1; $a < 30; $a++){
            $stocktable->addRow([
                '2015-10-' . $a, rand(800,1000), rand(800,1000)


                ]);


           }

              $chart = \Lava::AreaChart('MyStocks', $stocktable);
                           return view('welcome');

          }}

1 个答案:

答案 0 :(得分:0)

好吧,我认为这是一个很老的问题,但是如果仍然需要这样做的话……

根据我在这里看到的,您将需要数据库中的三件事; day_of_monthprojected_valueofficial_value

让我们说您在数据库中有一个表,该表映射到雄辩的模型Report

您现在可以按照以下步骤创建DataTable对象:

$stocktable = \Lava::DataTable();

$stocktable->addDateColumn('Day of Month')
           ->addNumberColumn('projected')
           ->addNumberColumn('official');
//$reports should be validated with number of days in month (ex. Feb has 28 days so you would have empty days and Jan has 31 so info for one day would be missing)
//for simplicities sake i won't do this here...
$reports = Report::whereBetween('day_of_month',[1,30])->get();

for($a = 1; $a < 30; $a++){
    //I'm assuming here you only have one entry in your database per day of the month, if not you need to take this into account
    $day = $reports->where('day_of_month')->first();
    if($day){
        $stocktable->addRow([
            '2015-10-' . $a, 
            $day->projected_value, 
            $day->official_value 
        ]);
    }

\Lava::AreaChart('MyStocks', $stocktable);

}

通过此操作,图形将被数据库中的数据填充。

希望这会有所帮助