xref: /webtrees/app/Statistics/Google/ChartCommonSurname.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
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;
25use Fisharebest\Webtrees\Tree;
26
27use function app;
28use function count;
29
30/**
31 * A chart showing the top surnames.
32 */
33class ChartCommonSurname
34{
35    /**
36     * @var ModuleThemeInterface
37     */
38    private $theme;
39
40    /**
41     * @var string
42     */
43    private $surname_tradition;
44
45    /**
46     * @var ColorService
47     */
48    private $color_service;
49
50    /**
51     * Constructor.
52     *
53     * @param Tree $tree
54     */
55    public function __construct(Tree $tree)
56    {
57        $this->theme             = app(ModuleThemeInterface::class);
58        $this->surname_tradition = $tree->getPreference('SURNAME_TRADITION');
59        $this->color_service     = new ColorService();
60    }
61
62    /**
63     * Count up the different versions of a name and returns the one with the most matches. Takes
64     * different surname traditions into account.
65     *
66     * @param array $surns
67     *
68     * @return array [ name, count ]
69     */
70    private function getTopNameAndCount(array $surns): array
71    {
72        $max_name  = 0;
73        $count_per = 0;
74        $top_name  = '';
75
76        foreach ($surns as $spfxsurn => $count) {
77            $per = $count;
78            $count_per += $per;
79
80            // select most common surname from all variants
81            if ($per > $max_name) {
82                $max_name = $per;
83                $top_name = $spfxsurn;
84            }
85        }
86
87        if ($this->surname_tradition === 'polish') {
88            // Most common surname should be in male variant (Kowalski, not Kowalska)
89            $top_name = preg_replace(
90                [
91                    '/ska$/',
92                    '/cka$/',
93                    '/dzka$/',
94                    '/żka$/',
95                ],
96                [
97                    'ski',
98                    'cki',
99                    'dzki',
100                    'żki',
101                ],
102                $top_name
103            );
104        }
105
106        return [
107            $top_name,
108            $count_per
109        ];
110    }
111
112    /**
113     * Create a chart of common surnames.
114     *
115     * @param int         $tot_indi     The total number of individuals
116     * @param array       $all_surnames The list of common surnames
117     * @param string|null $color_from
118     * @param string|null $color_to
119     *
120     * @return string
121     */
122    public function chartCommonSurnames(
123        int $tot_indi,
124        array $all_surnames,
125        string $color_from = null,
126        string $color_to = null
127    ): string {
128        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
129        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
130        $color_from   = $color_from ?? $chart_color1;
131        $color_to     = $color_to   ?? $chart_color2;
132
133        $tot = 0;
134        foreach ($all_surnames as $surn => $surnames) {
135            $tot += array_sum($surnames);
136        }
137
138        $data = [
139            [
140                I18N::translate('Name'),
141                I18N::translate('Total')
142            ],
143        ];
144
145        foreach ($all_surnames as $surns) {
146            $data[] = $this->getTopNameAndCount($surns);
147        }
148
149        $data[] = [
150            I18N::translate('Other'),
151            $tot_indi - $tot
152        ];
153
154        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
155
156        return view('statistics/other/charts/pie', [
157            'title'    => null,
158            'data'     => $data,
159            'colors'   => $colors,
160            'language' => I18N::languageTag(),
161        ]);
162    }
163}
164