1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReportPlugin\Renderer; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusReportPlugin\DataFetcher\Data; |
8
|
|
|
use Odiseo\SyliusReportPlugin\Form\Type\Renderer\ChartConfigurationType; |
9
|
|
|
use Odiseo\SyliusReportPlugin\Model\ReportInterface; |
10
|
|
|
use Twig\Environment; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Mateusz Zalewski <[email protected]> |
14
|
|
|
* @author Łukasz Chruściel <[email protected]> |
15
|
|
|
* @author Diego D'amico <[email protected]> |
16
|
|
|
* @author Rimas Kudelis <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ChartRenderer implements RendererInterface |
19
|
|
|
{ |
20
|
|
|
const BAR_CHART = 'bar'; |
21
|
|
|
const LINE_CHART = 'line'; |
22
|
|
|
const RADAR_CHART = 'radar'; |
23
|
|
|
const POLAR_CHART = 'polar'; |
24
|
|
|
const PIE_CHART = 'pie'; |
25
|
|
|
const DOUGHNUT_CHART = 'doughnut'; |
26
|
|
|
|
27
|
|
|
private Environment $templating; |
28
|
|
|
|
29
|
|
|
public function __construct(Environment $templating) |
30
|
|
|
{ |
31
|
|
|
$this->templating = $templating; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function render(ReportInterface $report, Data $data): string |
35
|
|
|
{ |
36
|
|
|
if (null !== $data->getData()) { |
|
|
|
|
37
|
|
|
$rendererData = [ |
38
|
|
|
'report' => $report, |
39
|
|
|
'values' => $data->getData(), |
40
|
|
|
'labels' => $data->getLabels(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$rendererConfiguration = $report->getRendererConfiguration(); |
44
|
|
|
|
45
|
|
|
return $this->templating->render($rendererConfiguration['template'], [ |
46
|
|
|
'data' => $rendererData, |
47
|
|
|
'configuration' => $rendererConfiguration, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->templating->render('@OdiseoSyliusReportPlugin/noDataTemplate.html.twig', [ |
52
|
|
|
'report' => $report, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getType(): string |
57
|
|
|
{ |
58
|
|
|
return ChartConfigurationType::class; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function getChartTypes(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
'Bar chart' => self::BAR_CHART, |
65
|
|
|
'Line chart' => self::LINE_CHART, |
66
|
|
|
'Radar chart' => self::RADAR_CHART, |
67
|
|
|
'Polar chart' => self::POLAR_CHART, |
68
|
|
|
'Pie chart' => self::PIE_CHART, |
69
|
|
|
'Doughnut chart' => self::DOUGHNUT_CHART, |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|