xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 6fd4f6e3d5a2c64c159e88e10883a1ccab4d4a42)
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;
23
24/**
25 *
26 */
27class ChartCommonGiven extends AbstractGoogle
28{
29    /**
30     * Create a chart of common given names.
31     *
32     * @param int         $tot_indi   The total number of individuals
33     * @param array       $given      The list of common given names
34     * @param string|null $size
35     * @param string|null $color_from
36     * @param string|null $color_to
37     *
38     * @return string
39     */
40    public function chartCommonGiven(
41        int $tot_indi,
42        array $given,
43        string $size = null,
44        string $color_from = null,
45        string $color_to = null
46    ) : string {
47        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
48        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
49        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
50        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
51
52        $size       = $size ?? ($chart_x . 'x' . $chart_y);
53        $color_from = $color_from ?? $chart_color1;
54        $color_to   = $color_to ?? $chart_color2;
55        $sizes      = explode('x', $size);
56
57        if (empty($given)) {
58            return '';
59        }
60
61        $tot = 0;
62        foreach ($given as $count) {
63            $tot += $count;
64        }
65
66        $chd = '';
67        $chl = [];
68
69        foreach ($given as $givn => $count) {
70            if ($tot === 0) {
71                $per = 0;
72            } else {
73                $per = intdiv(100 * $count, $tot_indi);
74            }
75            $chd .= $this->arrayToExtendedEncoding([$per]);
76            $chl[] = $givn . ' - ' . I18N::number($count);
77        }
78
79        $per   = intdiv(100 * ($tot_indi - $tot), $tot_indi);
80        $chd .= $this->arrayToExtendedEncoding([$per]);
81        $chl[] = I18N::translate('Other') . ' - ' . I18N::number($tot_indi - $tot);
82
83        $chart_title = implode(I18N::$list_separator, $chl);
84        $chl         = rawurlencode(implode('|', $chl));
85        $colors      = [$color_from, $color_to];
86
87        return view(
88            'statistics/other/chart-google',
89            [
90                'chart_title' => $chart_title,
91                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
92                'sizes'       => $sizes,
93            ]
94        );
95    }
96}
97