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