xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 9483aecd71c61904a8b3a2a683a942c6571afde6)
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\I18N;
24use Fisharebest\Webtrees\Module\ModuleThemeInterface;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function app;
29use function assert;
30use function count;
31
32/**
33 * A chart showing the top given names.
34 */
35class ChartCommonGiven
36{
37    /**
38     * @var ModuleThemeInterface
39     */
40    private $theme;
41
42    /**
43     * @var ColorService
44     */
45    private $color_service;
46
47    /**
48     * Constructor.
49     */
50    public function __construct()
51    {
52        $this->theme         = app(ModuleThemeInterface::class);
53        $this->color_service = new ColorService();
54    }
55
56    /**
57     * Create a chart of common given names.
58     *
59     * @param int         $tot_indi   The total number of individuals
60     * @param array       $given      The list of common given names
61     * @param string|null $color_from
62     * @param string|null $color_to
63     *
64     * @return string
65     */
66    public function chartCommonGiven(
67        int $tot_indi,
68        array $given,
69        string $color_from = null,
70        string $color_to = null
71    ): string {
72        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
73        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
74        $color_from   = $color_from ?? $chart_color1;
75        $color_to     = $color_to   ?? $chart_color2;
76
77        $tot = 0;
78        foreach ($given as $count) {
79            $tot += $count;
80        }
81
82        $data = [
83            [
84                I18N::translate('Name'),
85                I18N::translate('Total')
86            ],
87        ];
88
89        foreach ($given as $name => $count) {
90            $data[] = [ $name, $count ];
91        }
92
93        $data[] = [
94            I18N::translate('Other'),
95            $tot_indi - $tot
96        ];
97
98        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
99
100        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
101        assert($locale instanceof LocaleInterface);
102
103        return view('statistics/other/charts/pie', [
104            'title'    => null,
105            'data'     => $data,
106            'colors'   => $colors,
107            'language' => $locale->languageTag(),
108        ]);
109    }
110}
111