xref: /webtrees/app/Statistics/Google/ChartMortality.php (revision 77a2010793e07b8d01d1a336fa5967181694729d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Module\ModuleThemeInterface;
22use Fisharebest\Webtrees\Statistics\AbstractGoogle;
23use Fisharebest\Webtrees\Statistics\Repository\IndividualRepository;
24use Fisharebest\Webtrees\Theme;
25use Fisharebest\Webtrees\Tree;
26
27/**
28 *
29 */
30class ChartMortality extends AbstractGoogle
31{
32    /**
33     * @var IndividualRepository
34     */
35    private $individualRepository;
36
37    /**
38     * Constructor.
39     *
40     * @param Tree $tree
41     */
42    public function __construct(Tree $tree)
43    {
44        $this->individualRepository = new IndividualRepository($tree);
45    }
46
47    /**
48     * Create a chart showing mortality.
49     *
50     * @param int         $tot_l
51     * @param int         $tot_d
52     * @param string|null $size
53     * @param string|null $color_living
54     * @param string|null $color_dead
55     *
56     * @return string
57     */
58    public function chartMortality(
59        int $tot_l,
60        int $tot_d,
61        string $size = null,
62        string $color_living = null,
63        string $color_dead = null
64    ): string {
65        // Raw data - for calculation
66        $tot = $tot_l + $tot_d;
67
68        if ($tot === 0) {
69            return '';
70        }
71
72        $chart_x = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
73        $chart_y = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
74
75        $size         = $size ?? ($chart_x . 'x' . $chart_y);
76        $color_living = $color_living ?? 'ffffff';
77        $color_dead   = $color_dead ?? 'cccccc';
78
79        $sizes = explode('x', $size);
80
81        $chd = $this->arrayToExtendedEncoding([
82            intdiv(4095 * $tot_l, $tot),
83            intdiv(4095 * $tot_d, $tot),
84        ]);
85
86        $per_l = $this->individualRepository->totalLivingPercentage();
87        $per_d = $this->individualRepository->totalDeceasedPercentage();
88
89        $chl =
90            I18N::translate('Living') . ' - ' . $per_l . '|' .
91            I18N::translate('Dead') . ' - ' . $per_d . '|';
92
93        $chart_title =
94            I18N::translate('Living') . ' - ' . $per_l . I18N::$list_separator .
95            I18N::translate('Dead') . ' - ' . $per_d;
96
97        $colors = [$color_living, $color_dead];
98
99        return view(
100            'statistics/other/chart-google',
101            [
102                'chart_title' => $chart_title,
103                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
104                'sizes'       => $sizes,
105            ]
106        );
107    }
108}
109