OctoberCMS - Graphical record as track the data in a model

69 views Asked by At

so I have a model named user where all the user data is there. Is it possible to do a graph(?) as a track whenever there are new user register? It can be based on the month or year so it will be easier for me to track the data record. Can anyone give me any hint or guide on this?

1

There are 1 answers

0
Hardik Satasiya On

You need to arrange your data correctly, just follow below code to arrange your data.

// we will show last 7 (6 day + 1 current day) day's stats
$sixDayAgo = \Carbon\Carbon::now()->subDay(6);
$users = RainLab\User\Models\User::whereDate('created_at', '>=', 
                                      $sixDayAgo->toDateString())->get();

$period = \Carbon\CarbonPeriod::create($sixDayAgo, \Carbon\Carbon::now());

$chartData = [];
foreach ($period as $date) {
    $val = ['label' => $date->format('Y-m-d'), 'value' => 0];
    foreach ($users as $user) {
      if($date->format('Y-m-d') == $user->created_at->format('Y-m-d')) {
        $val['value']++;
      }
    }
    $chartData[] = $val;
}
// dd($chartData);

once you get your $chartData then you can show chart

follow https://tutorialmeta.com/october-cms/how-to-use-charts-in-octobercms this tutorial to show $chartData as chart.

Result

enter image description here

if any doubts please comment.