1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\I18N; 23use Fisharebest\Webtrees\Statistics\Service\CenturyService; 24use Fisharebest\Webtrees\Tree; 25use Illuminate\Database\Capsule\Manager as DB; 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) 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 function (object $row): object { 71 return (object) [ 72 'century' => (int) $row->century, 73 'total' => (float) $row->total, 74 ]; 75 }); 76 } 77 78 /** 79 * Creates a children per family chart. 80 * 81 * @return string 82 */ 83 public function chartChildren(): string 84 { 85 $data = [ 86 [ 87 I18N::translate('Century'), 88 I18N::translate('Average number') 89 ] 90 ]; 91 92 foreach ($this->queryRecords() as $record) { 93 $data[] = [ 94 $this->century_service->centuryName($record->century), 95 round($record->total, 2), 96 ]; 97 } 98 99 $chart_title = I18N::translate('Average number of children per family'); 100 $chart_options = [ 101 'title' => $chart_title, 102 'subtitle' => '', 103 'legend' => [ 104 'position' => 'none', 105 ], 106 'vAxis' => [ 107 'title' => I18N::translate('Number of children'), 108 ], 109 'hAxis' => [ 110 'showTextEvery' => 1, 111 'slantedText' => false, 112 'title' => I18N::translate('Century'), 113 ], 114 'colors' => [ 115 '#84beff' 116 ], 117 ]; 118 119 return view('statistics/other/charts/column', [ 120 'data' => $data, 121 'chart_options' => $chart_options, 122 'chart_title' => $chart_title, 123 'language' => I18N::languageTag(), 124 ]); 125 } 126} 127