xref: /webtrees/app/Statistics/Google/ChartDeath.php (revision 40217a61a2f92b29f16fd9c08e85ae48a5ff6a06)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Statistics\Helper\Century;
22use Fisharebest\Webtrees\Statistics\AbstractGoogle;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Database\Capsule\Manager as DB;
25
26/**
27 *
28 */
29class ChartDeath extends AbstractGoogle
30{
31    /**
32     * @var Century
33     */
34    private $centuryHelper;
35
36    /**
37     * Constructor.
38     *
39     * @param Tree $tree
40     */
41    public function __construct(Tree $tree)
42    {
43        parent::__construct($tree);
44
45        $this->centuryHelper = new Century();
46    }
47
48    /**
49     * Returns the related database records.
50     *
51     * @return \stdClass[]
52     */
53    private function queryRecords(): array
54    {
55        $query = DB::table('dates')
56            ->selectRaw('ROUND((d_year - 50) / 100) AS century')
57            ->selectRaw('COUNT(*) AS total')
58            ->where('d_file', '=', $this->tree->id())
59            ->where('d_year', '<>', 0)
60            ->where('d_fact', '=', 'DEAT')
61            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
62            ->groupBy(['century'])
63            ->orderBy('century');
64
65        return $query->get()->all();
66    }
67
68    /**
69     * Create a chart of death places.
70     *
71     * @param string|null $color_from
72     * @param string|null $color_to
73     *
74     * @return string
75     */
76    public function chartDeath(string $color_from = null, string $color_to = null): string
77    {
78        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
79        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
80        $color_from   = $color_from ?? $chart_color1;
81        $color_to     = $color_to   ?? $chart_color2;
82
83        $data = [
84            [
85                I18N::translate('Century'),
86                I18N::translate('Total')
87            ],
88        ];
89
90        foreach ($this->queryRecords() as $record) {
91            $data[] = [
92                $this->centuryHelper->centuryName((int) $record->century),
93                $record->total
94            ];
95        }
96
97        $colors = $this->interpolateRgb($color_from, $color_to, \count($data) - 1);
98
99        return view(
100            'statistics/other/charts/pie',
101            [
102                'title'  => I18N::translate('Deaths by century'),
103                'data'   => $data,
104                'colors' => $colors,
105            ]
106        );
107    }
108}
109