1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Statistics\Google; 20 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Statistics\Service\CenturyService; 23use Fisharebest\Webtrees\Tree; 24use Illuminate\Database\Capsule\Manager as DB; 25use Illuminate\Database\Query\JoinClause; 26use stdClass; 27 28/** 29 * A chart showing the average number of children by century. 30 */ 31class ChartChildren 32{ 33 /** 34 * @var Tree 35 */ 36 private $tree; 37 38 /** 39 * @var CenturyService 40 */ 41 private $century_service; 42 43 /** 44 * Constructor. 45 * 46 * @param Tree $tree 47 */ 48 public function __construct(Tree $tree) 49 { 50 $this->tree = $tree; 51 $this->century_service = new CenturyService(); 52 } 53 54 /** 55 * Returns the related database records. 56 * 57 * @return stdClass[] 58 */ 59 private function queryRecords(): array 60 { 61 $query = DB::table('families') 62 ->selectRaw('ROUND(AVG(f_numchil), 2) AS num') 63 ->selectRaw('ROUND((d_year + 49) / 100) AS century') 64 ->join('dates', static function (JoinClause $join): void { 65 $join->on('d_file', '=', 'f_file') 66 ->on('d_gid', '=', 'f_id'); 67 }) 68 ->where('f_file', '=', $this->tree->id()) 69 ->where('d_julianday1', '<>', 0) 70 ->where('d_fact', '=', 'MARR') 71 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 72 ->groupBy(['century']) 73 ->orderBy('century'); 74 75 return $query->get()->all(); 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((int) $record->century), 95 (float) $record->num 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 'title' => I18N::translate('Century'), 111 ], 112 'colors' => [ 113 '#84beff' 114 ], 115 ]; 116 117 return view( 118 'statistics/other/charts/column', 119 [ 120 'data' => $data, 121 'chart_options' => $chart_options, 122 'chart_title' => $chart_title, 123 ] 124 ); 125 } 126} 127