marineusde / larapex-charts

A Laravel wrapper for apex charts
https://github.com/marineusde/larapex-charts
MIT License
7 stars 3 forks source link

Reload chart data asynchronously #30

Closed coirpower closed 1 month ago

coirpower commented 2 months ago

I want to create a page with a bar chart, and a dropdown in which selecting an option should asynchronously reload the bar chart.

Try 1:

$this->emit('chartUpdated', 'machine-monthly-chart', $this->chart);

I need to pass chart as an array to this code but there is no toArray() method in LarapexChart! Then I tried Livewire to handle the asynchronous updates without relying on toArray().

Try 2:

$this->chartData = $chart->getOptions(); // Get options to pass to JavaScript

which resulted in getOptions() not available error. So, instead of using getOptions(), I directly used the configuration methods available in Larapex Charts and then tried to convert it to JSON in the view.

Try 3:

    // Convert chart configuration to JSON
     $this->chartConfig = $chart->toJson();

    // In Blade View: use JS to initialize and update the chart with the config passed from Livewire component
    <script>
        document.addEventListener('livewire:load', function () {
            let chartOneConfig = @json($chartConfig);

            let chartOne = new ApexCharts(document.querySelector("#machine-monthly-chart"), chartOneConfig);
            chartOne.render();

            Livewire.on('chartOneUpdated', function (data) {
                chartOne.updateOptions(data);
            });
        });
    </script>

which resulted in the error: Property type not supported in Livewire for property: [{"headers":{},"original": {"id":"....

Any help?

marineusde commented 2 months ago

You can try to use the chart in another Livewire component and use polling:

https://livewire.laravel.com/docs/wire-poll

Do you use Livewire 2 or 3?