1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Statistics\Google; 21 22use Fisharebest\Webtrees\DB; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Statistics\Service\CenturyService; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Query\JoinClause; 27use Illuminate\Support\Collection; 28use stdClass; 29 30/** 31 * A chart showing the average number of children by century. 32 */ 33class ChartChildren 34{ 35 private Tree $tree; 36 37 private CenturyService $century_service; 38 39 /** 40 * @param CenturyService $century_service 41 * @param Tree $tree 42 */ 43 public function __construct(CenturyService $century_service, Tree $tree) 44 { 45 $this->century_service = $century_service; 46 $this->tree = $tree; 47 } 48 49 /** 50 * Returns the related database records. 51 * 52 * @return Collection<array-key,stdClass> 53 */ 54 private function queryRecords(): Collection 55 { 56 return DB::table('families') 57 ->selectRaw('AVG(f_numchil) AS total') 58 ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century') 59 ->join('dates', static function (JoinClause $join): void { 60 $join->on('d_file', '=', 'f_file') 61 ->on('d_gid', '=', 'f_id'); 62 }) 63 ->where('f_file', '=', $this->tree->id()) 64 ->where('d_julianday1', '<>', 0) 65 ->where('d_fact', '=', 'MARR') 66 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 67 ->groupBy(['century']) 68 ->orderBy('century') 69 ->get() 70 ->map(static fn(object $row): object => (object) [ 71 'century' => (int) $row->century, 72 'total' => (float) $row->total, 73 ]); 74 } 75 76 /** 77 * Creates a children per family chart. 78 * 79 * @return string 80 */ 81 public function chartChildren(): string 82 { 83 $data = [ 84 [ 85 I18N::translate('Century'), 86 I18N::translate('Average number') 87 ] 88 ]; 89 90 foreach ($this->queryRecords() as $record) { 91 $data[] = [ 92 $this->century_service->centuryName($record->century), 93 round($record->total, 2), 94 ]; 95 } 96 97 $chart_title = I18N::translate('Average number of children per family'); 98 $chart_options = [ 99 'title' => $chart_title, 100 'subtitle' => '', 101 'legend' => [ 102 'position' => 'none', 103 ], 104 'vAxis' => [ 105 'title' => I18N::translate('Number of children'), 106 ], 107 'hAxis' => [ 108 'showTextEvery' => 1, 109 'slantedText' => false, 110 'title' => I18N::translate('Century'), 111 ], 112 'colors' => [ 113 '#84beff' 114 ], 115 ]; 116 117 return view('statistics/other/charts/column', [ 118 'data' => $data, 119 'chart_options' => $chart_options, 120 'chart_title' => $chart_title, 121 'language' => I18N::languageTag(), 122 ]); 123 } 124} 125