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