xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
248add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
258add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
268add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause;
2734e3587bSGreg Roachuse Illuminate\Support\Collection;
286ccdf4f0SGreg Roachuse stdClass;
298add1155SRico Sonntag
308add1155SRico Sonntag/**
3193ccd686SRico Sonntag * A chart showing the average number of children by century.
328add1155SRico Sonntag */
3393ccd686SRico Sonntagclass ChartChildren
348add1155SRico Sonntag{
358add1155SRico Sonntag    /**
3693ccd686SRico Sonntag     * @var Tree
378add1155SRico Sonntag     */
3893ccd686SRico Sonntag    private $tree;
3993ccd686SRico Sonntag
4093ccd686SRico Sonntag    /**
4193ccd686SRico Sonntag     * @var CenturyService
4293ccd686SRico Sonntag     */
4393ccd686SRico Sonntag    private $century_service;
448add1155SRico Sonntag
458add1155SRico Sonntag    /**
468add1155SRico Sonntag     * Constructor.
478add1155SRico Sonntag     *
488add1155SRico Sonntag     * @param Tree $tree
498add1155SRico Sonntag     */
508add1155SRico Sonntag    public function __construct(Tree $tree)
518add1155SRico Sonntag    {
5293ccd686SRico Sonntag        $this->tree            = $tree;
5393ccd686SRico Sonntag        $this->century_service = new CenturyService();
548add1155SRico Sonntag    }
558add1155SRico Sonntag
568add1155SRico Sonntag    /**
578add1155SRico Sonntag     * Returns the related database records.
588add1155SRico Sonntag     *
5934e3587bSGreg Roach     * @return Collection<stdClass>
608add1155SRico Sonntag     */
6134e3587bSGreg Roach    private function queryRecords(): Collection
628add1155SRico Sonntag    {
6334e3587bSGreg Roach        return DB::table('families')
6434e3587bSGreg Roach            ->selectRaw('AVG(f_numchil) AS total')
653413ec75SRico Sonntag            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
660b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
678add1155SRico Sonntag                $join->on('d_file', '=', 'f_file')
688add1155SRico Sonntag                    ->on('d_gid', '=', 'f_id');
698add1155SRico Sonntag            })
708add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
718add1155SRico Sonntag            ->where('d_julianday1', '<>', 0)
728add1155SRico Sonntag            ->where('d_fact', '=', 'MARR')
738add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
748add1155SRico Sonntag            ->groupBy(['century'])
7534e3587bSGreg Roach            ->orderBy('century')
7634e3587bSGreg Roach            ->get()
7734e3587bSGreg Roach            ->map(static function (stdClass $row): stdClass {
7834e3587bSGreg Roach                return (object) [
7934e3587bSGreg Roach                    'century' => (int) $row->century,
8034e3587bSGreg Roach                    'total'   => (float) $row->total,
8134e3587bSGreg Roach                ];
8234e3587bSGreg Roach            });
838add1155SRico Sonntag    }
848add1155SRico Sonntag
858add1155SRico Sonntag    /**
8688de55fdSRico Sonntag     * Creates a children per family chart.
878add1155SRico Sonntag     *
888add1155SRico Sonntag     * @return string
898add1155SRico Sonntag     */
9088de55fdSRico Sonntag    public function chartChildren(): string
918add1155SRico Sonntag    {
9288de55fdSRico Sonntag        $data = [
9388de55fdSRico Sonntag            [
9488de55fdSRico Sonntag                I18N::translate('Century'),
9588de55fdSRico Sonntag                I18N::translate('Average number')
9688de55fdSRico Sonntag            ]
9788de55fdSRico Sonntag        ];
988add1155SRico Sonntag
9988de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
10088de55fdSRico Sonntag            $data[] = [
10134e3587bSGreg Roach                $this->century_service->centuryName($record->century),
10234e3587bSGreg Roach                round($record->total, 2),
10388de55fdSRico Sonntag            ];
1048add1155SRico Sonntag        }
1058add1155SRico Sonntag
106a81e5019SRico Sonntag        $chart_title   = I18N::translate('Average number of children per family');
107a81e5019SRico Sonntag        $chart_options = [
108a81e5019SRico Sonntag            'title' => $chart_title,
109a81e5019SRico Sonntag            'subtitle' => '',
110a81e5019SRico Sonntag            'legend' => [
111a81e5019SRico Sonntag                'position'  => 'none',
112a81e5019SRico Sonntag            ],
113a81e5019SRico Sonntag            'vAxis' => [
114a81e5019SRico Sonntag                'title' => I18N::translate('Number of children'),
115a81e5019SRico Sonntag            ],
116a81e5019SRico Sonntag            'hAxis' => [
117a81e5019SRico Sonntag                'title' => I18N::translate('Century'),
118a81e5019SRico Sonntag            ],
119a81e5019SRico Sonntag            'colors' => [
120a81e5019SRico Sonntag                '#84beff'
121a81e5019SRico Sonntag            ],
122a81e5019SRico Sonntag        ];
123a81e5019SRico Sonntag
12490a2f718SGreg Roach        return view('statistics/other/charts/column', [
12588de55fdSRico Sonntag            'data'          => $data,
126a81e5019SRico Sonntag            'chart_options' => $chart_options,
127a81e5019SRico Sonntag            'chart_title'   => $chart_title,
12865cf5706SGreg Roach            'language'      => I18N::languageTag(),
12990a2f718SGreg Roach        ]);
1308add1155SRico Sonntag    }
1318add1155SRico Sonntag}
132