xref: /webtrees/app/Statistics/Google/ChartDeath.php (revision f70bcff589628705e50ed72a6fc6cbad68558677)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\I18N;
23use Fisharebest\Webtrees\Module\ModuleThemeInterface;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Database\Capsule\Manager as DB;
28use Illuminate\Support\Collection;
29
30use function app;
31use function count;
32
33/**
34 * A chart showing the deaths by century.
35 */
36class ChartDeath
37{
38    /**
39     * @var Tree
40     */
41    private $tree;
42
43    /**
44     * @var ModuleThemeInterface
45     */
46    private $theme;
47
48    /**
49     * @var CenturyService
50     */
51    private $century_service;
52
53    /**
54     * @var ColorService
55     */
56    private $color_service;
57
58    /**
59     * Constructor.
60     *
61     * @param Tree $tree
62     */
63    public function __construct(Tree $tree)
64    {
65        $this->tree            = $tree;
66        $this->theme           = app(ModuleThemeInterface::class);
67        $this->century_service = new CenturyService();
68        $this->color_service   = new ColorService();
69    }
70
71    /**
72     * Returns the related database records.
73     *
74     * @return Collection<object>
75     */
76    private function queryRecords(): Collection
77    {
78        return DB::table('dates')
79            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
80            ->selectRaw('COUNT(*) AS total')
81            ->where('d_file', '=', $this->tree->id())
82            ->where('d_year', '<>', 0)
83            ->where('d_fact', '=', 'DEAT')
84            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
85            ->groupBy(['century'])
86            ->orderBy('century')
87            ->get()
88            ->map(static function (object $row): object {
89                return (object) [
90                    'century' => (int) $row->century,
91                    'total'   => (float) $row->total,
92                ];
93            });
94    }
95
96    /**
97     * Create a chart of death places.
98     *
99     * @param string|null $color_from
100     * @param string|null $color_to
101     *
102     * @return string
103     */
104    public function chartDeath(string $color_from = null, string $color_to = null): string
105    {
106        $chart_color1 = $this->theme->parameter('distribution-chart-no-values');
107        $chart_color2 = $this->theme->parameter('distribution-chart-high-values');
108        $color_from   = $color_from ?? $chart_color1;
109        $color_to     = $color_to   ?? $chart_color2;
110
111        $data = [
112            [
113                I18N::translate('Century'),
114                I18N::translate('Total')
115            ],
116        ];
117
118        foreach ($this->queryRecords() as $record) {
119            $data[] = [
120                $this->century_service->centuryName($record->century),
121                $record->total
122            ];
123        }
124
125        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
126
127        return view('statistics/other/charts/pie', [
128            'title'    => I18N::translate('Deaths by century'),
129            'data'     => $data,
130            'colors'   => $colors,
131            'language' => I18N::languageTag(),
132        ]);
133    }
134}
135