xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision dc270d8cc5085ed29f3f419c99734ddd0960c624)
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;
28use stdClass;
29
30/**
31 * A chart showing the average number of children by century.
32 */
33class ChartChildren
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 Collection<stdClass>
60     */
61    private function queryRecords(): Collection
62    {
63        return DB::table('families')
64            ->selectRaw('AVG(f_numchil) AS total')
65            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
66            ->join('dates', static function (JoinClause $join): void {
67                $join->on('d_file', '=', 'f_file')
68                    ->on('d_gid', '=', 'f_id');
69            })
70            ->where('f_file', '=', $this->tree->id())
71            ->where('d_julianday1', '<>', 0)
72            ->where('d_fact', '=', 'MARR')
73            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
74            ->groupBy(['century'])
75            ->orderBy('century')
76            ->get()
77            ->map(static function (stdClass $row): stdClass {
78                return (object) [
79                    'century' => (int) $row->century,
80                    'total'   => (float) $row->total,
81                ];
82            });
83    }
84
85    /**
86     * Creates a children per family chart.
87     *
88     * @return string
89     */
90    public function chartChildren(): string
91    {
92        $data = [
93            [
94                I18N::translate('Century'),
95                I18N::translate('Average number')
96            ]
97        ];
98
99        foreach ($this->queryRecords() as $record) {
100            $data[] = [
101                $this->century_service->centuryName($record->century),
102                round($record->total, 2),
103            ];
104        }
105
106        $chart_title   = I18N::translate('Average number of children per family');
107        $chart_options = [
108            'title' => $chart_title,
109            'subtitle' => '',
110            'legend' => [
111                'position'  => 'none',
112            ],
113            'vAxis' => [
114                'title' => I18N::translate('Number of children'),
115            ],
116            'hAxis' => [
117                'title' => I18N::translate('Century'),
118            ],
119            'colors' => [
120                '#84beff'
121            ],
122        ];
123
124        return view('statistics/other/charts/column', [
125            'data'          => $data,
126            'chart_options' => $chart_options,
127            'chart_title'   => $chart_title,
128            'language'      => I18N::languageTag(),
129        ]);
130    }
131}
132