xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 $color_from = null,
56        string $color_to = null
57    ): string {
58        $color_from = $color_from ?? 'ffffff';
59        $color_to   = $color_to ?? '84beff';
60
61        $data = [
62            [
63                I18N::translate('Type'),
64                I18N::translate('Total')
65            ],
66        ];
67
68        $values = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values();
69
70        foreach ($media as $type => $count) {
71            $data[] = [
72                $values[$type] ?? $type,
73                $count
74            ];
75        }
76
77        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
78
79        return view('statistics/other/charts/pie', [
80            'title'    => null,
81            'data'     => $data,
82            'colors'   => $colors,
83            'language' => I18N::languageTag(),
84        ]);
85    }
86}
87