src/Controller/HomeController.php line 22

  1. <?php
  2. namespace App\Controller;
  3. use DateTime;
  4. use Exception;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  12. use Symfony\UX\Chartjs\Model\Chart;
  13. class HomeController extends AbstractController
  14. {
  15.     /**
  16.      *
  17.      * @Route("/", name="home")
  18.      */
  19.     public function index(ChartBuilderInterface $chartBuilder): Response
  20.     {
  21.         $defaultColor 'rgb(206 211 217)';
  22.         $primaryColor 'rgb(0 181 200)';
  23.         $barData=[
  24.             'Song 1'=>32,
  25.             'Song 2'=>10,
  26.             'Song 3'=>5,
  27.             'Song 4'=>53,
  28.             'Song 5'=>39,
  29.             'Song 6'=>45,
  30.             'Song 7'=>33
  31.         ];
  32.         $values array_values($barData);
  33.         $barChartColours = [];
  34.         $max=0;
  35.         for ($i=0$iMax count($values); $i$iMax$i++) {
  36.             $barChartColours[$i] = $defaultColor;
  37.             if ($values[$i] > $values[$max]) {
  38.                 $max $i;
  39.             }
  40.         }
  41.         $barChartColours[$max] = $primaryColor;
  42.         $barChart $chartBuilder->createChart(Chart::TYPE_BAR);
  43.         $barChart->setData([
  44.             'labels' => array_keys($barData),
  45.             'datasets' => [
  46.                 [
  47.                     'label' => 'Listened',
  48.                     'backgroundColor' => $barChartColours,
  49.                     'data' => array_values($barData),
  50.                 ],
  51.             ],
  52.         ]);
  53.         $barChart->setOptions([
  54.             'scales' => [
  55.                 'y' => [
  56.                     'suggestedMin' => 0,
  57.                     'suggestedMax' => 100,
  58.                 ],
  59.             ],
  60.             'indexAxis' => 'y',
  61.             'plugins' => [
  62.                 'legend' => [
  63.                     'display' => false
  64.                 ]
  65.             ]
  66.         ]);
  67.         $donutChart $chartBuilder->createChart(Chart::TYPE_DOUGHNUT);
  68.         $donutChart->setData([
  69.             'labels' => ['Song 1''Song 2''Song 3''Song 4''Song 5'],
  70.             'datasets' => [
  71.                 [
  72.                     'label' => 'Listened',
  73.                     'backgroundColor' => [
  74.                         'rgb(255, 99, 132)',
  75.                         'rgb(54, 162, 235)',
  76.                         'rgb(134, 55, 182)',
  77.                         'rgb(255, 99, 132)',
  78.                         'rgb(255, 205, 86)',
  79.                     ],
  80.                     'data' => [3210203045],
  81.                     'hoverOffset' => 5
  82.                 ],
  83.             ],
  84.         ]);
  85.         $donutChart->setOptions([
  86.             'scales' => [
  87.                 'y' => [
  88.                     'suggestedMin' => 0,
  89.                     'suggestedMax' => 100,
  90.                 ],
  91.             ],
  92.             'plugins' => [
  93.                 /*'title' => [
  94.                     'display' => true,
  95.                 ],*/
  96.                 'legend' => [
  97.                     'display' => false
  98.                 ]
  99.             ]
  100.         ]);
  101.         return $this->render('home/index.html.twig',[
  102.             'initialDate' => new DateTime(),
  103.             'barChart' => $barChart,
  104.             'donutChart' => $donutChart,
  105.             'mainData' => [
  106.                 'sessions_a_day' => 0,
  107.                 'time_of_use' => 0,
  108.                 'completed_sessions' => 0,
  109.                 'previous_assessment' => 0,
  110.                 'late_assessment' => 0
  111.             ]
  112.         ]);
  113.     }
  114.     /**
  115.      *
  116.      * @Route("/data", name="home_data")
  117.      *
  118.      */
  119.     public function data(Request $request): Response
  120.     {
  121.         if (!$request->isXmlHttpRequest()) {
  122.             throw new NotFoundHttpException();
  123.         }
  124.         $dateRange $request->get('date-range');
  125.         [$startDateString$endDateString] = explode(' - '$dateRange);
  126.         try {
  127.             $startDate = new DateTime(str_replace('/','-',$startDateString));
  128.             $endDate = new DateTime(str_replace('/','-',$endDateString));
  129.         } catch (Exception $e) {
  130.             return new JsonResponse([],400);
  131.         }
  132.         if (in_array($request->get('country'),['all','es','cu','us'])) {
  133.             $country $request->get('country');
  134.         }
  135.         return new JsonResponse([
  136.             'sessions_a_day' => 5*$startDate->format('d'),
  137.             'time_of_use' => $startDate->format('d'),
  138.             'completed_sessions' => 2*$endDate->format('d'),
  139.             'previous_assessment' => 3.3,
  140.             'late_assessment' => 4.7
  141.         ]);
  142.     }
  143. }