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