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