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\Statistics\Service\ColorService; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Support\Collection; 28 29use function count; 30use function view; 31 32/** 33 * A chart showing birth by century. 34 */ 35class ChartBirth 36{ 37 private Tree $tree; 38 39 private CenturyService $century_service; 40 41 private ColorService $color_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 $this->color_service = new ColorService(); 53 } 54 55 /** 56 * Returns the related database records. 57 * 58 * @return Collection<object> 59 */ 60 private function queryRecords(): Collection 61 { 62 return DB::table('dates') 63 ->selectRaw('ROUND((d_year + 49) / 100) AS century') 64 ->selectRaw('COUNT(*) AS total') 65 ->where('d_file', '=', $this->tree->id()) 66 ->where('d_year', '<>', 0) 67 ->where('d_fact', '=', 'BIRT') 68 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 69 ->groupBy(['century']) 70 ->orderBy('century') 71 ->get() 72 ->map(static function (object $row): object { 73 return (object) [ 74 'century' => (int) $row->century, 75 'total' => (float) $row->total, 76 ]; 77 }); 78 } 79 80 /** 81 * Create a chart of birth places. 82 * 83 * @param string|null $color_from 84 * @param string|null $color_to 85 * 86 * @return string 87 */ 88 public function chartBirth(string $color_from = null, string $color_to = null): string 89 { 90 $color_from = $color_from ?? 'ffffff'; 91 $color_to = $color_to ?? '84beff'; 92 93 $data = [ 94 [ 95 I18N::translate('Century'), 96 I18N::translate('Total') 97 ], 98 ]; 99 100 foreach ($this->queryRecords() as $record) { 101 $data[] = [ 102 $this->century_service->centuryName($record->century), 103 $record->total 104 ]; 105 } 106 107 $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); 108 109 return view('statistics/other/charts/pie', [ 110 'title' => I18N::translate('Births by century'), 111 'data' => $data, 112 'colors' => $colors, 113 'language' => I18N::languageTag(), 114 ]); 115 } 116} 117