Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChartBuilderService
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDashboardChart
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
1
 getStoreChart
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Service;
6
7use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
8use Symfony\UX\Chartjs\Model\Chart;
9
10class ChartBuilderService
11{
12    public function __construct(
13        private readonly ChartBuilderInterface $chartBuilder
14    ) {}
15
16    /**
17     * @param array<int, string> $labels
18     * @param array<int, float> $data
19     */
20    public function getDashboardChart(
21        string $title,
22        array $labels,
23        array $data
24    ): Chart
25    {
26        $bgColors = [
27            'rgba(255, 99, 132, 0.2)',
28            'rgba(54, 162, 235, 0.2)',
29            'rgba(255, 206, 86, 0.2)',
30            'rgba(75, 192, 192, 0.2)',
31            'rgba(153, 102, 255, 0.2)',
32            'rgba(255, 159, 64, 0.2)',
33            'rgba(255, 99, 132, 0.2)',
34            'rgba(54, 162, 235, 0.2)',
35            'rgba(255, 206, 86, 0.2)',
36        ];
37
38        $borderColors = [
39            'rgba(255,99,132,1)',
40            'rgba(54, 162, 235, 1)',
41            'rgba(255, 206, 86, 1)',
42            'rgba(75, 192, 192, 1)',
43            'rgba(153, 102, 255, 1)',
44            'rgba(255, 159, 64, 1)',
45            'rgba(255,99,132,1)',
46            'rgba(54, 162, 235, 1)',
47            'rgba(255, 206, 86, 1)',
48        ];
49
50        $chart = $this->chartBuilder->createChart(Chart::TYPE_BAR);
51        $chart->setData(
52            [
53                'labels' => $labels,
54                'datasets' => [
55                    [
56                        'label' => $title,
57                        'backgroundColor' => $bgColors,
58                        'borderColor' => $borderColors,
59                        'borderWidth' => 1,
60                        'data' => $data,
61                    ],
62                ],
63            ]
64        );
65
66        return $chart;
67    }
68
69    /**
70     * @param array<string> $labels
71     * @param array<int, float> $dataPayments
72     * @param array<int, float> $dataRent
73     */
74    public function getStoreChart(
75        array $labels,
76        array $dataPayments,
77        array $dataRent
78    ): Chart
79    {
80        $chart = $this->chartBuilder->createChart(Chart::TYPE_LINE);
81        $chart->setData(
82            [
83                'labels' => $labels,
84                'datasets' => [
85                    [
86                        'label' => 'Pagos',
87                        'data' => $dataPayments,
88                        'fill' => 'false',
89                        'lineTension' => 0.1,
90                        'backgroundColor' => 'rgba(75,192,192,0.4)',
91                        'borderColor' => 'rgba(75,192,192,1)',
92                    ],
93                    [
94                        'label' => 'Alquiler',
95                        'data' => $dataRent,
96                        'backgroundColor' => 'rgba(255, 206, 86, 0.2)',
97                        'borderColor' => 'rgba(255, 206, 86, 0.2)',
98                        'borderWidth' => 1,
99                    ],
100                ],
101            ]
102        );
103
104        return $chart;
105    }
106}