xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 9483aecd71c61904a8b3a2a683a942c6571afde6)
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\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Database\Query\JoinClause;
28use Psr\Http\Message\ServerRequestInterface;
29use stdClass;
30
31use function app;
32use function assert;
33
34/**
35 * A chart showing the average number of children by century.
36 */
37class ChartChildren
38{
39    /**
40     * @var Tree
41     */
42    private $tree;
43
44    /**
45     * @var CenturyService
46     */
47    private $century_service;
48
49    /**
50     * Constructor.
51     *
52     * @param Tree $tree
53     */
54    public function __construct(Tree $tree)
55    {
56        $this->tree            = $tree;
57        $this->century_service = new CenturyService();
58    }
59
60    /**
61     * Returns the related database records.
62     *
63     * @return stdClass[]
64     */
65    private function queryRecords(): array
66    {
67        $query = DB::table('families')
68            ->selectRaw('ROUND(AVG(f_numchil), 2) AS num')
69            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
70            ->join('dates', static function (JoinClause $join): void {
71                $join->on('d_file', '=', 'f_file')
72                    ->on('d_gid', '=', 'f_id');
73            })
74            ->where('f_file', '=', $this->tree->id())
75            ->where('d_julianday1', '<>', 0)
76            ->where('d_fact', '=', 'MARR')
77            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
78            ->groupBy(['century'])
79            ->orderBy('century');
80
81        return $query->get()->all();
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((int) $record->century),
101                (float) $record->num
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        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
124        assert($locale instanceof LocaleInterface);
125
126        return view('statistics/other/charts/column', [
127            'data'          => $data,
128            'chart_options' => $chart_options,
129            'chart_title'   => $chart_title,
130            'language'      => $locale->languageTag(),
131        ]);
132    }
133}
134