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