xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision db3aeb3eb846bb233f34557aedb1559aadf9275a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\GedcomTag;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Module\ModuleThemeInterface;
23use Fisharebest\Webtrees\Statistics\Service\ColorService;
24
25/**
26 * A chart showing the top used media types.
27 */
28class ChartMedia
29{
30    /**
31     * @var ModuleThemeInterface
32     */
33    private $theme;
34
35    /**
36     * @var ColorService
37     */
38    private $color_service;
39
40    /**
41     * Constructor.
42     */
43    public function __construct()
44    {
45        $this->theme         = app(ModuleThemeInterface::class);
46        $this->color_service = new ColorService();
47    }
48
49    /**
50     * Create a chart of media types.
51     *
52     * @param array       $media      The list of media types to display
53     * @param string|null $color_from
54     * @param string|null $color_to
55     *
56     * @return string
57     */
58    public function chartMedia(
59        array $media,
60        string $color_from = null,
61        string $color_to   = null
62    ): string {
63        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
64        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
65        $color_from   = $color_from ?? $chart_color1;
66        $color_to     = $color_to   ?? $chart_color2;
67
68        $data = [
69            [
70                I18N::translate('Type'),
71                I18N::translate('Total')
72            ],
73        ];
74
75        foreach ($media as $type => $count) {
76            $data[] = [
77                GedcomTag::getFileFormTypeValue($type),
78                $count
79            ];
80        }
81
82        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
83
84        return view(
85            'statistics/other/charts/pie',
86            [
87                'title'  => null,
88                'data'   => $data,
89                'colors' => $colors,
90            ]
91        );
92    }
93}
94