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