xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 4062e425719e0c10354283f336db3fe2ac216a64)
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\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\ModuleThemeInterface;
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       $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        foreach ($media as $type => $count) {
81            $data[] = [
82                GedcomTag::getFileFormTypeValue($type),
83                $count
84            ];
85        }
86
87        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
88
89        return view('statistics/other/charts/pie', [
90            'title'    => null,
91            'data'     => $data,
92            'colors'   => $colors,
93            'language' => I18N::languageTag(),
94        ]);
95    }
96}
97