LaravelDaily / laravel-charts

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

Condition in model #34

Closed Overx closed 4 years ago

Overx commented 4 years ago

How do I use conditions like where in the model? Thx

PovilasKorop commented 4 years ago

See in readme:

'conditions'            => [
        ['name' => 'Food', 'condition' => 'category_id = 1', 'color' => 'black'],
        ['name' => 'Transport', 'condition' => 'category_id = 2', 'color' => 'blue'],
    ],
Overx commented 4 years ago

Are fields such as name and color required?

PovilasKorop commented 4 years ago

@Overx no, optional

Overx commented 4 years ago
$chart_options = [
            'chart_title' => 'Clientes por mês',
            'report_type' => 'group_by_date',
            'model' => 'App\Models\User',
            'conditions' => [
                ['name' => 'Clientes', 'condition' => 'user_id = ' . auth()->user()->id, 'color' => 'black']
            ],
            'group_by_field' => 'created_at',
            'group_by_period' => 'month',
            'group_by_field_format' => 'd-m-Y',
            'chart_type' => 'bar',

        ];

        $chart1 = new LaravelChart($chart_options);

Not working for me

PovilasKorop commented 4 years ago

@Overx what is the error you're getting?

Overx commented 4 years ago

what is the error you're getting?

It does not present any error, just does not have the expected effect, I would like it to show the record of this user, and are showing all the records, it is not filtering with the condition where user_id

PovilasKorop commented 4 years ago

@Overx oh now I understood why it's not working, it works only with line chart, with purpose of having multiple lines on the same chart. With bar chart you're using, it won't work. Sorry, for bar we don't have where condition logic. But again, we are happy to accept a Pull Request if you build it.

gathuku commented 3 years ago

@PovilasKorop Thank you for this packge, looking at this issue is it possible to filter the data which get analyzed. For example, a Business has many Sales, can you be able to narrow down to specific business sales

'model' => Sales::where('business_id', 12)
PovilasKorop commented 3 years ago

@gathuku yes, it should be possible with where_raw parameter, look at readme or in the source how it works.

gathuku commented 3 years ago

@PovilasKorop where_raw worked fine. I have another issue where the chart isn't rendered dynamically with a livewire component. What am trying to achieve is I have a dropdown which populated the filter_days, on change should render a new chart depending on the selected value. My guess is that the javascript to render the chart isn't triggered.

PovilasKorop commented 3 years ago

@gathuku no idea, we haven't tried the package with Livewire component