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