xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 9deadf1c5b187f630fcc0ae0771860f690e7f202)
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\Module\ModuleThemeInterface;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26
27use function app;
28use function count;
29
30/**
31 * A chart showing the top used media types.
32 */
33class ChartMedia
34{
35    /**
36     * @var ModuleThemeInterface
37     */
38    private $theme;
39
40    /**
41     * @var ColorService
42     */
43    private $color_service;
44
45    /**
46     * Constructor.
47     */
48    public function __construct()
49    {
50        $this->theme         = app(ModuleThemeInterface::class);
51        $this->color_service = new ColorService();
52    }
53
54    /**
55     * Create a chart of media types.
56     *
57     * @param array<string,int> $media      The list of media types to display
58     * @param string|null       $color_from
59     * @param string|null       $color_to
60     *
61     * @return string
62     */
63    public function chartMedia(
64        array $media,
65        string $color_from = null,
66        string $color_to = null
67    ): string {
68        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
69        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
70        $color_from   = $color_from ?? $chart_color1;
71        $color_to     = $color_to   ?? $chart_color2;
72
73        $data = [
74            [
75                I18N::translate('Type'),
76                I18N::translate('Total')
77            ],
78        ];
79
80        $values = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values();
81
82        foreach ($media as $type => $count) {
83            $data[] = [
84                $values[$type] ?? $type,
85                $count
86            ];
87        }
88
89        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
90
91        return view('statistics/other/charts/pie', [
92            'title'    => null,
93            'data'     => $data,
94            'colors'   => $colors,
95            'language' => I18N::languageTag(),
96        ]);
97    }
98}
99