leandrocfe / filament-apex-charts

Apex Charts integration for Filament PHP.
https://filament-apex-charts-demo.leandroferreira.dev.br
MIT License
286 stars 36 forks source link

Change on single filter doesnt update #39

Closed sairiz closed 6 months ago

sairiz commented 11 months ago

Hi,

I copied a working graph from v2 in filament v2 to current version 3.0.1 in Filament 3.027

Upon selecting single filter, there is no update request.

public ?string $filter = '2023';

/**
 * Filter Options
 */
protected function getFilters(): ?array
{
    $years = range(now()->year, 2015);

    foreach ($years as $year) {
        $newYears[$year] = $year;
    }

    return $newYears;
}

/**
 * Chart options (series, labels, types, size, animations...)
 * https://apexcharts.com/docs/options
 *
 * @return array
 */
protected function getOptions(): array
{
    $activeFilter = $this->filter;

    $data = Trend::query(Stat::where('type', 'sales')->where('system', 'MY'))
        ->between(
            start: Carbon::create($activeFilter)->startOfYear(),
            end: Carbon::create($activeFilter)->endOfYear(),
        )
        ->perMonth()
        ->sum('data');

    return [

        'chart' => [
            'type' => 'line',
            'height' => 300,
            'toolbar' => [
                'show' => false,
            ],
        ],
        'series' => [
            [
                'name' => 'RM',
                'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
                'type' => 'column',
            ],
        ],
        'stroke' => [
            'width' => [0, 4],
            'curve' => 'smooth',
        ],
        'xaxis' => [
            'categories' => $data->map(fn (TrendValue $value) => Carbon::parse($value->date)->format('M')),
            'labels' => [
                'style' => [
                    'colors' => '#9ca3af',
                    'fontWeight' => 600,
                ],
            ],
        ],
        'yaxis' => [
            'labels' => [
                'style' => [
                    'colors' => '#9ca3af',
                    'fontWeight' => 600,
                ],
            ],
        ],
        'legend' => [
            'labels' => [
                'colors' => '#9ca3af',
                'fontWeight' => 600,
            ],
        ],
        'colors' => ['#6366f1', '#38bdf8'],
        'fill' => [
            'type' => 'gradient',
            'gradient' => [
                'shade' => 'dark',
                'type' => 'vertical',
                'shadeIntensity' => 0.5,
                'gradientToColors' => ['#d946ef'],
                'inverseColors' => true,
                'opacityFrom' => 1,
                'opacityTo' => 1,
                'stops' => [0, 100],
            ],
        ],
        'plotOptions' => [
            'bar' => [
                'borderRadius' => 10,
            ],
        ],
    ];
}

public function data($i)
{
    $stat = new stat;

    return $stat->select('data')
        ->whereBetween('created_at', [now()->firstOfYear()->addMonths($i - 1)->toDateString(), now()->firstOfYear()->addMonths($i - 1)->endOfMonth()->toDateString()])
        ->sum('data');
}

The documentation were just being updated with adding reactive if using getFormSchema().

Is this a bug or something i am missing when using getFilters()

margarizaldi commented 8 months ago

Since it was too long for this issue to be fixed, I'm afraid that my PR #48 will also take a long time to be reviewed. I suggest you or everyone to publish the view and modify like the PR until it is fixed in the future release.

leandrocfe commented 6 months ago

Please check the new version and open a new issue if it was necessary.