我为我的项目创建了 data_log 图表小部件,在我的第一个代码中,我为每月交易量创建了此代码。在我的数据库中有用户、计量气体、日志。在metergas 列中,有serialNo、connectivity 和user_id。列日志中包含 id、condition、volume、type_io、battery 和metergas_id。所以,这是我的 LogAppChart.php(dailyMonth):
<?php
namespace App\Filament\Widgets;
use App\Models\Log;
use App\Models\Metergas;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Str; // To use the random color function
class LogAppChart extends ChartWidget
{
protected static ?string $heading = 'Volume PerMonth';
protected static ?string $pollingInterval = null;
protected static ?string $maxHeight = '300px';
protected int | string | array $columnSpan = 'full';
protected function getData(): array
{
$user_id = Auth::user()->id;
$metergas = Metergas::where('user_id', $user_id)
->get();
$data_log = [];
$period = CarbonPeriod::create(Carbon::now()->subMonth()->startOfDay(), '1 day', Carbon::now()->startOfDay());
$allDates = iterator_to_array($period);
$allDates = array_map(fn ($date) => $date->format('Y-m-d'), $allDates);
if ($metergas->isNotEmpty()) {
foreach ($metergas as $item) {
$logs = Log::where('metergas_id', $item->id)
->whereDate('created_at', '>=', Carbon::now()->subMonth())
->get()
->groupBy(function ($date) {
return Carbon::parse($date->created_at)->format('Y-m-d');
})
->map(function ($dayLogs) {
return $dayLogs->sum('volume');
});
$dailyData = [];
foreach ($allDates as $date) {
$dailyData[] = $logs->get($date, 0);
}
// Generate a random color for each dataset
$color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
$data_log[] = [
'label' => 'Metergas ' . $item->serialNo,
'data' => $dailyData,
'backgroundColor' => $color,
'borderColor' => $color,
];
}
}
return [
'datasets' => $data_log,
'labels' => $allDates,
];
}
protected function getType(): string
{
return 'line';
}
}
我使用类似的代码创建了另一个图表,但它是每日交易量。因此,它会计算每天每小时的流量。但是,代码图表小部件没有出现在我的面板中。 在此输入图片描述
<?php
namespace App\Filament\Widgets;
use App\Models\Log;
use App\Models\Metergas;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Str; // To use the random color function
class DayLogAppChart extends ChartWidget
{
protected static ?string $heading = 'Volume PerDay';
protected static ?string $pollingInterval = null;
protected static ?string $maxHeight = '300px';
protected int | string | array $columnSpan = 'full';
protected function getData(): array
{
$user_id = Auth::user()->id;
$metergas = Metergas::where('user_id', $user_id)
->get();
$data_log = [];
$period = CarbonPeriod::create(Carbon::now()->subDay()->startOfHour(), '1 hour', Carbon::now()->startOfHour());
$allHours = iterator_to_array($period);
$allHours = array_map(fn ($date) => $date->format('Y-m-d H:00'), $allHours);
if ($metergas->isNotEmpty()) {
foreach ($metergas as $item) {
$logs = Log::where('metergas_id', $item->id)
->whereDate('created_at', '>=', Carbon::now()->subDay())
->get()
->groupBy(function ($log) {
return Carbon::parse($log->created_at)->format('Y-m-d H:00');
})
->map(function ($hourLogs) {
return $hourLogs->sum('volume');
});
foreach ($allHours as $hourLabel) {
$hourlyData[] = $logs->get($hourLabel, 0);
}
// Generate a random color for each dataset
$color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
$data_log[] = [
'label' => 'Metergas ' . $item->serialNo,
'data' => $hourlyData,
'backgroundColor' => $color,
'borderColor' => $color,
];
}
}
// dd($data_log, $allHours); // Uncomment for debugging
return [
'datasets' => $data_log,
'labels' => $allHours,
];
}
protected function getType(): string
{
return 'line';
}
}
您可能尚未将这个新小部件添加到您的仪表板,在
app/Providers/Filament/AdminPanelProvider.php
中。
public function panel(Panel $panel): Panel
{
return $panel
// ...
->widgets([
// your Dashboard Widgets go here
]);
}