xref: /webtrees/app/Statistics/Google/ChartFamilyWithSources.php (revision 65cf57062fc712114f930335533210fd95170cea)
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 families with sources.
31 */
32class ChartFamilyWithSources
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 of individuals with/without sources.
55     *
56     * @param int         $tot_fam        The total number of families
57     * @param int         $tot_fam_source The total number of families with sources
58     * @param string|null $color_from
59     * @param string|null $color_to
60     *
61     * @return string
62     */
63    public function chartFamsWithSources(
64        int $tot_fam,
65        int $tot_fam_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_fam || $tot_fam_source) {
82            $data[] = [
83                I18N::translate('Without sources'),
84                $tot_fam - $tot_fam_source
85            ];
86
87            $data[] = [
88                I18N::translate('With sources'),
89                $tot_fam_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('Families with sources'),
97            'data'             => $data,
98            'colors'           => $colors,
99            'labeledValueText' => 'percentage',
100            'language'         => I18N::languageTag(),
101        ]);
102    }
103}
104