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