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