xref: /webtrees/app/Statistics/Google/ChartFamilyWithSources.php (revision 3aa29b9734fdb6fae278f43a276b231c61e8112b)
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\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\ModuleThemeInterface;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function app;
29use function assert;
30use function count;
31
32/**
33 * A chart showing families with sources.
34 */
35class ChartFamilyWithSources
36{
37    /**
38     * @var ModuleThemeInterface
39     */
40    private $theme;
41
42    /**
43     * @var ColorService
44     */
45    private $color_service;
46
47    /**
48     * Constructor.
49     */
50    public function __construct()
51    {
52        $this->theme         = app(ModuleThemeInterface::class);
53        $this->color_service = new ColorService();
54    }
55
56    /**
57     * Create a chart of individuals with/without sources.
58     *
59     * @param int         $tot_fam        The total number of families
60     * @param int         $tot_fam_source The total number of families with sources
61     * @param string|null $color_from
62     * @param string|null $color_to
63     *
64     * @return string
65     */
66    public function chartFamsWithSources(
67        int $tot_fam,
68        int $tot_fam_source,
69        string $color_from = null,
70        string $color_to = null
71    ): string {
72        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
73        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
74        $color_from   = $color_from ?? $chart_color1;
75        $color_to     = $color_to   ?? $chart_color2;
76
77        $data = [
78            [
79                I18N::translate('Type'),
80                I18N::translate('Total')
81            ],
82        ];
83
84        if ($tot_fam || $tot_fam_source) {
85            $data[] = [
86                I18N::translate('Without sources'),
87                $tot_fam - $tot_fam_source
88            ];
89
90            $data[] = [
91                I18N::translate('With sources'),
92                $tot_fam_source
93            ];
94        }
95
96        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
97
98        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
99        assert($locale instanceof LocaleInterface);
100
101        return view('statistics/other/charts/pie', [
102            'title'            => I18N::translate('Families with sources'),
103            'data'             => $data,
104            'colors'           => $colors,
105            'labeledValueText' => 'percentage',
106            'language'         => $locale->languageTag(),
107        ]);
108    }
109}
110