Как сделать Laravel Графики - PullRequest
1 голос
/ 18 марта 2020

Я работаю над проектом, и я использовал Laravel Библиотека диаграмм (https://charts.erik.cat/) для создания диаграммы, где пользователь выбирает дату от и до, и из этого диапазона дат генерирует диаграмму. Мне нужно, чтобы из выбранного пользователем диапазона дат (глядя на поле базы данных op_date) было сгенерировано, сколько раз использовались OBD, D C, BSL (это данные поля базы данных "con_type") и сгенерирована диаграмма. Мой код:

namespace App\Http\Controllers;

use App\Content;
use App\Charts\UserLineChart;

use Illuminate\Http\Request;

class ChartController extends Controller
{

  /*
    * vei_sn database field, this is like user ID
    * date_from - HTML input field Date from
    * date_to - HTML input field Date to

  */
  public function index(Request $request)
  {
    $content = Content::where('vei_sn', '23333');
    if ($request->has('date_from') && $request->input('date_from') != '' && $request->has('date_to') && $request->input('date_to') != '') {
        $datefrom = $request->input('date_from');
        $dateto = $request->input('date_to');
        $content = $content->where(function($query) use ($datefrom, $dateto){
                                   $query->whereBetween('op_date',array($datefrom, $dateto));
                                 });
    }
      $OBD = $content->where('con_type', 'OBD')->count();
      $DC = $content->where('con_type', 'DC')->count();
      $BSL = $content->where('con_type', 'BSL')->count();
      $content = $content->pluck('con_type', 'con_type');


      $chart = new UserLineChart;
      $chart->labels($content->values());

      /*
        * Here we enter the values of what will be labels, for example OBD will be 24
      */
      $chart->dataset('Connection types', 'bar', [$OBD, $DC, $BSL])->backgroundColor([
         'red',
         'green',
         'yellow'
      ]);
      return view('chart.index', compact('chart'));
  }
}

Схема базы данных:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contents', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('file_id');
            $table->integer('vei_sn');
            $table->dateTime('op_full_date');
            $table->date('op_date');
            $table->time('op_time');
            $table->string('con_type');
            // operation
            $table->string('operation');
            $table->string('ecu');
            $table->string('ecu_sub')->nullable();
            $table->string('ecu_sub2')->nullable();
            $table->integer('tokens')->nullable();
            $table->integer('tokens_left')->nullable();
            $table->timestamps();

            $table->foreign('file_id')
                  ->references('id')->on('files')
                  ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contents');
    }
}

1 Ответ

0 голосов
/ 18 марта 2020

Кому интересно, я нашел решение. Вот код:

<?php

namespace App\Http\Controllers;

use App\Content;
use App\Charts\UserLineChart;

use Illuminate\Http\Request;

class ChartController extends Controller
{

  public function index(Request $request)
  {
    $content = Content::where('vei_sn', '23333')->get();
    if ($request->has('date_from') && $request->input('date_from') != '' && $request->has('date_to') && $request->input('date_to') != '') {
        $datefrom = $request->input('date_from');
        $dateto = $request->input('date_to');
        // $content = $content->where(function($query) use ($datefrom, $dateto){
        //                            $query->whereBetween('op_date',array($datefrom, $dateto));
        //                          });
        $content = $content->whereBetween('op_date', [$datefrom, $dateto]);
    }

      $OBD = $content->where('con_type', 'OBD')->count();
      $DC = $content->where('con_type', 'DC')->count();
      $BSL = $content->where('con_type', 'BSL')->count();
      $content = $content->pluck('con_type', 'con_type');

      $chart = new UserLineChart;

      $chart->labels($content->values());

      $chart->dataset('Connections', 'bar', [$OBD, $DC, $BSL])->backgroundColor([
         'red',
         'green',
         'yellow'
      ]);
      return view('chart.index', compact('chart'));
  }
}

...