xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 1f1ffa65b3b51df2b95b5c68894525436855964a)
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;
25use stdClass;
26
27/**
28 * A chart showing the average number of children by century.
29 */
30class ChartChildren
31{
32    /**
33     * @var Tree
34     */
35    private $tree;
36
37    /**
38     * @var CenturyService
39     */
40    private $century_service;
41
42    /**
43     * Constructor.
44     *
45     * @param Tree $tree
46     */
47    public function __construct(Tree $tree)
48    {
49        $this->tree            = $tree;
50        $this->century_service = new CenturyService();
51    }
52
53    /**
54     * Returns the related database records.
55     *
56     * @return stdClass[]
57     */
58    private function queryRecords(): array
59    {
60        $query = DB::table('families')
61            ->selectRaw('ROUND(AVG(f_numchil), 2) AS num')
62            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
63            ->join('dates', static function (JoinClause $join): void {
64                $join->on('d_file', '=', 'f_file')
65                    ->on('d_gid', '=', 'f_id');
66            })
67            ->where('f_file', '=', $this->tree->id())
68            ->where('d_julianday1', '<>', 0)
69            ->where('d_fact', '=', 'MARR')
70            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
71            ->groupBy(['century'])
72            ->orderBy('century');
73
74        return $query->get()->all();
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((int) $record->century),
94                (float) $record->num
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                'title' => I18N::translate('Century'),
110            ],
111            'colors' => [
112                '#84beff'
113            ],
114        ];
115
116        return view(
117            'statistics/other/charts/column',
118            [
119                'data'          => $data,
120                'chart_options' => $chart_options,
121                'chart_title'   => $chart_title,
122            ]
123        );
124    }
125}
126