xref: /webtrees/app/Statistics/Google/ChartAge.php (revision 102b4ac44f212e64db4f1856a1da4ed1949a71ef)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\DB;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Query\Expression;
27use Illuminate\Database\Query\JoinClause;
28use Illuminate\Support\Collection;
29
30use function round;
31use function view;
32
33/**
34 * A chart showing the average age of individuals related to the death century.
35 */
36class ChartAge
37{
38    private Tree $tree;
39
40    private CenturyService $century_service;
41
42    /**
43     * @param CenturyService $century_service
44     * @param Tree           $tree
45     */
46    public function __construct(CenturyService $century_service, Tree $tree)
47    {
48        $this->century_service = $century_service;
49        $this->tree            = $tree;
50    }
51
52    /**
53     * Returns the related database records.
54     *
55     * @return Collection<array-key,object>
56     */
57    private function queryRecords(): Collection
58    {
59        return DB::table('individuals')
60            ->select([
61                new Expression('AVG(' . DB::prefix('death.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ') / 365.25 AS age'),
62                new Expression('ROUND((' . DB::prefix('death.d_year') . ' + 49) / 100, 0) AS century'),
63                'i_sex AS sex'
64            ])
65            ->join('dates AS birth', static function (JoinClause $join): void {
66                $join
67                    ->on('birth.d_file', '=', 'i_file')
68                    ->on('birth.d_gid', '=', 'i_id');
69            })
70            ->join('dates AS death', static function (JoinClause $join): void {
71                $join
72                    ->on('death.d_file', '=', 'i_file')
73                    ->on('death.d_gid', '=', 'i_id');
74            })
75            ->where('i_file', '=', $this->tree->id())
76            ->where('birth.d_fact', '=', 'BIRT')
77            ->where('death.d_fact', '=', 'DEAT')
78            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
79            ->whereIn('death.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
80            ->whereColumn('death.d_julianday1', '>=', 'birth.d_julianday2')
81            ->where('birth.d_julianday2', '<>', 0)
82            ->groupBy(['century', 'sex'])
83            ->orderBy('century')
84            ->orderBy('sex')
85            ->get()
86            ->map(static fn (object $row): object => (object) [
87                'age'     => (float) $row->age,
88                'century' => (int) $row->century,
89                'sex'     => $row->sex,
90            ]);
91    }
92
93    /**
94     * General query on ages.
95     *
96     * @return string
97     */
98    public function chartAge(): string
99    {
100        $out = [];
101        foreach ($this->queryRecords() as $record) {
102            $out[$record->century][$record->sex] = $record->age;
103        }
104
105        $data = [
106            [
107                I18N::translate('Century'),
108                I18N::translate('Males'),
109                I18N::translate('Females'),
110                I18N::translate('Average age'),
111            ]
112        ];
113
114        foreach ($out as $century => $values) {
115            $female_age  = $values['F'] ?? 0;
116            $male_age    = $values['M'] ?? 0;
117            $average_age = ($female_age + $male_age) / 2.0;
118
119            $data[] = [
120                $this->century_service->centuryName($century),
121                round($male_age, 1),
122                round($female_age, 1),
123                round($average_age, 1),
124            ];
125        }
126
127        $chart_title   = I18N::translate('Average age related to death century');
128        $chart_options = [
129            'title' => $chart_title,
130            'subtitle' => I18N::translate('Average age at death'),
131            'vAxis' => [
132                'title' => I18N::translate('Age'),
133            ],
134            'hAxis' => [
135                'showTextEvery' => 1,
136                'slantedText'   => false,
137                'title'         => I18N::translate('Century'),
138            ],
139            'colors' => [
140                '#84beff',
141                '#ffd1dc',
142                '#ff0000',
143            ],
144        ];
145
146        return view('statistics/other/charts/combo', [
147            'data'          => $data,
148            'chart_options' => $chart_options,
149            'chart_title'   => $chart_title,
150            'language'      => I18N::languageTag(),
151        ]);
152    }
153}
154