xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 0363a7fd32cdf352e691c0cd4ecd5255eb9004fe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Statistics\Service\ColorService;
24
25use function count;
26use function view;
27
28/**
29 * A chart showing the top given names.
30 */
31class ChartCommonGiven
32{
33    private ColorService $color_service;
34
35    /**
36     * @param ColorService $color_service
37     */
38    public function __construct(ColorService $color_service)
39    {
40        $this->color_service = $color_service;
41    }
42
43    /**
44     * Create a chart of common given names.
45     *
46     * @param int         $tot_indi   The total number of individuals
47     * @param array<int>  $given      The list of common given names
48     * @param string|null $color_from
49     * @param string|null $color_to
50     *
51     * @return string
52     */
53    public function chartCommonGiven(
54        int $tot_indi,
55        array $given,
56        string $color_from = null,
57        string $color_to = null
58    ): string {
59        $color_from = $color_from ?? 'ffffff';
60        $color_to   = $color_to ?? '84beff';
61
62        $tot = 0;
63        foreach ($given as $count) {
64            $tot += $count;
65        }
66
67        $data = [
68            [
69                I18N::translate('Name'),
70                I18N::translate('Total')
71            ],
72        ];
73
74        foreach ($given as $name => $count) {
75            $data[] = [$name, $count];
76        }
77
78        $data[] = [
79            I18N::translate('Other'),
80            $tot_indi - $tot
81        ];
82
83        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
84
85        return view('statistics/other/charts/pie', [
86            'title'    => null,
87            'data'     => $data,
88            'colors'   => $colors,
89            'language' => I18N::languageTag(),
90        ]);
91    }
92}
93