xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 9f9acdbc09170c04c1e150c36ee57d49027e314a)
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\I18N;
23use Fisharebest\Webtrees\Module\ModuleThemeInterface;
24use Fisharebest\Webtrees\Statistics\Service\ColorService;
25
26use function app;
27use function count;
28
29/**
30 * A chart showing the top given names.
31 */
32class ChartCommonGiven
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 common given names.
55     *
56     * @param int         $tot_indi   The total number of individuals
57     * @param array       $given      The list of common given names
58     * @param string|null $color_from
59     * @param string|null $color_to
60     *
61     * @return string
62     */
63    public function chartCommonGiven(
64        int $tot_indi,
65        array $given,
66        string $color_from = null,
67        string $color_to = null
68    ): string {
69        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
70        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
71        $color_from   = $color_from ?? $chart_color1;
72        $color_to     = $color_to   ?? $chart_color2;
73
74        $tot = 0;
75        foreach ($given as $count) {
76            $tot += $count;
77        }
78
79        $data = [
80            [
81                I18N::translate('Name'),
82                I18N::translate('Total')
83            ],
84        ];
85
86        foreach ($given as $name => $count) {
87            $data[] = [ $name, $count ];
88        }
89
90        $data[] = [
91            I18N::translate('Other'),
92            $tot_indi - $tot
93        ];
94
95        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
96
97        return view('statistics/other/charts/pie', [
98            'title'    => null,
99            'data'     => $data,
100            'colors'   => $colors,
101            'language' => I18N::languageTag(),
102        ]);
103    }
104}
105