xref: /webtrees/app/Statistics/Google/ChartDeath.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\Module\ModuleThemeInterface;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
268add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
278add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
2834e3587bSGreg Roachuse Illuminate\Support\Collection;
296ccdf4f0SGreg Roachuse stdClass;
308add1155SRico Sonntag
3190a2f718SGreg Roachuse function app;
3271378461SGreg Roachuse function count;
3371378461SGreg Roach
348add1155SRico Sonntag/**
3593ccd686SRico Sonntag * A chart showing the deaths by century.
368add1155SRico Sonntag */
3793ccd686SRico Sonntagclass ChartDeath
388add1155SRico Sonntag{
398add1155SRico Sonntag    /**
4093ccd686SRico Sonntag     * @var Tree
418add1155SRico Sonntag     */
4293ccd686SRico Sonntag    private $tree;
4393ccd686SRico Sonntag
4493ccd686SRico Sonntag    /**
4593ccd686SRico Sonntag     * @var ModuleThemeInterface
4693ccd686SRico Sonntag     */
4793ccd686SRico Sonntag    private $theme;
4893ccd686SRico Sonntag
4993ccd686SRico Sonntag    /**
5093ccd686SRico Sonntag     * @var CenturyService
5193ccd686SRico Sonntag     */
5293ccd686SRico Sonntag    private $century_service;
5393ccd686SRico Sonntag
5493ccd686SRico Sonntag    /**
5593ccd686SRico Sonntag     * @var ColorService
5693ccd686SRico Sonntag     */
5793ccd686SRico Sonntag    private $color_service;
588add1155SRico Sonntag
598add1155SRico Sonntag    /**
608add1155SRico Sonntag     * Constructor.
618add1155SRico Sonntag     *
628add1155SRico Sonntag     * @param Tree $tree
638add1155SRico Sonntag     */
648add1155SRico Sonntag    public function __construct(Tree $tree)
658add1155SRico Sonntag    {
6693ccd686SRico Sonntag        $this->tree            = $tree;
67cab242e7SGreg Roach        $this->theme           = app(ModuleThemeInterface::class);
6893ccd686SRico Sonntag        $this->century_service = new CenturyService();
6993ccd686SRico Sonntag        $this->color_service   = new ColorService();
708add1155SRico Sonntag    }
718add1155SRico Sonntag
728add1155SRico Sonntag    /**
738add1155SRico Sonntag     * Returns the related database records.
748add1155SRico Sonntag     *
7534e3587bSGreg Roach     * @return Collection<stdClass>
768add1155SRico Sonntag     */
7734e3587bSGreg Roach    private function queryRecords(): Collection
788add1155SRico Sonntag    {
7934e3587bSGreg Roach        return DB::table('dates')
803413ec75SRico Sonntag            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
818add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
828add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
838add1155SRico Sonntag            ->where('d_year', '<>', 0)
848add1155SRico Sonntag            ->where('d_fact', '=', 'DEAT')
858add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
868add1155SRico Sonntag            ->groupBy(['century'])
8734e3587bSGreg Roach            ->orderBy('century')
8834e3587bSGreg Roach            ->get()
8934e3587bSGreg Roach            ->map(static function (stdClass $row): stdClass {
9034e3587bSGreg Roach                return (object) [
9134e3587bSGreg Roach                    'century' => (int) $row->century,
9234e3587bSGreg Roach                    'total'   => (float) $row->total,
9334e3587bSGreg Roach                ];
9434e3587bSGreg Roach            });
958add1155SRico Sonntag    }
968add1155SRico Sonntag
978add1155SRico Sonntag    /**
988add1155SRico Sonntag     * Create a chart of death places.
998add1155SRico Sonntag     *
1008add1155SRico Sonntag     * @param string|null $color_from
1018add1155SRico Sonntag     * @param string|null $color_to
1028add1155SRico Sonntag     *
1038add1155SRico Sonntag     * @return string
1048add1155SRico Sonntag     */
10588de55fdSRico Sonntag    public function chartDeath(string $color_from = null, string $color_to = null): string
1068add1155SRico Sonntag    {
10788de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
10888de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
1098add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
1108add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
1118add1155SRico Sonntag
11288de55fdSRico Sonntag        $data = [
11388de55fdSRico Sonntag            [
11488de55fdSRico Sonntag                I18N::translate('Century'),
11588de55fdSRico Sonntag                I18N::translate('Total')
11688de55fdSRico Sonntag            ],
11788de55fdSRico Sonntag        ];
1188add1155SRico Sonntag
11988de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
12088de55fdSRico Sonntag            $data[] = [
12134e3587bSGreg Roach                $this->century_service->centuryName($record->century),
12288de55fdSRico Sonntag                $record->total
12388de55fdSRico Sonntag            ];
1248add1155SRico Sonntag        }
1258add1155SRico Sonntag
1266ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1278add1155SRico Sonntag
12890a2f718SGreg Roach        return view('statistics/other/charts/pie', [
12988de55fdSRico Sonntag            'title'    => I18N::translate('Deaths by century'),
13088de55fdSRico Sonntag            'data'     => $data,
13188de55fdSRico Sonntag            'colors'   => $colors,
13265cf5706SGreg Roach            'language' => I18N::languageTag(),
13390a2f718SGreg Roach        ]);
1348add1155SRico Sonntag    }
1358add1155SRico Sonntag}
136