None of this is production code, but me trying to figure out how to work multiple datasets into a Laravel project with chartjs. Here's my table schema
Schema::create('statistics', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('type');
$table->string('table');
$table->integer('count');
$table->timestamps();
});
Controller
public function grabStats($type = null, $table = null)
{
$first = strtotime('first day this month');
$months = [];
$countMonths = date('n');
for ($i = $countMonths - 1; $i >= 0; $i--) {
array_push($months, date('F', strtotime("-$i month", $first)));
}
for ($i = 0; $i <= 2; $i++) {
$years[] = date("Y", strtotime(date('Y-m-01') . " -$i years"));
}
$output['labels'] = $months;
$colors = [
[
'fillcolor' => 'rgba(114,131,153,0.2)',
'strokecolor' => 'rgba(114,131,153,1)',
],
[
'fillcolor' => 'rgba(151,187,205,0.2)',
'strokecolor' => 'rgba(151,187,205,1)',
],
[
'fillcolor' => 'rgba(220,220,220,0.2)',
'strokecolor' => 'rgba(220,220,220,1)',
],
];
$i = 0;
foreach ($years as $year) {
$array = [];
$array['label'] = $year;
$array['fillColor'] = $colors[$i]['fillcolor'];
$array['strokeColor'] = $colors[$i]['strokecolor'];
$array['pointColor'] = $colors[$i]['strokecolor'];
$array['pointStrokeColor'] = '#fff';
$array['pointHighlightFill'] = '#fff';
$array['pointHighlightStroke'] = $colors[$i]['strokecolor'];
$i++;
$data = $this->statistic->yearlyStats($type, $table, $year, $countMonths);
foreach ($data as $set) {
$array['data'][] = $set['count'];
}
if (!empty($array['data'])) {
$output['datasets'][] = $array;
}
}
return json_encode($output);
}
Output from months[]
array:4 [▼
0 => "January"
1 => "February"
2 => "March"
3 => "April"
]
Output from years[]
array:3 [▼
0 => "2016"
1 => "2015"
2 => "2014"
]
Repository
public function yearlyStats($type = null, $table = null, $year, $months)
{
if($year != date('Y')) {
$subtract = date('Y') - $year;
$start = Carbon::now()->subYears($subtract);
$end = Carbon::now()->subYears($subtract)->subMonths($months);
}else{
$start = Carbon::now();
$end = Carbon::now()->subMonths($months);
}
if($type)
{
return $this->statistic->select('count')->where('type','=',$type)->whereYear('created_at', '=', $year)->whereBetween('created_at', [$end, $start])->get();
}
return $this->statistic->select('count')->where('table','=',$table)->whereYear('created_at', '=', $year)->whereBetween('created_at', [$end, $start])->get();
}
Output before json encode
array:2 [▼
"labels" => array:4 [▼
0 => "January"
1 => "February"
2 => "March"
3 => "April"
]
"datasets" => array:2 [▼
0 => array:8 [▼
"label" => "2016"
"fillColor" => "rgba(114,131,153,0.2)"
"strokeColor" => "rgba(114,131,153,1)"
"pointColor" => "rgba(114,131,153,1)"
"pointStrokeColor" => "#fff"
"pointHighlightFill" => "#fff"
"pointHighlightStroke" => "rgba(114,131,153,1)"
"data" => array:4 [▼
0 => 45
1 => 2
2 => 19
3 => 19
]
]
1 => array:8 [▼
"label" => "2015"
"fillColor" => "rgba(151,187,205,0.2)"
"strokeColor" => "rgba(151,187,205,1)"
"pointColor" => "rgba(151,187,205,1)"
"pointStrokeColor" => "#fff"
"pointHighlightFill" => "#fff"
"pointHighlightStroke" => "rgba(151,187,205,1)"
"data" => array:4 [▼
0 => 45
1 => 21
2 => 9
3 => 29
]
]
]
]