18add1155SRico Sonntag<?php 23976b470SGreg Roach 38add1155SRico Sonntag/** 48add1155SRico Sonntag * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify 78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by 88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or 98add1155SRico Sonntag * (at your option) any later version. 108add1155SRico Sonntag * This program is distributed in the hope that it will be useful, 118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of 128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138add1155SRico Sonntag * GNU General Public License for more details. 148add1155SRico Sonntag * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168add1155SRico Sonntag */ 17fcfa147eSGreg Roach 188add1155SRico Sonntagdeclare(strict_types=1); 198add1155SRico Sonntag 208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google; 218add1155SRico Sonntag 226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N; 2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService; 258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree; 268add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause; 2734e3587bSGreg Roachuse Illuminate\Support\Collection; 288add1155SRico Sonntag 298add1155SRico Sonntag/** 3093ccd686SRico Sonntag * A chart showing the average number of children by century. 318add1155SRico Sonntag */ 3293ccd686SRico Sonntagclass ChartChildren 338add1155SRico Sonntag{ 3434b20f29SGreg Roach private Tree $tree; 3593ccd686SRico Sonntag 3634b20f29SGreg Roach private CenturyService $century_service; 378add1155SRico Sonntag 388add1155SRico Sonntag /** 39f78da678SGreg Roach * @param CenturyService $century_service 408add1155SRico Sonntag * @param Tree $tree 418add1155SRico Sonntag */ 42f78da678SGreg Roach public function __construct(CenturyService $century_service, Tree $tree) 438add1155SRico Sonntag { 44f78da678SGreg Roach $this->century_service = $century_service; 4593ccd686SRico Sonntag $this->tree = $tree; 468add1155SRico Sonntag } 478add1155SRico Sonntag 488add1155SRico Sonntag /** 498add1155SRico Sonntag * Returns the related database records. 508add1155SRico Sonntag * 51*d7c2f5ccSGreg Roach * @return Collection<array-key,object> 528add1155SRico Sonntag */ 5334e3587bSGreg Roach private function queryRecords(): Collection 548add1155SRico Sonntag { 5534e3587bSGreg Roach return DB::table('families') 5634e3587bSGreg Roach ->selectRaw('AVG(f_numchil) AS total') 57cde98219SGreg Roach ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century') 580b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 598add1155SRico Sonntag $join->on('d_file', '=', 'f_file') 608add1155SRico Sonntag ->on('d_gid', '=', 'f_id'); 618add1155SRico Sonntag }) 628add1155SRico Sonntag ->where('f_file', '=', $this->tree->id()) 638add1155SRico Sonntag ->where('d_julianday1', '<>', 0) 648add1155SRico Sonntag ->where('d_fact', '=', 'MARR') 658add1155SRico Sonntag ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 668add1155SRico Sonntag ->groupBy(['century']) 6734e3587bSGreg Roach ->orderBy('century') 6834e3587bSGreg Roach ->get() 69f25fc0f9SGreg Roach ->map(static fn (object $row): object => (object) [ 7034e3587bSGreg Roach 'century' => (int) $row->century, 7134e3587bSGreg Roach 'total' => (float) $row->total, 72f25fc0f9SGreg Roach ]); 738add1155SRico Sonntag } 748add1155SRico Sonntag 758add1155SRico Sonntag /** 7688de55fdSRico Sonntag * Creates a children per family chart. 778add1155SRico Sonntag * 788add1155SRico Sonntag * @return string 798add1155SRico Sonntag */ 8088de55fdSRico Sonntag public function chartChildren(): string 818add1155SRico Sonntag { 8288de55fdSRico Sonntag $data = [ 8388de55fdSRico Sonntag [ 8488de55fdSRico Sonntag I18N::translate('Century'), 8588de55fdSRico Sonntag I18N::translate('Average number') 8688de55fdSRico Sonntag ] 8788de55fdSRico Sonntag ]; 888add1155SRico Sonntag 8988de55fdSRico Sonntag foreach ($this->queryRecords() as $record) { 9088de55fdSRico Sonntag $data[] = [ 9134e3587bSGreg Roach $this->century_service->centuryName($record->century), 9234e3587bSGreg Roach round($record->total, 2), 9388de55fdSRico Sonntag ]; 948add1155SRico Sonntag } 958add1155SRico Sonntag 96a81e5019SRico Sonntag $chart_title = I18N::translate('Average number of children per family'); 97a81e5019SRico Sonntag $chart_options = [ 98a81e5019SRico Sonntag 'title' => $chart_title, 99a81e5019SRico Sonntag 'subtitle' => '', 100a81e5019SRico Sonntag 'legend' => [ 101a81e5019SRico Sonntag 'position' => 'none', 102a81e5019SRico Sonntag ], 103a81e5019SRico Sonntag 'vAxis' => [ 104a81e5019SRico Sonntag 'title' => I18N::translate('Number of children'), 105a81e5019SRico Sonntag ], 106a81e5019SRico Sonntag 'hAxis' => [ 10776e7c249SGreg Roach 'showTextEvery' => 1, 10876e7c249SGreg Roach 'slantedText' => false, 109a81e5019SRico Sonntag 'title' => I18N::translate('Century'), 110a81e5019SRico Sonntag ], 111a81e5019SRico Sonntag 'colors' => [ 112a81e5019SRico Sonntag '#84beff' 113a81e5019SRico Sonntag ], 114a81e5019SRico Sonntag ]; 115a81e5019SRico Sonntag 11690a2f718SGreg Roach return view('statistics/other/charts/column', [ 11788de55fdSRico Sonntag 'data' => $data, 118a81e5019SRico Sonntag 'chart_options' => $chart_options, 119a81e5019SRico Sonntag 'chart_title' => $chart_title, 12065cf5706SGreg Roach 'language' => I18N::languageTag(), 12190a2f718SGreg Roach ]); 1228add1155SRico Sonntag } 1238add1155SRico Sonntag} 124