Fix SpiceChart: use class property for spice IDs, build edit URLs via url()
Deploy / deploy (push) Successful in 15s
Deploy / deploy (push) Successful in 15s
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
092a78e306
commit
7d45e3027c
1 file changed
+86
-93
@@ -6,103 +6,96 @@ use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
|
|||||||
use Filament\Support\RawJs;
|
use Filament\Support\RawJs;
|
||||||
use App\Models\Spice;
|
use App\Models\Spice;
|
||||||
|
|
||||||
class SpiceChart extends ApexChartWidget {
|
class SpiceChart extends ApexChartWidget
|
||||||
protected static ?string $chartId = 'spiceChart';
|
{
|
||||||
protected static ?string $heading = 'Rohstoffmengen im Lager';
|
protected static ?string $chartId = 'spiceChart';
|
||||||
|
protected static ?string $heading = 'Rohstoffmengen im Lager';
|
||||||
|
|
||||||
protected function getOptions(): array {
|
private array $spiceIds = [];
|
||||||
$data = Spice::all();
|
|
||||||
|
|
||||||
$data_array = [];
|
protected function getOptions(): 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 {
|
|
||||||
$urls = \App\Models\Spice::all()
|
|
||||||
->map(fn ($s) => \App\Filament\User\Resources\SpiceResource::getUrl('edit', ['record' => $s->id]))
|
|
||||||
->values()
|
|
||||||
->toJson();
|
|
||||||
|
|
||||||
return RawJs::make(<<<JS
|
|
||||||
{
|
{
|
||||||
chart: {
|
$spices = Spice::all();
|
||||||
events: {
|
|
||||||
dataPointSelection: function(event, chartContext, config) {
|
$data = [];
|
||||||
var urls = {$urls};
|
$labels = [];
|
||||||
var url = urls[config.dataPointIndex];
|
|
||||||
if (url) window.location.href = url;
|
foreach ($spices as $spice) {
|
||||||
}
|
$this->spiceIds[] = $spice->id;
|
||||||
}
|
|
||||||
},
|
if ($spice->unit === 'grams') {
|
||||||
xaxis: {
|
$data[] = round($spice->quantity / 1000, 2);
|
||||||
labels: {
|
$labels[] = $spice->name . ' (kg)';
|
||||||
formatter: function (val) {
|
} elseif ($spice->unit === 'pieces') {
|
||||||
return val.replace('( ', '(')
|
$data[] = $spice->quantity;
|
||||||
}
|
$labels[] = $spice->name . ' (Stück)';
|
||||||
}
|
} elseif ($spice->unit === 'liter') {
|
||||||
},
|
$data[] = $spice->quantity;
|
||||||
dataLabels: {
|
$labels[] = $spice->name . ' (Liter)';
|
||||||
enabled: true,
|
} else {
|
||||||
formatter: function (val, opt) {
|
$data[] = $spice->quantity;
|
||||||
return val+''+opt.w.globals.labels[opt.dataPointIndex].match(/\((.*?)\)/)[1]
|
$labels[] = $spice->name . ' (' . ucfirst($spice->unit) . ')';
|
||||||
},
|
|
||||||
dropShadow: {
|
|
||||||
enabled: true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'chart' => [
|
||||||
|
'type' => 'bar',
|
||||||
|
'height' => 300,
|
||||||
|
],
|
||||||
|
'series' => [
|
||||||
|
[
|
||||||
|
'name' => 'Bestand',
|
||||||
|
'data' => $data,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'xaxis' => [
|
||||||
|
'categories' => $labels,
|
||||||
|
'labels' => ['style' => ['fontFamily' => 'inherit']],
|
||||||
|
],
|
||||||
|
'yaxis' => [
|
||||||
|
'labels' => ['style' => ['fontFamily' => 'inherit']],
|
||||||
|
],
|
||||||
|
'colors' => ['#f59e0b'],
|
||||||
|
'plotOptions' => [
|
||||||
|
'bar' => ['borderRadius' => 3, 'horizontal' => false],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function extraJsOptions(): ?RawJs
|
||||||
|
{
|
||||||
|
$urls = collect($this->spiceIds)
|
||||||
|
->map(fn ($id) => url('/dashboard/rohstoffe/' . $id . '/edit'))
|
||||||
|
->values()
|
||||||
|
->toJson();
|
||||||
|
|
||||||
|
return RawJs::make(<<<JS
|
||||||
|
{
|
||||||
|
chart: {
|
||||||
|
events: {
|
||||||
|
dataPointSelection: function(event, chartContext, config) {
|
||||||
|
var urls = {$urls};
|
||||||
|
var url = urls[config.dataPointIndex];
|
||||||
|
if (url) window.location.href = url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
xaxis: {
|
||||||
|
labels: {
|
||||||
|
formatter: function (val) {
|
||||||
|
return val.replace('( ', '(');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dataLabels: {
|
||||||
|
enabled: true,
|
||||||
|
formatter: function (val, opt) {
|
||||||
|
return val + '' + opt.w.globals.labels[opt.dataPointIndex].match(/\((.*?)\)/)[1];
|
||||||
|
},
|
||||||
|
dropShadow: { enabled: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JS);
|
||||||
}
|
}
|
||||||
JS);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user