我正在尝试制作一个 Laravel 应用程序,其中线图显示与特定日期相对应的登录用户的血糖水平。我目前正在使用 ConsoleTV 软件包,但对其他选项持开放态度。
我尝试了几个不同的查询,但我似乎无法让它准确地显示我想要的内容。 Y 轴上不显示血糖水平,而是只计算当天的记录数量。
我的 Controller :
public function chart()
{
// $bloodSugar = BloodSugar::where(\DB::raw("(DATE_FORMAT(created_at,'%D'))"),date('D'))->where('user_id', Auth::id())->get();
$bloodSugar = BloodSugar::where('user_id', Auth::id())->get();
$chart = Charts::database($bloodSugar, 'line', 'highcharts')
->title('Blood Sugars')
->elementLabel('Blood Sugar Level')
->groupByDay();
return view ('bloodsugars.index', compact('chart'));
}
我的用户模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function reviews()
{
return $this->hasMany(BloodSugar::class);
}
}
我的血糖模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BloodSugar extends Model
{
protected $fillable = ['date', 'time', 'blood_sugar_level', 'scenario', 'user_id'];
public function user()
{
return $this->belongsTo(User::class);
}
}
我的用户数据库表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->nullable();
$table->date('dob')->nullable();
$table->string('diabetic_type')->nullable();
$table->string('other_info')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('provider')->nullable();
$table->string('provider_id')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
我的血糖表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBloodSugarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blood_sugars', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->time('time');
$table->double('blood_sugar_level');
$table->text('scenario');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blood_sugars');
}
}
我的图表当前输出当天记录的血糖数量,而不是实际的血糖水平本身。
最佳答案
当您使用的库版本已过时且与当前文档不匹配时,很难提供帮助。我能做的就是根据当前文档向您指出解决方案。
查看 Eloquent example ,因为这就是我正在修改的内容,以便为您提供这些示例。这些将插入 Controller 方法的中间。下面,我们简单地给出了一天中每个时间读数的折线图。
$chart = new SampleChart;
$chart->labels($bloodSugar->map(function (Bloodsugar $reading) {
return Carbon::parse($reading->date . ' ' . $reading->time);
}));
$chart->dataset('Bloodsugar Readings', 'line', $bloodSugar->pluck('blood_sugar_level'));
如果您想报告每天的平均血糖,那么您只需使用 Collection
方法对数据进行一些转换,然后再将其传递到图表库即可:
// Keys are dates and values are average levels for those dates.
$averageBloodSugarByDay = $bloodSugar->groupBy('date')
->map(function (Bloodsugar $readingsForDate) {
return $readingsForDate->avg('blood_sugar_level');
});
$chart = new SampleChart;
$chart->labels($averageBloodSugarByDay->keys());
$chart->dataset('Bloodsugar Readings', 'line', $averageBloodSugarByDay->values());
请参阅Laravel Collection如果您不清楚这些片段中发生的任何事情,请参阅文档。
关于mysql - 如何在 Laravel 中显示使用同一数据库表中两个不同值的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853127/