1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Statistics\Google; 19 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Module\ModuleThemeInterface; 22use Fisharebest\Webtrees\Statistics\Service\ColorService; 23use Fisharebest\Webtrees\Tree; 24 25/** 26 * A chart showing the top surnames. 27 */ 28class ChartCommonSurname 29{ 30 /** 31 * @var ModuleThemeInterface 32 */ 33 private $theme; 34 35 /** 36 * @var string 37 */ 38 private $surname_tradition; 39 40 /** 41 * @var ColorService 42 */ 43 private $color_service; 44 45 /** 46 * Constructor. 47 * 48 * @param Tree $tree 49 */ 50 public function __construct(Tree $tree) 51 { 52 $this->theme = app()->make(ModuleThemeInterface::class); 53 $this->surname_tradition = $tree->getPreference('SURNAME_TRADITION'); 54 $this->color_service = new ColorService(); 55 } 56 57 /** 58 * Count up the different versions of a name and returns the one with the most matches. Takes 59 * different surname traditions into account. 60 * 61 * @param array $surns 62 * 63 * @return array [ name, count ] 64 */ 65 private function getTopNameAndCount(array $surns): array 66 { 67 $max_name = 0; 68 $count_per = 0; 69 $top_name = ''; 70 71 foreach ($surns as $spfxsurn => $count) { 72 $per = $count; 73 $count_per += $per; 74 75 // select most common surname from all variants 76 if ($per > $max_name) { 77 $max_name = $per; 78 $top_name = $spfxsurn; 79 } 80 } 81 82 if ($this->surname_tradition === 'polish') { 83 // Most common surname should be in male variant (Kowalski, not Kowalska) 84 $top_name = preg_replace( 85 [ 86 '/ska$/', 87 '/cka$/', 88 '/dzka$/', 89 '/żka$/', 90 ], 91 [ 92 'ski', 93 'cki', 94 'dzki', 95 'żki', 96 ], 97 $top_name 98 ); 99 } 100 101 return [ 102 $top_name, 103 $count_per 104 ]; 105 } 106 107 /** 108 * Create a chart of common surnames. 109 * 110 * @param int $tot_indi The total number of individuals 111 * @param array $all_surnames The list of common surnames 112 * @param string|null $color_from 113 * @param string|null $color_to 114 * 115 * @return string 116 */ 117 public function chartCommonSurnames( 118 int $tot_indi, 119 array $all_surnames, 120 string $color_from = null, 121 string $color_to = null 122 ): string { 123 $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values'); 124 $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values'); 125 $color_from = $color_from ?? $chart_color1; 126 $color_to = $color_to ?? $chart_color2; 127 128 $tot = 0; 129 foreach ($all_surnames as $surn => $surnames) { 130 $tot += array_sum($surnames); 131 } 132 133 $data = [ 134 [ 135 I18N::translate('Name'), 136 I18N::translate('Total') 137 ], 138 ]; 139 140 foreach ($all_surnames as $surns) { 141 $data[] = $this->getTopNameAndCount($surns); 142 } 143 144 $data[] = [ 145 I18N::translate('Other'), 146 $tot_indi - $tot 147 ]; 148 149 $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1); 150 151 return view( 152 'statistics/other/charts/pie', 153 [ 154 'title' => null, 155 'data' => $data, 156 'colors' => $colors, 157 ] 158 ); 159 } 160} 161