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