xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 93ccd68614f026f01c7c92e49f24344e66f17edb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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\Service\ColorService;
23
24/**
25 * A chart showing the top given names.
26 */
27class ChartCommonGiven
28{
29    /**
30     * @var ModuleThemeInterface
31     */
32    private $theme;
33
34    /**
35     * @var ColorService
36     */
37    private $color_service;
38
39    /**
40     * Constructor.
41     */
42    public function __construct()
43    {
44        $this->theme         = app()->make(ModuleThemeInterface::class);
45        $this->color_service = new ColorService();
46    }
47
48    /**
49     * Create a chart of common given names.
50     *
51     * @param int         $tot_indi   The total number of individuals
52     * @param array       $given      The list of common given names
53     * @param string|null $color_from
54     * @param string|null $color_to
55     *
56     * @return string
57     */
58    public function chartCommonGiven(
59        int $tot_indi,
60        array $given,
61        string $color_from = null,
62        string $color_to = null
63    ) : string {
64        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
65        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
66        $color_from   = $color_from ?? $chart_color1;
67        $color_to     = $color_to   ?? $chart_color2;
68
69        $tot = 0;
70        foreach ($given as $count) {
71            $tot += $count;
72        }
73
74        $data = [
75            [
76                I18N::translate('Name'),
77                I18N::translate('Total')
78            ],
79        ];
80
81        foreach ($given as $name => $count) {
82            $data[] = [ $name, $count ];
83        }
84
85        $data[] = [
86            I18N::translate('Other'),
87            $tot_indi - $tot
88        ];
89
90        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
91
92        return view(
93            'statistics/other/charts/pie',
94            [
95                'title'  => null,
96                'data'   => $data,
97                'colors' => $colors,
98            ]
99        );
100    }
101}
102