xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 3cfcc809af53e831fa6cafac7b274a2cb407db6e)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Statistics\Google;
20
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Module\ModuleThemeInterface;
23use Fisharebest\Webtrees\Statistics\Service\ColorService;
24
25use function count;
26
27/**
28 * A chart showing the top given names.
29 */
30class ChartCommonGiven
31{
32    /**
33     * @var ModuleThemeInterface
34     */
35    private $theme;
36
37    /**
38     * @var ColorService
39     */
40    private $color_service;
41
42    /**
43     * Constructor.
44     */
45    public function __construct()
46    {
47        $this->theme         = app(ModuleThemeInterface::class);
48        $this->color_service = new ColorService();
49    }
50
51    /**
52     * Create a chart of common given names.
53     *
54     * @param int         $tot_indi   The total number of individuals
55     * @param array       $given      The list of common given names
56     * @param string|null $color_from
57     * @param string|null $color_to
58     *
59     * @return string
60     */
61    public function chartCommonGiven(
62        int $tot_indi,
63        array $given,
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        $tot = 0;
73        foreach ($given as $count) {
74            $tot += $count;
75        }
76
77        $data = [
78            [
79                I18N::translate('Name'),
80                I18N::translate('Total')
81            ],
82        ];
83
84        foreach ($given as $name => $count) {
85            $data[] = [ $name, $count ];
86        }
87
88        $data[] = [
89            I18N::translate('Other'),
90            $tot_indi - $tot
91        ];
92
93        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
94
95        return view(
96            'statistics/other/charts/pie',
97            [
98                'title'  => null,
99                'data'   => $data,
100                'colors' => $colors,
101            ]
102        );
103    }
104}
105