xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision ba1d7494e9648df5a87aaa3fece3e098e5ce26b1)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\GedcomTag;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Module\ModuleThemeInterface;
26use Fisharebest\Webtrees\Statistics\Service\ColorService;
27use Psr\Http\Message\ServerRequestInterface;
28
29use function app;
30use function assert;
31use function count;
32
33/**
34 * A chart showing the top used media types.
35 */
36class ChartMedia
37{
38    /**
39     * @var ModuleThemeInterface
40     */
41    private $theme;
42
43    /**
44     * @var ColorService
45     */
46    private $color_service;
47
48    /**
49     * Constructor.
50     */
51    public function __construct()
52    {
53        $this->theme         = app(ModuleThemeInterface::class);
54        $this->color_service = new ColorService();
55    }
56
57    /**
58     * Create a chart of media types.
59     *
60     * @param array       $media      The list of media types to display
61     * @param string|null $color_from
62     * @param string|null $color_to
63     *
64     * @return string
65     */
66    public function chartMedia(
67        array $media,
68        string $color_from = null,
69        string $color_to = null
70    ): string {
71        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
72        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
73        $color_from   = $color_from ?? $chart_color1;
74        $color_to     = $color_to   ?? $chart_color2;
75
76        $data = [
77            [
78                I18N::translate('Type'),
79                I18N::translate('Total')
80            ],
81        ];
82
83        foreach ($media as $type => $count) {
84            $data[] = [
85                GedcomTag::getFileFormTypeValue($type),
86                $count
87            ];
88        }
89
90        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
91
92        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
93        assert($locale instanceof LocaleInterface);
94
95        return view('statistics/other/charts/pie', [
96            'title'    => null,
97            'data'     => $data,
98            'colors'   => $colors,
99            'language' => $locale->languageTag(),
100        ]);
101    }
102}
103