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