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