ConsoleTVs / Charts

[Deprecated] Charts for Laravel
MIT License
75 stars 35 forks source link

refresh chart labels when using API/Ajax [QUESTION] #16

Open mohcinsarrar opened 1 year ago

mohcinsarrar commented 1 year ago

i am using consoletvs/charts:6.* with laravel 9 i want to refresh labels and data when calling ajax request

mnluribef commented 1 year ago

Hi friend, did you find a solution?

sheko016 commented 1 year ago

Hello, good afternoon, how are you?

I have the same problem. I am using laravel chart by API/Ajax. But I have a problem. When loading for the first time, you see he shows me every month in the lebels. image

but after updating the graph. I only want to show in the lebels the months February March April and May image

I don't want to show every month it doesn't work for me that way. Is there a way to update the lebels.??

sheko016 commented 1 year ago

Studying the code a bit I could see that currently Laravel Chart does not update the Labels by API The file where I saw it was in https://github.com/ConsoleTVs/Charts/blob/main/src/Views/chartjs/script.blade.php

I add an image. image

I can request to be able to collaborate in the project to see if I can make it possible to update the labels. Greetings thank you

mnluribef commented 1 year ago

I came across this post: https://www.georgebuckingham.com/laravel-livewire-chart-js-realtime/ it uses ConsoleTVs/Charts as a base and some helper components to reload the graph with AJAX, i tried it and found out it gives me this error:

Attempt to read property "id" on null, file wan-speed-tests.blade.php, line 5 <div wire:ignore wire:key={{ $chart->id }}>

Any ideas on how to solve this?

sheko016 commented 1 year ago

Hi guys, I know how to fix it. I already know how to update the labels. I would like permission to upload the applied update. In the same way I leave here a print of the edited code.

Let's go first to the file: https://github.com/ConsoleTVs/Charts/blob/main/src/Classes/BaseChart.php image

and the code is like this. Annex Print original code

image

an if (count($this->labels) >= 1) {} is performed to ask if there are new labels if the old ones are not left.

Now in the file. https://github.com/ConsoleTVs/Charts/blob/main/src/Views/chartjs/script.blade.php

the code is like this. Annex Print original code image

Now the edited code looks like this. Annex Print edited code image

Now in line 9 the variable data is equal to data = data.datasets; and in the lines 36 37 38 39 update as the info comes

{{ $chart->id }}.data.datasets = data.datasets;
 if (data.labels) {
     {{ $chart->id }}.data.labels = data.labels;
 }

Now I show you how it looks. as I had it before you can see the screen prints in this comment

https://github.com/ConsoleTVs/Charts/issues/16#issuecomment-1664449354

And the new way is now.

Initial charge image

updating via ajax

image

It only brings me the labels that I want to show. I only wish that the account administrator could upload those changes, that would be very good.