xref: /webtrees/app/Statistics/Google/ChartDeath.php (revision b3087c253cf6a932d19775d2730fe656c3008d7f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Support\Collection;
28use stdClass;
29
30use function count;
31use function view;
32
33/**
34 * A chart showing the deaths by century.
35 */
36class ChartDeath
37{
38    private Tree $tree;
39
40    private CenturyService $century_service;
41
42    private ColorService $color_service;
43
44    /**
45     * @param CenturyService $century_service
46     * @param ColorService   $color_service
47     * @param Tree           $tree
48     */
49    public function __construct(CenturyService $century_service, ColorService $color_service, Tree $tree)
50    {
51        $this->century_service = $century_service;
52        $this->color_service   = $color_service;
53        $this->tree            = $tree;
54    }
55
56    /**
57     * Returns the related database records.
58     *
59     * @return Collection<array-key,stdClass>
60     */
61    private function queryRecords(): Collection
62    {
63        return DB::table('dates')
64            ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century')
65            ->selectRaw('COUNT(*) AS total')
66            ->where('d_file', '=', $this->tree->id())
67            ->where('d_year', '<>', 0)
68            ->where('d_fact', '=', 'DEAT')
69            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
70            ->groupBy(['century'])
71            ->orderBy('century')
72            ->get()
73            ->map(static fn (object $row): object => (object) [
74                'century' => (int) $row->century,
75                'total'   => (float) $row->total,
76            ]);
77    }
78
79    /**
80     * Create a chart of death places.
81     *
82     * @param string|null $color_from
83     * @param string|null $color_to
84     *
85     * @return string
86     */
87    public function chartDeath(string|null $color_from = null, string|null $color_to = null): string
88    {
89        $color_from ??= 'ffffff';
90        $color_to ??= '84beff';
91
92        $data = [
93            [
94                I18N::translate('Century'),
95                I18N::translate('Total')
96            ],
97        ];
98
99        foreach ($this->queryRecords() as $record) {
100            $data[] = [
101                $this->century_service->centuryName($record->century),
102                $record->total
103            ];
104        }
105
106        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
107
108        return view('statistics/other/charts/pie', [
109            'title'    => I18N::translate('Deaths by century'),
110            'data'     => $data,
111            'colors'   => $colors,
112            'language' => I18N::languageTag(),
113        ]);
114    }
115}
116