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