xref: /webtrees/app/Statistics/Google/ChartFamilyWithSources.php (revision 93ccd68614f026f01c7c92e49f24344e66f17edb)
18add1155SRico Sonntag<?php
28add1155SRico Sonntag/**
38add1155SRico Sonntag * webtrees: online genealogy
4242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
58add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
68add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
78add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
88add1155SRico Sonntag * (at your option) any later version.
98add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
108add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
118add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128add1155SRico Sonntag * GNU General Public License for more details.
138add1155SRico Sonntag * You should have received a copy of the GNU General Public License
148add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
158add1155SRico Sonntag */
168add1155SRico Sonntagdeclare(strict_types=1);
178add1155SRico Sonntag
188add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
198add1155SRico Sonntag
208add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
21*93ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
22*93ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
238add1155SRico Sonntag
248add1155SRico Sonntag/**
25*93ccd686SRico Sonntag * A chart showing families with sources.
268add1155SRico Sonntag */
27*93ccd686SRico Sonntagclass ChartFamilyWithSources
288add1155SRico Sonntag{
298add1155SRico Sonntag    /**
30*93ccd686SRico Sonntag     * @var ModuleThemeInterface
31*93ccd686SRico Sonntag     */
32*93ccd686SRico Sonntag    private $theme;
33*93ccd686SRico Sonntag
34*93ccd686SRico Sonntag    /**
35*93ccd686SRico Sonntag     * @var ColorService
36*93ccd686SRico Sonntag     */
37*93ccd686SRico Sonntag    private $color_service;
38*93ccd686SRico Sonntag
39*93ccd686SRico Sonntag    /**
40*93ccd686SRico Sonntag     * Constructor.
41*93ccd686SRico Sonntag     */
42*93ccd686SRico Sonntag    public function __construct()
43*93ccd686SRico Sonntag    {
44*93ccd686SRico Sonntag        $this->theme         = app()->make(ModuleThemeInterface::class);
45*93ccd686SRico Sonntag        $this->color_service = new ColorService();
46*93ccd686SRico Sonntag    }
47*93ccd686SRico Sonntag
48*93ccd686SRico Sonntag    /**
498add1155SRico Sonntag     * Create a chart of individuals with/without sources.
508add1155SRico Sonntag     *
518add1155SRico Sonntag     * @param int         $tot_fam        The total number of families
528add1155SRico Sonntag     * @param int         $tot_fam_source The total number of families with sources
538add1155SRico Sonntag     * @param string|null $color_from
548add1155SRico Sonntag     * @param string|null $color_to
558add1155SRico Sonntag     *
568add1155SRico Sonntag     * @return string
578add1155SRico Sonntag     */
588add1155SRico Sonntag    public function chartFamsWithSources(
598add1155SRico Sonntag        int $tot_fam,
608add1155SRico Sonntag        int $tot_fam_source,
618add1155SRico Sonntag        string $color_from = null,
628add1155SRico Sonntag        string $color_to   = null
638add1155SRico Sonntag    ): string {
6488de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
6588de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
668add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
678add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
688add1155SRico Sonntag
6988de55fdSRico Sonntag        $data = [
7088de55fdSRico Sonntag            [
7188de55fdSRico Sonntag                I18N::translate('Type'),
7288de55fdSRico Sonntag                I18N::translate('Total')
7388de55fdSRico Sonntag            ],
74dd7dd2a1SRico Sonntag        ];
75dd7dd2a1SRico Sonntag
76dd7dd2a1SRico Sonntag        if ($tot_fam || $tot_fam_source) {
77dd7dd2a1SRico Sonntag            $data[] = [
7888de55fdSRico Sonntag                I18N::translate('Without sources'),
7988de55fdSRico Sonntag                $tot_fam - $tot_fam_source
80dd7dd2a1SRico Sonntag            ];
81dd7dd2a1SRico Sonntag
82dd7dd2a1SRico Sonntag            $data[] = [
8388de55fdSRico Sonntag                I18N::translate('With sources'),
8488de55fdSRico Sonntag                $tot_fam_source
8588de55fdSRico Sonntag            ];
86dd7dd2a1SRico Sonntag        }
878add1155SRico Sonntag
88*93ccd686SRico Sonntag        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
898add1155SRico Sonntag
908add1155SRico Sonntag        return view(
9188de55fdSRico Sonntag            'statistics/other/charts/pie',
928add1155SRico Sonntag            [
9388de55fdSRico Sonntag                'title'            => I18N::translate('Families with sources'),
9488de55fdSRico Sonntag                'data'             => $data,
9588de55fdSRico Sonntag                'colors'           => $colors,
9688de55fdSRico Sonntag                'labeledValueText' => 'percentage',
978add1155SRico Sonntag            ]
988add1155SRico Sonntag        );
998add1155SRico Sonntag    }
1008add1155SRico Sonntag}
101