2022-09-29 03:48:58 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
|
2022-09-29 13:20:58 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
/**
|
2022-09-29 03:48:58 -07:00
|
|
|
* Class PieChartTransformer
|
|
|
|
*
|
|
|
|
* This handles the standardized formatting of the API response we need to provide for
|
|
|
|
* the pie charts
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
2022-09-29 13:20:58 -07:00
|
|
|
* @since [v6.0.11]
|
2022-09-29 03:48:58 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
*/
|
|
|
|
class PieChartTransformer
|
|
|
|
{
|
|
|
|
public function transformPieChartDate($totals)
|
|
|
|
{
|
|
|
|
|
|
|
|
$labels = [];
|
|
|
|
$counts = [];
|
|
|
|
$default_color_count = 0;
|
|
|
|
$colors_array = [];
|
|
|
|
|
|
|
|
foreach ($totals as $total) {
|
|
|
|
|
|
|
|
if ($total['count'] > 0) {
|
|
|
|
|
2022-09-29 13:20:58 -07:00
|
|
|
$labels[] = $total['label']." (".$total['count'].")";
|
2022-09-29 03:48:58 -07:00
|
|
|
$counts[] = $total['count'];
|
|
|
|
|
|
|
|
if (isset($total['color'])) {
|
|
|
|
$colors_array[] = $total['color'];
|
|
|
|
} else {
|
|
|
|
$colors_array[] = Helper::defaultChartColors($default_color_count);
|
|
|
|
$default_color_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [
|
|
|
|
'labels' => $labels,
|
|
|
|
'datasets' => [[
|
|
|
|
'data' => $counts,
|
|
|
|
'backgroundColor' => $colors_array,
|
|
|
|
'hoverBackgroundColor' => $colors_array,
|
|
|
|
]],
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
}
|