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