LaravelDaily / laravel-charts

Package to draw charts in Laravel with Chart.js
MIT License
522 stars 116 forks source link

Add function to return datasets #86

Closed mitulkoradiya closed 2 years ago

mitulkoradiya commented 2 years ago

Add function to return datasets for the next ajax request to update data in the graph.

PovilasKorop commented 2 years ago

@mitulkoradiya thanks for the contribution!

But I don't really understand the use-case. What do you mean by "next ajax request", can you provide the code example of how you would use such a method?

mitulkoradiya commented 2 years ago
Screenshot 2022-01-13 at 6 51 38 PM

The graph data is updated on filter change

  1. Backend Ajax request code
    
    ...
    $options = [
    'chart_title' => 'Company',
    'chart_type' => 'line',
    'report_type' => 'group_by_date',
    'model' => Item::class,
    'group_by_field' => 'created_at',
    'group_by_period' => $filter,
    'filter_days' => 50,
    'chart_color' => '160,208,245'
    ];

$companyChart = new LaravelChart($options); return response()->json($companyChart->getDatasets()); ...


2. Front-end javascript code

{!! $companyChart->renderChartJsLibrary() !!} {!! $companyChart->renderJs() !!}

PovilasKorop commented 2 years ago

@mitulkoradiya I've never seen anyone using this package like this, but ok, fair enough, it doesn't break anything, so I will merge the change.