|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Service\StatsService; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
9
|
|
|
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface; |
|
10
|
|
|
use Symfony\UX\Chartjs\Model\Chart; |
|
11
|
|
|
|
|
12
|
|
|
class StatsController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @Route("/stats", name="stats_index") |
|
16
|
|
|
*/ |
|
17
|
|
|
public function index( |
|
18
|
|
|
StatsService $statsService, |
|
19
|
|
|
ChartBuilderInterface $chartBuilder |
|
20
|
|
|
): Response { |
|
21
|
|
|
$wanderStats = $statsService->getWanderStats(); |
|
22
|
|
|
$imageStats = $statsService->getImageStats(); |
|
23
|
|
|
|
|
24
|
|
|
$wanderNumberColour = '#A6246C'; |
|
25
|
|
|
$imagesNumberColour = '#0367A6'; |
|
26
|
|
|
|
|
27
|
|
|
$monthlyWanderChart = $chartBuilder->createChart(Chart::TYPE_BAR); |
|
28
|
|
|
$monthlyWanderChart->setData([ |
|
29
|
|
|
'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['monthlyStats']), |
|
30
|
|
|
'datasets' => [ |
|
31
|
|
|
// TODO: These colours should be defined in CSS. Add class somehow? |
|
32
|
|
|
[ |
|
33
|
|
|
'label' => 'Number of Wanders', |
|
34
|
|
|
'backgroundColor' => $wanderNumberColour, |
|
35
|
|
|
'borderColor' => 'black', |
|
36
|
|
|
'data' => array_map(fn($dp): int => $dp['numberOfWanders'], $wanderStats['monthlyStats']), |
|
37
|
|
|
], |
|
38
|
|
|
[ |
|
39
|
|
|
'label' => 'Distance Walked (km)', |
|
40
|
|
|
'backgroundColor' => '#ffb266',// '#66ffff', |
|
41
|
|
|
'borderColor' => 'black', |
|
42
|
|
|
'data' => array_map(fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2), $wanderStats['monthlyStats']), |
|
43
|
|
|
] |
|
44
|
|
|
] |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$yearlyWanderChart = $chartBuilder->createChart(Chart::TYPE_BAR); |
|
48
|
|
|
$yearlyWanderChart->setData([ |
|
49
|
|
|
'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['yearlyStats']), |
|
50
|
|
|
'datasets' => [ |
|
51
|
|
|
// TODO: These colours should be defined in CSS. Add class somehow? |
|
52
|
|
|
[ |
|
53
|
|
|
'label' => 'Number of Wanders', |
|
54
|
|
|
'backgroundColor' => $wanderNumberColour, |
|
55
|
|
|
'borderColor' => 'black', |
|
56
|
|
|
'data' => array_map(fn($dp): int => $dp['numberOfWanders'], $wanderStats['yearlyStats']), |
|
57
|
|
|
], |
|
58
|
|
|
[ |
|
59
|
|
|
'label' => 'Distance Walked (km)', |
|
60
|
|
|
'backgroundColor' => '#ffb266', // '#66ffff', |
|
61
|
|
|
'borderColor' => 'black', |
|
62
|
|
|
'data' => array_map(fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2), $wanderStats['yearlyStats']), |
|
63
|
|
|
] |
|
64
|
|
|
] |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
$monthlyImagesChart = $chartBuilder->createChart(Chart::TYPE_BAR); |
|
68
|
|
|
$monthlyImagesChart->setData([ |
|
69
|
|
|
'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['monthlyStats']), |
|
70
|
|
|
'datasets' => [ |
|
71
|
|
|
// TODO: These colours should be defined in CSS. Add class somehow? |
|
72
|
|
|
[ |
|
73
|
|
|
'label' => 'Photos Taken', |
|
74
|
|
|
'backgroundColor' => $imagesNumberColour, // '#ffb266', |
|
75
|
|
|
'borderColor' => 'black', |
|
76
|
|
|
'data' => array_map(fn($dp): int => $dp['numberOfImages'], $wanderStats['monthlyStats']), |
|
77
|
|
|
] |
|
78
|
|
|
] |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$yearlyImagesChart = $chartBuilder->createChart(Chart::TYPE_BAR); |
|
82
|
|
|
$yearlyImagesChart->setData([ |
|
83
|
|
|
'labels' => array_map(fn($dp): string => $dp['periodLabel'], $wanderStats['yearlyStats']), |
|
84
|
|
|
'datasets' => [ |
|
85
|
|
|
// TODO: These colours should be defined in CSS. Add class somehow? |
|
86
|
|
|
[ |
|
87
|
|
|
'label' => 'Photos Taken', |
|
88
|
|
|
'backgroundColor' => $imagesNumberColour, // '#ffb266', |
|
89
|
|
|
'borderColor' => 'black', |
|
90
|
|
|
'data' => array_map(fn($dp): int => $dp['numberOfImages'], $wanderStats['yearlyStats']), |
|
91
|
|
|
] |
|
92
|
|
|
] |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
return $this->render('stats/index.html.twig', [ |
|
96
|
|
|
'controller_name' => 'StatsController', // TODO: Remove this boilerplate |
|
97
|
|
|
'imageStats' => $imageStats, |
|
98
|
|
|
'wanderStats' => $wanderStats, |
|
99
|
|
|
'monthlyWanderChart' => $monthlyWanderChart, |
|
100
|
|
|
'yearlyWanderChart' => $yearlyWanderChart, |
|
101
|
|
|
'monthlyImagesChart' => $monthlyImagesChart, |
|
102
|
|
|
'yearlyImagesChart' => $yearlyImagesChart |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|