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