xref: /webtrees/app/Statistics/Google/ChartIndividualWithSources.php (revision da58fa21ddd29f92b1fde63c8ee425cf3d7f4eee)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Module\ModuleThemeInterface;
22use Fisharebest\Webtrees\Statistics\Service\ColorService;
23
24/**
25 * A chart showing individuals with sources.
26 */
27class ChartIndividualWithSources
28{
29    /**
30     * @var ModuleThemeInterface
31     */
32    private $theme;
33
34    /**
35     * @var ColorService
36     */
37    private $color_service;
38
39    /**
40     * Constructor.
41     */
42    public function __construct()
43    {
44        $this->theme         = app()->make(ModuleThemeInterface::class);
45        $this->color_service = new ColorService();
46    }
47
48    /**
49     * Create a chart showing individuals with/without sources.
50     *
51     * @param int         $tot_indi        The total number of individuals
52     * @param int         $tot_indi_source The total number of individuals with sources
53     * @param string|null $color_from
54     * @param string|null $color_to
55     *
56     * @return string
57     */
58    public function chartIndisWithSources(
59        int $tot_indi,
60        int $tot_indi_source,
61        string $color_from = null,
62        string $color_to   = null
63    ): string {
64        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
65        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
66        $color_from   = $color_from ?? $chart_color1;
67        $color_to     = $color_to   ?? $chart_color2;
68
69        $data = [
70            [
71                I18N::translate('Type'),
72                I18N::translate('Total')
73            ],
74        ];
75
76        if ($tot_indi || $tot_indi_source) {
77            $data[] = [
78                I18N::translate('Without sources'),
79                $tot_indi - $tot_indi_source
80            ];
81
82            $data[] = [
83                I18N::translate('With sources'),
84                $tot_indi_source
85            ];
86        }
87
88        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
89
90        return view(
91            'statistics/other/charts/pie',
92            [
93                'title'            => I18N::translate('Individuals with sources'),
94                'data'             => $data,
95                'colors'           => $colors,
96                'labeledValueText' => 'percentage',
97            ]
98        );
99    }
100}
101