Changing the x-axis data type of a graph? PHP/Laravel 5.6

Asked

Viewed 30 times

1

Good night,

I’m using a fast development tool called Quickadmin, accurate render a graph on the screen to the user who has on the x axis a string type data and on the y axis a time type data. The graph will be like this:

Gráfico

But Quickadmin only allows me to put data of the date/time type on the x axis, so I can only put year or hours there and not the sector name as it should. This is a graph generated by Quickadmin:

inserir a descrição da imagem aqui

Not being able to understand the logic to make this change in the code, does anyone know what I should change in Controller to take a string type data on this x-axis of the graph?

<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Treinamento;
use App\Turma;
use Carbon\Carbon; 

class ReportsController extends Controller
{
    public function horasTreinadasPorSetor(Request $request)
    {
         if ($request->has('date_filter')) { 
              $parts = explode(' - ' , $request->input('date_filter')); 
              $date_from = Carbon::createFromFormat(config('app.date_format'), $parts[0])->format('Y-m-d');
              $date_to = Carbon::createFromFormat(config('app.date_format'), $parts[1])->format('Y-m-d');
         } else { 
              $date_from = new Carbon('last Monday');
              $date_to = new Carbon('this Sunday');
         } 
        $reportTitle = 'Horas Treinadas por Setor';
        $reportLabel = 'AVG';
        $chartType   = 'bar';

        $results = Treinamento::where('created_at', '>=', $date_from)->where('created_at', '<=', $date_to)->get()->sortBy('created_at')->groupBy(function ($entry) {
            if ($entry->created_at instanceof \Carbon\Carbon) {
                return \Carbon\Carbon::parse($entry->created_at)->format('Y');
            }
            try {
               return \Carbon\Carbon::createFromFormat(config('app.date_format'), $entry->created_at)->format('Y');
            } catch (\Exception $e) {
                 return \Carbon\Carbon::createFromFormat(config('app.date_format') . ' H:i:s', $entry->created_at)->format('Y');
            }        })->map(function ($entries, $group) {
            return $entries->avg('id');
        });

        return view('admin.reports', compact('reportTitle', 'results', 'chartType', 'reportLabel'));
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.