xref: /webtrees/app/Statistics/Google/ChartMortality.php (revision 7c4add84379afdbaa7c4c272763673edc20fb830)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\ColorService;
25use Psr\Http\Message\ServerRequestInterface;
26
27use function app;
28use function assert;
29use function count;
30
31/**
32 * A chart showing the mortality.
33 */
34class ChartMortality
35{
36    /**
37     * @var ColorService
38     */
39    private $color_service;
40
41    /**
42     * Constructor.
43     */
44    public function __construct()
45    {
46        $this->color_service = new ColorService();
47    }
48
49    /**
50     * Create a chart showing mortality.
51     *
52     * @param int         $tot_l
53     * @param int         $tot_d
54     * @param string|null $color_living
55     * @param string|null $color_dead
56     *
57     * @return string
58     */
59    public function chartMortality(
60        int $tot_l,
61        int $tot_d,
62        string $color_living = null,
63        string $color_dead = null
64    ): string {
65        $color_living = $color_living ?? '#ffffff';
66        $color_dead   = $color_dead   ?? '#cccccc';
67
68        $data = [
69            [
70                I18N::translate('Century'),
71                I18N::translate('Total')
72            ]
73        ];
74
75        if ($tot_l || $tot_d) {
76            $data[] = [
77                I18N::translate('Living'),
78                $tot_l
79            ];
80
81            $data[] =  [
82                I18N::translate('Dead'),
83                $tot_d
84            ];
85        }
86
87        $colors = $this->color_service->interpolateRgb($color_living, $color_dead, count($data) - 1);
88
89        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
90        assert($locale instanceof LocaleInterface);
91
92        return view('statistics/other/charts/pie', [
93            'title'            => null,
94            'data'             => $data,
95            'colors'           => $colors,
96            'labeledValueText' => 'percentage',
97            'language'         => $locale->languageTag(),
98        ]);
99    }
100}
101