xref: /webtrees/app/Statistics/Google/ChartMortality.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
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\Statistics\Service\ColorService;
24
25use function count;
26
27/**
28 * A chart showing the mortality.
29 */
30class ChartMortality
31{
32    /**
33     * @var ColorService
34     */
35    private $color_service;
36
37    /**
38     * Constructor.
39     */
40    public function __construct()
41    {
42        $this->color_service = new ColorService();
43    }
44
45    /**
46     * Create a chart showing mortality.
47     *
48     * @param int         $tot_l
49     * @param int         $tot_d
50     * @param string|null $color_living
51     * @param string|null $color_dead
52     *
53     * @return string
54     */
55    public function chartMortality(
56        int $tot_l,
57        int $tot_d,
58        string $color_living = null,
59        string $color_dead = null
60    ): string {
61        $color_living = $color_living ?? '#ffffff';
62        $color_dead   = $color_dead   ?? '#cccccc';
63
64        $data = [
65            [
66                I18N::translate('Century'),
67                I18N::translate('Total')
68            ]
69        ];
70
71        if ($tot_l || $tot_d) {
72            $data[] = [
73                I18N::translate('Living'),
74                $tot_l
75            ];
76
77            $data[] =  [
78                I18N::translate('Dead'),
79                $tot_d
80            ];
81        }
82
83        $colors = $this->color_service->interpolateRgb($color_living, $color_dead, count($data) - 1);
84
85        return view('statistics/other/charts/pie', [
86            'title'            => null,
87            'data'             => $data,
88            'colors'           => $colors,
89            'labeledValueText' => 'percentage',
90            'language'         => I18N::languageTag(),
91        ]);
92    }
93}
94