xref: /webtrees/app/Statistics/Google/ChartBirth.php (revision d7c2f5cc56f17801156c069987440ca1da2c5057)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
268add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
2734e3587bSGreg Roachuse Illuminate\Support\Collection;
288add1155SRico Sonntag
2971378461SGreg Roachuse function count;
3034b20f29SGreg Roachuse function view;
3171378461SGreg Roach
328add1155SRico Sonntag/**
3393ccd686SRico Sonntag * A chart showing birth by century.
348add1155SRico Sonntag */
3593ccd686SRico Sonntagclass ChartBirth
368add1155SRico Sonntag{
3734b20f29SGreg Roach    private Tree $tree;
3893ccd686SRico Sonntag
3934b20f29SGreg Roach    private CenturyService $century_service;
4093ccd686SRico Sonntag
4134b20f29SGreg Roach    private ColorService $color_service;
428add1155SRico Sonntag
438add1155SRico Sonntag    /**
44f78da678SGreg Roach     * @param CenturyService $century_service
45f78da678SGreg Roach     * @param ColorService   $color_service
468add1155SRico Sonntag     * @param Tree           $tree
478add1155SRico Sonntag     */
48f78da678SGreg Roach    public function __construct(CenturyService $century_service, ColorService $color_service, Tree $tree)
498add1155SRico Sonntag    {
50f78da678SGreg Roach        $this->century_service = $century_service;
51f78da678SGreg Roach        $this->color_service   = $color_service;
5293ccd686SRico Sonntag        $this->tree            = $tree;
538add1155SRico Sonntag    }
548add1155SRico Sonntag
558add1155SRico Sonntag    /**
568add1155SRico Sonntag     * Returns the related database records.
578add1155SRico Sonntag     *
58*d7c2f5ccSGreg Roach     * @return Collection<array-key,object>
598add1155SRico Sonntag     */
6034e3587bSGreg Roach    private function queryRecords(): Collection
618add1155SRico Sonntag    {
6234e3587bSGreg Roach        return DB::table('dates')
63cde98219SGreg Roach            ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century')
648add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
658add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
668add1155SRico Sonntag            ->where('d_year', '<>', 0)
678add1155SRico Sonntag            ->where('d_fact', '=', 'BIRT')
688add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
698add1155SRico Sonntag            ->groupBy(['century'])
7034e3587bSGreg Roach            ->orderBy('century')
7134e3587bSGreg Roach            ->get()
72f25fc0f9SGreg Roach            ->map(static fn (object $row): object => (object) [
7334e3587bSGreg Roach                'century' => (int) $row->century,
7434e3587bSGreg Roach                'total'   => (float) $row->total,
75f25fc0f9SGreg Roach            ]);
768add1155SRico Sonntag    }
778add1155SRico Sonntag
788add1155SRico Sonntag    /**
798add1155SRico Sonntag     * Create a chart of birth places.
808add1155SRico Sonntag     *
818add1155SRico Sonntag     * @param string|null $color_from
828add1155SRico Sonntag     * @param string|null $color_to
838add1155SRico Sonntag     *
848add1155SRico Sonntag     * @return string
858add1155SRico Sonntag     */
862c6f1bd5SGreg Roach    public function chartBirth(string|null $color_from = null, string|null $color_to = null): string
878add1155SRico Sonntag    {
883529c469SGreg Roach        $color_from ??= 'ffffff';
893529c469SGreg Roach        $color_to ??= '84beff';
908add1155SRico Sonntag
9188de55fdSRico Sonntag        $data = [
9288de55fdSRico Sonntag            [
9388de55fdSRico Sonntag                I18N::translate('Century'),
9488de55fdSRico Sonntag                I18N::translate('Total')
9588de55fdSRico Sonntag            ],
9688de55fdSRico Sonntag        ];
978add1155SRico Sonntag
9888de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
9988de55fdSRico Sonntag            $data[] = [
10034e3587bSGreg Roach                $this->century_service->centuryName($record->century),
10188de55fdSRico Sonntag                $record->total
10288de55fdSRico Sonntag            ];
1038add1155SRico Sonntag        }
1048add1155SRico Sonntag
1056ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1068add1155SRico Sonntag
10790a2f718SGreg Roach        return view('statistics/other/charts/pie', [
10888de55fdSRico Sonntag            'title'    => I18N::translate('Births by century'),
10988de55fdSRico Sonntag            'data'     => $data,
11088de55fdSRico Sonntag            'colors'   => $colors,
11165cf5706SGreg Roach            'language' => I18N::languageTag(),
11290a2f718SGreg Roach        ]);
1138add1155SRico Sonntag    }
1148add1155SRico Sonntag}
115