Passed
Branch master (c87ba8)
by Christian
16:02
created

BarChartWidget::renderWidgetContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Dashboard\Widgets;
19
20
use TYPO3\CMS\Fluid\View\StandaloneView;
21
22
/**
23
 * Concrete Bar Chart widget implementation
24
 *
25
 * Shows a widget with a bar chart. The data for this chart will be provided by the data provider you will set.
26
 * You can add a button to the widget by defining a button provider.
27
 *
28
 * There are no options available for this widget
29
 *
30
 * @see ChartDataProviderInterface
31
 * @see ButtonProviderInterface
32
 */
33
class BarChartWidget implements WidgetInterface, EventDataInterface, AdditionalCssInterface, RequireJsModuleInterface
34
{
35
    /**
36
     * @var WidgetConfigurationInterface
37
     */
38
    private $configuration;
39
40
    /**
41
     * @var ChartDataProviderInterface
42
     */
43
    private $dataProvider;
44
45
    /**
46
     * @var StandaloneView
47
     */
48
    private $view;
49
50
    /**
51
     * @var ButtonProviderInterface|null
52
     */
53
    private $buttonProvider;
54
55
    /**
56
     * @var array
57
     */
58
    private $options;
59
60
    public function __construct(
61
        WidgetConfigurationInterface $configuration,
62
        ChartDataProviderInterface $dataProvider,
63
        StandaloneView $view,
64
        $buttonProvider = null,
65
        array $options = []
66
    ) {
67
        $this->configuration = $configuration;
68
        $this->dataProvider = $dataProvider;
69
        $this->view = $view;
70
        $this->options = $options;
71
        $this->buttonProvider = $buttonProvider;
72
    }
73
74
    public function renderWidgetContent(): string
75
    {
76
        $this->view->setTemplate('Widget/ChartWidget');
77
        $this->view->assignMultiple([
78
            'button' => $this->buttonProvider,
79
            'options' => $this->options,
80
            'configuration' => $this->configuration,
81
        ]);
82
        return $this->view->render();
83
    }
84
85
    public function getEventData(): array
86
    {
87
        return [
88
            'graphConfig' => [
89
                'type' => 'bar',
90
                'options' => [
91
                    'maintainAspectRatio' => false,
92
                    'legend' => [
93
                        'display' => false,
94
                    ],
95
                    'scales' => [
96
                        'yAxes' => [
97
                            [
98
                                'ticks' => [
99
                                    'beginAtZero' => true,
100
                                ],
101
                            ],
102
                        ],
103
                        'xAxes' => [
104
                            [
105
                                'ticks' => [
106
                                    'maxTicksLimit' => 15,
107
                                ],
108
                            ],
109
                        ],
110
                    ],
111
                ],
112
                'data' => $this->dataProvider->getChartData(),
113
            ],
114
        ];
115
    }
116
117
    public function getCssFiles(): array
118
    {
119
        return ['EXT:dashboard/Resources/Public/Css/Contrib/chart.css'];
120
    }
121
122
    public function getRequireJsModules(): array
123
    {
124
        return [
125
            'TYPO3/CMS/Dashboard/Contrib/chartjs',
126
            'TYPO3/CMS/Dashboard/ChartInitializer',
127
        ];
128
    }
129
}
130