xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 65cf57062fc712114f930335533210fd95170cea)
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\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 stdClass;
28
29/**
30 * A chart showing the average number of children by century.
31 */
32class ChartChildren
33{
34    /**
35     * @var Tree
36     */
37    private $tree;
38
39    /**
40     * @var CenturyService
41     */
42    private $century_service;
43
44    /**
45     * Constructor.
46     *
47     * @param Tree $tree
48     */
49    public function __construct(Tree $tree)
50    {
51        $this->tree            = $tree;
52        $this->century_service = new CenturyService();
53    }
54
55    /**
56     * Returns the related database records.
57     *
58     * @return stdClass[]
59     */
60    private function queryRecords(): array
61    {
62        $query = DB::table('families')
63            ->selectRaw('ROUND(AVG(f_numchil), 2) AS num')
64            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
65            ->join('dates', static function (JoinClause $join): void {
66                $join->on('d_file', '=', 'f_file')
67                    ->on('d_gid', '=', 'f_id');
68            })
69            ->where('f_file', '=', $this->tree->id())
70            ->where('d_julianday1', '<>', 0)
71            ->where('d_fact', '=', 'MARR')
72            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
73            ->groupBy(['century'])
74            ->orderBy('century');
75
76        return $query->get()->all();
77    }
78
79    /**
80     * Creates a children per family chart.
81     *
82     * @return string
83     */
84    public function chartChildren(): string
85    {
86        $data = [
87            [
88                I18N::translate('Century'),
89                I18N::translate('Average number')
90            ]
91        ];
92
93        foreach ($this->queryRecords() as $record) {
94            $data[] = [
95                $this->century_service->centuryName((int) $record->century),
96                (float) $record->num
97            ];
98        }
99
100        $chart_title   = I18N::translate('Average number of children per family');
101        $chart_options = [
102            'title' => $chart_title,
103            'subtitle' => '',
104            'legend' => [
105                'position'  => 'none',
106            ],
107            'vAxis' => [
108                'title' => I18N::translate('Number of children'),
109            ],
110            'hAxis' => [
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