xref: /webtrees/app/Statistics/Google/ChartSex.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 ChartSex 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     * Generate a chart showing sex distribution.
49     *
50     * @param int         $tot_m         The total number of male individuals
51     * @param int         $tot_f         The total number of female individuals
52     * @param int         $tot_u         The total number of unknown individuals
53     * @param string|null $size
54     * @param string|null $color_female
55     * @param string|null $color_male
56     * @param string|null $color_unknown
57     *
58     * @return string
59     */
60    public function chartSex(
61        int $tot_m,
62        int $tot_f,
63        int $tot_u,
64        string $size          = null,
65        string $color_female  = null,
66        string $color_male    = null,
67        string $color_unknown = null
68    ): string {
69        $chart_x = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
70        $chart_y = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
71
72        $size          = $size ?? ($chart_x . 'x' . $chart_y);
73        $color_female  = $color_female ?? 'ffd1dc';
74        $color_male    = $color_male ?? '84beff';
75        $color_unknown = $color_unknown ?? '777777';
76
77        $sizes = explode('x', $size);
78
79        // Raw data - for calculation
80        $tot = $tot_f + $tot_m + $tot_u;
81
82        // I18N data - for display
83        $per_f = $this->individualRepository->totalSexFemalesPercentage();
84        $per_m = $this->individualRepository->totalSexMalesPercentage();
85        $per_u = $this->individualRepository->totalSexUnknownPercentage();
86
87        if ($tot === 0) {
88            return '';
89        }
90
91        if ($tot_u > 0) {
92            $chd = $this->arrayToExtendedEncoding([
93                intdiv(4095 * $tot_u, $tot),
94                intdiv(4095 * $tot_f, $tot),
95                intdiv(4095 * $tot_m, $tot),
96            ]);
97
98            $chl =
99                I18N::translateContext('unknown people', 'Unknown') . ' - ' . $per_u . '|' .
100                I18N::translate('Females') . ' - ' . $per_f . '|' .
101                I18N::translate('Males') . ' - ' . $per_m;
102
103            $chart_title =
104                I18N::translate('Males') . ' - ' . $per_m . I18N::$list_separator .
105                I18N::translate('Females') . ' - ' . $per_f . I18N::$list_separator .
106                I18N::translateContext('unknown people', 'Unknown') . ' - ' . $per_u;
107
108            $colors = [$color_unknown, $color_female, $color_male];
109        } else {
110            $chd = $this->arrayToExtendedEncoding([
111                intdiv(4095 * $tot_f, $tot),
112                intdiv(4095 * $tot_m, $tot),
113            ]);
114
115            $chl =
116                I18N::translate('Females') . ' - ' . $per_f . '|' .
117                I18N::translate('Males') . ' - ' . $per_m;
118
119            $chart_title =
120                I18N::translate('Males') . ' - ' . $per_m . I18N::$list_separator .
121                I18N::translate('Females') . ' - ' . $per_f;
122
123            $colors = [$color_female, $color_male];
124        }
125
126        return view(
127            'statistics/other/chart-google',
128            [
129                'chart_title' => $chart_title,
130                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
131                'sizes'       => $sizes,
132            ]
133        );
134    }
135}
136