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