xref: /webtrees/app/Statistics/Google/ChartFamilyWithSources.php (revision 54c1ab5ea4e2eb9e21dbacd6aa33a0f25550ac18)
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 families with sources.
30 */
31class ChartFamilyWithSources
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 individuals with/without sources.
54     *
55     * @param int         $tot_fam        The total number of families
56     * @param int         $tot_fam_source The total number of families with sources
57     * @param string|null $color_from
58     * @param string|null $color_to
59     *
60     * @return string
61     */
62    public function chartFamsWithSources(
63        int $tot_fam,
64        int $tot_fam_source,
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        $data = [
74            [
75                I18N::translate('Type'),
76                I18N::translate('Total')
77            ],
78        ];
79
80        if ($tot_fam || $tot_fam_source) {
81            $data[] = [
82                I18N::translate('Without sources'),
83                $tot_fam - $tot_fam_source
84            ];
85
86            $data[] = [
87                I18N::translate('With sources'),
88                $tot_fam_source
89            ];
90        }
91
92        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
93
94        return view(
95            'statistics/other/charts/pie',
96            [
97                'title'            => I18N::translate('Families with sources'),
98                'data'             => $data,
99                'colors'           => $colors,
100                'labeledValueText' => 'percentage',
101            ]
102        );
103    }
104}
105