xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 40217a61a2f92b29f16fd9c08e85ae48a5ff6a06)
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;
22
23/**
24 *
25 */
26class ChartCommonGiven extends AbstractGoogle
27{
28    /**
29     * Create a chart of common given names.
30     *
31     * @param int         $tot_indi   The total number of individuals
32     * @param array       $given      The list of common given names
33     * @param string|null $color_from
34     * @param string|null $color_to
35     *
36     * @return string
37     */
38    public function chartCommonGiven(
39        int $tot_indi,
40        array $given,
41        string $color_from = null,
42        string $color_to = null
43    ) : string {
44        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
45        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
46        $color_from   = $color_from ?? $chart_color1;
47        $color_to     = $color_to   ?? $chart_color2;
48
49        $tot = 0;
50        foreach ($given as $count) {
51            $tot += $count;
52        }
53
54        $data = [
55            [
56                I18N::translate('Name'),
57                I18N::translate('Total')
58            ],
59        ];
60
61        foreach ($given as $name => $count) {
62            $data[] = [ $name, $count ];
63        }
64
65        $data[] = [
66            I18N::translate('Other'),
67            $tot_indi - $tot
68        ];
69
70        $colors = $this->interpolateRgb($color_from, $color_to, \count($data) - 1);
71
72        return view(
73            'statistics/other/charts/pie',
74            [
75                'title'  => null,
76                'data'   => $data,
77                'colors' => $colors,
78            ]
79        );
80    }
81}
82