![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/dceprojects.corals.io/Corals/modules/Timesheet/Widgets/ |
<?php namespace Corals\Modules\Timesheet\Widgets; class ProjectHoursChart extends BasePieChart { public function __construct() { parent::__construct(); $this->type('pie'); $this->options([ 'maintainAspectRatio' => false, 'scales' => [ 'yAxes' => [ 'display' => false ], ], 'legend' => [ 'display' => false, ], 'title' => [ 'display' => true, 'text' => 'Jobs Hours' ] ]); } public function run($args): string { $model = $args['model']; $labelAttribute = $args['label_attribute']; $entrieables = $model::with(['entries' => function ($query) use ($args) { $query->when(isset($args['start_date']) && isset($args['end_date']), function ($query) use ($args) { $query->when(isset($args['start_date']) && isset($args['end_date']), function ($query) use ($args) { $query->where(function ($query) use ($args) { $query->whereDate("ts_entries.spent_at", '>=', $args['start_date']) ->whereDate("ts_entries.spent_at", '<=', $args['end_date']); }); }); }); }])->get(); $entrieableHours = $entrieables->map(function ($entrieable) use ($labelAttribute) { return [ 'label' => $entrieable->{$labelAttribute}, 'hours' => (($entrieable->entries->sum('hours') * 60) + $entrieable->entries->sum('minutes')) / 60 ]; })->sortByDesc('hours')->toArray(); $labels = array_column($entrieableHours, 'label'); $hours = array_column($entrieableHours, 'hours'); $colors = array_map(function () { return sprintf('#%06X', mt_rand(0, 0xFFFFFF)); }, $labels); $this->setData($labels, $hours)->backgroundColor($this->generateRandomColors(count($hours))); $legendPosition = $args['legend_position'] ?? static::$defaultLegendPosition; $chartHtml = view('Corals::chart')->with(['chart' => $this])->render(); $legendHtml = $this->generateCustomLegend($labels, $hours, $colors); return $this->wrapChartWithLegend($chartHtml, $legendHtml, $legendPosition); } }