xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 34b20f294deb14f65541b617e67c4eb4ff84d013)
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\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Database\Query\JoinClause;
27use Illuminate\Support\Collection;
28
29/**
30 * A chart showing the average number of children by century.
31 */
32class ChartChildren
33{
34    private Tree $tree;
35
36    private CenturyService $century_service;
37
38    /**
39     * Constructor.
40     *
41     * @param Tree $tree
42     */
43    public function __construct(Tree $tree)
44    {
45        $this->tree            = $tree;
46        $this->century_service = new CenturyService();
47    }
48
49    /**
50     * Returns the related database records.
51     *
52     * @return Collection<object>
53     */
54    private function queryRecords(): Collection
55    {
56        return DB::table('families')
57            ->selectRaw('AVG(f_numchil) AS total')
58            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
59            ->join('dates', static function (JoinClause $join): void {
60                $join->on('d_file', '=', 'f_file')
61                    ->on('d_gid', '=', 'f_id');
62            })
63            ->where('f_file', '=', $this->tree->id())
64            ->where('d_julianday1', '<>', 0)
65            ->where('d_fact', '=', 'MARR')
66            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
67            ->groupBy(['century'])
68            ->orderBy('century')
69            ->get()
70            ->map(static function (object $row): object {
71                return (object) [
72                    'century' => (int) $row->century,
73                    'total'   => (float) $row->total,
74                ];
75            });
76    }
77
78    /**
79     * Creates a children per family chart.
80     *
81     * @return string
82     */
83    public function chartChildren(): string
84    {
85        $data = [
86            [
87                I18N::translate('Century'),
88                I18N::translate('Average number')
89            ]
90        ];
91
92        foreach ($this->queryRecords() as $record) {
93            $data[] = [
94                $this->century_service->centuryName($record->century),
95                round($record->total, 2),
96            ];
97        }
98
99        $chart_title   = I18N::translate('Average number of children per family');
100        $chart_options = [
101            'title' => $chart_title,
102            'subtitle' => '',
103            'legend' => [
104                'position'  => 'none',
105            ],
106            'vAxis' => [
107                'title' => I18N::translate('Number of children'),
108            ],
109            'hAxis' => [
110                'title' => I18N::translate('Century'),
111            ],
112            'colors' => [
113                '#84beff'
114            ],
115        ];
116
117        return view('statistics/other/charts/column', [
118            'data'          => $data,
119            'chart_options' => $chart_options,
120            'chart_title'   => $chart_title,
121            'language'      => I18N::languageTag(),
122        ]);
123    }
124}
125