xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 362b84648b116e80ad8951484a8cb04147899fad)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Statistics\Service\CenturyService;
22use Fisharebest\Webtrees\Tree;
23use Illuminate\Database\Capsule\Manager as DB;
24use Illuminate\Database\Query\JoinClause;
25
26/**
27 * A chart showing the average number of children by century.
28 */
29class ChartChildren
30{
31    /**
32     * @var Tree
33     */
34    private $tree;
35
36    /**
37     * @var CenturyService
38     */
39    private $century_service;
40
41    /**
42     * Constructor.
43     *
44     * @param Tree $tree
45     */
46    public function __construct(Tree $tree)
47    {
48        $this->tree            = $tree;
49        $this->century_service = new CenturyService();
50    }
51
52    /**
53     * Returns the related database records.
54     *
55     * @return \stdClass[]
56     */
57    private function queryRecords(): array
58    {
59        $query = DB::table('families')
60            ->selectRaw('ROUND(AVG(f_numchil), 2) AS num')
61            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
62            ->join('dates', function (JoinClause $join): void {
63                $join->on('d_file', '=', 'f_file')
64                    ->on('d_gid', '=', 'f_id');
65            })
66            ->where('f_file', '=', $this->tree->id())
67            ->where('d_julianday1', '<>', 0)
68            ->where('d_fact', '=', 'MARR')
69            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
70            ->groupBy(['century'])
71            ->orderBy('century');
72
73        return $query->get()->all();
74    }
75
76    /**
77     * Creates a children per family chart.
78     *
79     * @return string
80     */
81    public function chartChildren(): string
82    {
83        $data = [
84            [
85                I18N::translate('Century'),
86                I18N::translate('Average number')
87            ]
88        ];
89
90        foreach ($this->queryRecords() as $record) {
91            $data[] = [
92                $this->century_service->centuryName((int) $record->century),
93                (float) $record->num
94            ];
95        }
96
97        $chart_title   = I18N::translate('Average number of children per family');
98        $chart_options = [
99            'title' => $chart_title,
100            'subtitle' => '',
101            'legend' => [
102                'position'  => 'none',
103            ],
104            'vAxis' => [
105                'title' => I18N::translate('Number of children'),
106            ],
107            'hAxis' => [
108                'title' => I18N::translate('Century'),
109            ],
110            'colors' => [
111                '#84beff'
112            ],
113        ];
114
115        return view(
116            'statistics/other/charts/column',
117            [
118                'data'          => $data,
119                'chart_options' => $chart_options,
120                'chart_title'   => $chart_title,
121            ]
122        );
123    }
124}
125