/
LineChartDataProvider.php
98 lines (84 loc) · 3.1 KB
/
LineChartDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Xima\XmGoaccess\Widgets\Provider;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
class LineChartDataProvider extends AbstractGoaccessDataProvider implements ChartDataProviderInterface
{
/**
* @param int $days
* @return array{labels: string[], hits: int[], visitors: int[], bytes: int[]}
* @throws \Exception
*/
public function getGoaccessChartData(int $days = 31): array
{
$type = $this->goaccessType;
$data = $this->readJsonData();
$chartData = [
'labels' => [],
'hits' => [],
'visitors' => [],
'bytes' => [],
];
if (!isset($data[$type])) {
return $chartData;
}
$rawData = array_slice($data[$type]->data, 0, $days);
try {
new \DateTime($rawData[0]?->data);
$reverse = true;
} catch (\Exception) {
$reverse = false;
}
if ($reverse) {
$rawData = array_reverse($rawData);
}
foreach ($rawData as $day) {
try {
$date = (new \DateTime($day->data))->format('d.m.');
} catch (\Exception) {
$date = $day->data;
}
$chartData['labels'][] = $date;
foreach (['hits', 'visitors', 'bytes'] as $dataTypes) {
$chartData[$dataTypes][] = $day->$dataTypes->count;
}
}
return $chartData;
}
/**
* @return array{labels: string[], datasets: array<mixed>}
* @throws \Exception
*/
public function getChartData(): array
{
$data = $this->getGoaccessChartData();
$data = [
'labels' => $data['labels'],
'datasets' => [
[
'label' => $this->languageService->sL('LLL:EXT:xm_goaccess/Resources/Private/Language/locallang.xlf:visitors'),
'borderColor' => WidgetApi::getDefaultChartColors()[0],
'backgroundColor' => self::hex2rgba(WidgetApi::getDefaultChartColors()[0], 0.1),
'parsing' => ['yAxisKey' => 'A'],
'borderWidth' => 1,
'data' => $data['visitors'],
],
[
'label' => $this->languageService->sL('LLL:EXT:xm_goaccess/Resources/Private/Language/locallang.xlf:hits'),
'borderColor' => WidgetApi::getDefaultChartColors()[1],
'backgroundColor' => self::hex2rgba(WidgetApi::getDefaultChartColors()[1], 0.1),
'yAxisID' => 'right',
'borderWidth' => 1,
'data' => $data['hits'],
],
],
];
$typo3Version = (int)VersionNumberUtility::convertVersionStringToArray(VersionNumberUtility::getCurrentTypo3Version())['version_main'];
if ($typo3Version >= 12) {
$data['datasets'][0]['fill'] = 'origin';
$data['datasets'][1]['fill'] = 'origin';
}
return $data;
}
}