xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 1542a3c1c3f069aa1fa975b9754a150971b42c6f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Registry;
24use Fisharebest\Webtrees\Statistics\Service\ColorService;
25
26use function count;
27use function view;
28
29/**
30 * A chart showing the top used media types.
31 */
32class ChartMedia
33{
34    private ColorService $color_service;
35
36    /**
37     * @param ColorService $color_service
38     */
39    public function __construct(ColorService $color_service)
40    {
41        $this->color_service = $color_service;
42    }
43
44    /**
45     * Create a chart of media types.
46     *
47     * @param array<string,int> $media      The list of media types to display
48     * @param string|null       $color_from
49     * @param string|null       $color_to
50     *
51     * @return string
52     */
53    public function chartMedia(
54        array $media,
55        string|null $color_from = null,
56        string|null $color_to = null
57    ): string {
58        $color_from ??= 'ffffff';
59        $color_to ??= '84beff';
60
61        $data = [
62            [
63                I18N::translate('Type'),
64                I18N::translate('Total')
65            ],
66        ];
67
68        $element = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE');
69        $values  = $element->values();
70
71        foreach ($media as $type => $count) {
72            $data[] = [
73                $values[$element->canonical($type)] ?? $type,
74                $count
75            ];
76        }
77
78        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
79
80        return view('statistics/other/charts/pie', [
81            'title'    => null,
82            'data'     => $data,
83            'colors'   => $colors,
84            'language' => I18N::languageTag(),
85        ]);
86    }
87}
88