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