1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Statistics\Google; 19 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Statistics\AbstractGoogle; 22use Fisharebest\Webtrees\Statistics\Helper\Century; 23use Fisharebest\Webtrees\Tree; 24use Illuminate\Database\Capsule\Manager as DB; 25use Illuminate\Database\Query\JoinClause; 26 27/** 28 * 29 */ 30class ChartChildren extends AbstractGoogle 31{ 32 /** 33 * @var Century 34 */ 35 private $centuryHelper; 36 37 /** 38 * Constructor. 39 * 40 * @param Tree $tree 41 */ 42 public function __construct(Tree $tree) 43 { 44 parent::__construct($tree); 45 46 $this->centuryHelper = new Century(); 47 } 48 49 /** 50 * Returns the related database records. 51 * 52 * @return \stdClass[] 53 */ 54 private function queryRecords(): array 55 { 56 $query = DB::table('families') 57 ->selectRaw('ROUND(AVG(f_numchil), 2) AS num') 58 ->selectRaw('ROUND((d_year - 50) / 100) AS century') 59 ->join('dates', function (JoinClause $join) { 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 70 return $query->get()->all(); 71 } 72 73 /** 74 * Creates a children per family chart. 75 * 76 * @return string 77 */ 78 public function chartChildren(): string 79 { 80 $data = [ 81 [ 82 I18N::translate('Century'), 83 I18N::translate('Average number') 84 ] 85 ]; 86 87 foreach ($this->queryRecords() as $record) { 88 $data[] = [ 89 $this->centuryHelper->centuryName((int) $record->century), 90 (float) $record->num 91 ]; 92 } 93 94 $chart_title = I18N::translate('Average number of children per family'); 95 $chart_options = [ 96 'title' => $chart_title, 97 'subtitle' => '', 98 'legend' => [ 99 'position' => 'none', 100 ], 101 'vAxis' => [ 102 'title' => I18N::translate('Number of children'), 103 ], 104 'hAxis' => [ 105 'title' => I18N::translate('Century'), 106 ], 107 'colors' => [ 108 '#84beff' 109 ], 110 ]; 111 112 return view( 113 'statistics/other/charts/column', 114 [ 115 'data' => $data, 116 'chart_options' => $chart_options, 117 'chart_title' => $chart_title, 118 ] 119 ); 120 } 121} 122