95 lines
2.2 KiB
PHP
95 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\User\Resources\SpiceResource\Widgets;
|
|
|
|
use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
|
|
use Filament\Support\RawJs;
|
|
use App\Models\Spice;
|
|
|
|
class SpiceChart extends ApexChartWidget {
|
|
protected static ?string $chartId = 'spiceChart';
|
|
protected static ?string $heading = 'Rohstoffmengen im Lager';
|
|
|
|
protected function getOptions(): array {
|
|
$data = Spice::all();
|
|
|
|
$data_array = [];
|
|
$labels = [];
|
|
foreach ($data as $label) {
|
|
if ($label->unit == 'grams') {
|
|
$data_array[] = round($label->quantity / 1000, 2);
|
|
$labels[] = $label->name . ' (kg)';
|
|
} else {
|
|
$data_array[] = $label->quantity;
|
|
if ($label->unit == 'pieces') {
|
|
$labels[] = $label->name . ' (Stück)';
|
|
} elseif ($label->unit == 'liter') {
|
|
$labels[] = $label->name . ' (Liter)';
|
|
} else {
|
|
$labels[] = $label->name . ' (' . ucfirst($label->unit) . ')';
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
'chart' => [
|
|
'type' => 'bar',
|
|
'height' => 300,
|
|
],
|
|
'series' => [
|
|
[
|
|
'name' => 'BasicBarChart',
|
|
'data' => $data_array,
|
|
],
|
|
],
|
|
'xaxis' => [
|
|
'categories' => $labels,
|
|
'labels' => [
|
|
'style' => [
|
|
'fontFamily' => 'inherit',
|
|
],
|
|
],
|
|
],
|
|
'yaxis' => [
|
|
'labels' => [
|
|
'style' => [
|
|
'fontFamily' => 'inherit',
|
|
],
|
|
],
|
|
],
|
|
'colors' => ['#f59e0b'],
|
|
'plotOptions' => [
|
|
'bar' => [
|
|
'borderRadius' => 3,
|
|
'horizontal' => false,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function extraJsOptions(): ?RawJs {
|
|
return RawJs::make(<<<'JS'
|
|
{
|
|
xaxis: {
|
|
labels: {
|
|
formatter: function (val, timestamp, opts) {
|
|
return val.replace('( ', '(')
|
|
}
|
|
}
|
|
},
|
|
dataLabels: {
|
|
enabled: true,
|
|
formatter: function (val, opt) {
|
|
return val+''+opt.w.globals.labels[opt.dataPointIndex].match(/\((.*?)\)/)[1]
|
|
},
|
|
dropShadow: {
|
|
enabled: true
|
|
},
|
|
}
|
|
}
|
|
JS);
|
|
}
|
|
}
|