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