LaravelDaily / laravel-charts

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

Group by field #23

Closed tommiekn closed 4 years ago

tommiekn commented 4 years ago

Hi,

I am trying to create a chart that shows how many users are active in a log table, daily.

I have one table logs with an "user" column that refers to the user/id, trying to get the results like running a DISTINCT(user_id)

I was trying with something like this but it shows the total numbers of records for each day, not grouping it by user. (es. i have 100 transactions of 5 different users, now i get 100 but i would like to get 5).

Is it possible?

$chart_options_daily_users = [
            'chart_title' => 'Daily Users',
            'chart_type' => 'line',
            'report_type' => 'group_by_date',
            'model' => 'App\ApiLog',
            'conditions'            => [
                ['name' => 'Users', 'condition' => '', 'color' => 'red'],
            ],

            'relationship_name' => 'user',

            'group_by_field' => 'name',

            //'aggregate_function' => 'sum',
            //'aggregate_field' => 'user',

            'filter_field' => 'created_at',
            'filter_days' => 30, // show only transactions for last 30 days
            'filter_period' => 'month', // show only transactions for this week
            'continuous_time' => false, // show continuous timeline including dates without data
        ];
PovilasKorop commented 4 years ago

Not 100% sure, but try this:

    'report_type'        => 'group_by_relationship',
    'relationship_name'  => 'user',
    'group_by_field'     => 'name',
tommiekn commented 4 years ago

@PovilasKorop i have tried that already, it doesnt work cause group_by_relationship doesnt allow the grouping by date.

If i use your code i get a graph that shows the total number of requests for each user. I do need to show how many users have generated logs with a group by date concept.

So each day theres 2/3/4/5/x active users.

PovilasKorop commented 4 years ago

@tommiekn oh you want to group by user AND by date? No, we don't support that, sorry. Feel free to fork the package and add any functionality you need, our goal was to create a simple package for quick usage and basic use cases.

tommiekn commented 4 years ago

I have opened a pull request, its a very easy edit, updated the readme as well, feel free to explain it better:

https://github.com/LaravelDaily/laravel-charts/pull/25