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