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