xref: /webtrees/app/Statistics/Google/ChartSex.php (revision 7ef421a4290b6b468944b845497d6139b0eddd3f)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\I18N;
24use Psr\Http\Message\ServerRequestInterface;
25
26use function app;
27use function assert;
28
29/**
30 * A chart showing the distribution of males and females.
31 */
32class ChartSex
33{
34    /**
35     * Generate a chart showing sex distribution.
36     *
37     * @param int         $tot_m         The total number of male individuals
38     * @param int         $tot_f         The total number of female individuals
39     * @param int         $tot_u         The total number of unknown individuals
40     * @param string|null $color_female
41     * @param string|null $color_male
42     * @param string|null $color_unknown
43     *
44     * @return string
45     */
46    public function chartSex(
47        int $tot_m,
48        int $tot_f,
49        int $tot_u,
50        string $color_female = null,
51        string $color_male = null,
52        string $color_unknown = null
53    ): string {
54        $color_female  = $color_female  ?? '#ffd1dc';
55        $color_male    = $color_male    ?? '#84beff';
56        $color_unknown = $color_unknown ?? '#777777';
57
58        $data = [
59            [
60                I18N::translate('Type'),
61                I18N::translate('Total')
62            ],
63        ];
64
65        if ($tot_m || $tot_f || $tot_u) {
66            $data[] = [
67                I18N::translate('Males'),
68                $tot_m
69            ];
70
71            $data[] = [
72                I18N::translate('Females'),
73                $tot_f
74            ];
75
76            $data[] = [
77                I18N::translate('Unknown'),
78                $tot_u
79            ];
80        }
81
82        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
83        assert($locale instanceof LocaleInterface);
84
85        return view('statistics/other/charts/pie', [
86            'title'            => null,
87            'data'             => $data,
88            'colors'           => [$color_male, $color_female, $color_unknown],
89            'labeledValueText' => 'percentage',
90            'language'         => $locale->languageTag(),
91        ]);
92    }
93}
94