gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:01:49 25 4
gpt4 key购买 nike

其实我想在我的 Laravel 网络应用程序中添加动态图表
对数据库中的变化使用react..我正在使用 lavacharts..我能够创建图表来处理我通过我的代码输入的数据。

这是我的 Controller

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

}}

最佳答案

好的,我看到这是一个很老的问题,但如果仍然需要这个,请继续......

从我在这里看到的情况来看,您需要从数据库中获取三样东西; day_of_month , projected_valueofficial_value .

假设您在数据库中有一个表,它映射到一个 eloquent 模型 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);

}

有了这个,您的图表就充满了数据库中的数据。

希望这可以帮助

关于charts - 如何使用 lavacharts 和 laravel 创建动态图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38610500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com