xref: /webtrees/app/Statistics/Google/ChartIndividualWithSources.php (revision 90a2f71801fb87cd8a5109b7c84d162b5549b38c)
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 individuals with sources.
34 */
35class ChartIndividualWithSources
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 showing individuals with/without sources.
58     *
59     * @param int         $tot_indi        The total number of individuals
60     * @param int         $tot_indi_source The total number of individuals with sources
61     * @param string|null $color_from
62     * @param string|null $color_to
63     *
64     * @return string
65     */
66    public function chartIndisWithSources(
67        int $tot_indi,
68        int $tot_indi_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_indi || $tot_indi_source) {
85            $data[] = [
86                I18N::translate('Without sources'),
87                $tot_indi - $tot_indi_source
88            ];
89
90            $data[] = [
91                I18N::translate('With sources'),
92                $tot_indi_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('Individuals with sources'),
103            'data'             => $data,
104            'colors'           => $colors,
105            'labeledValueText' => 'percentage',
106            'language'         => $locale->languageTag(),
107        ]);
108    }
109}
110