xref: /webtrees/app/Statistics/Google/ChartFamilyWithSources.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
158add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17*fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
258add1155SRico Sonntag
2671378461SGreg Roachuse function count;
2771378461SGreg Roach
288add1155SRico Sonntag/**
2993ccd686SRico Sonntag * A chart showing families with sources.
308add1155SRico Sonntag */
3193ccd686SRico Sonntagclass ChartFamilyWithSources
328add1155SRico Sonntag{
338add1155SRico Sonntag    /**
3493ccd686SRico Sonntag     * @var ModuleThemeInterface
3593ccd686SRico Sonntag     */
3693ccd686SRico Sonntag    private $theme;
3793ccd686SRico Sonntag
3893ccd686SRico Sonntag    /**
3993ccd686SRico Sonntag     * @var ColorService
4093ccd686SRico Sonntag     */
4193ccd686SRico Sonntag    private $color_service;
4293ccd686SRico Sonntag
4393ccd686SRico Sonntag    /**
4493ccd686SRico Sonntag     * Constructor.
4593ccd686SRico Sonntag     */
4693ccd686SRico Sonntag    public function __construct()
4793ccd686SRico Sonntag    {
48cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
4993ccd686SRico Sonntag        $this->color_service = new ColorService();
5093ccd686SRico Sonntag    }
5193ccd686SRico Sonntag
5293ccd686SRico Sonntag    /**
538add1155SRico Sonntag     * Create a chart of individuals with/without sources.
548add1155SRico Sonntag     *
558add1155SRico Sonntag     * @param int         $tot_fam        The total number of families
568add1155SRico Sonntag     * @param int         $tot_fam_source The total number of families with sources
578add1155SRico Sonntag     * @param string|null $color_from
588add1155SRico Sonntag     * @param string|null $color_to
598add1155SRico Sonntag     *
608add1155SRico Sonntag     * @return string
618add1155SRico Sonntag     */
628add1155SRico Sonntag    public function chartFamsWithSources(
638add1155SRico Sonntag        int $tot_fam,
648add1155SRico Sonntag        int $tot_fam_source,
658add1155SRico Sonntag        string $color_from = null,
668add1155SRico Sonntag        string $color_to = null
678add1155SRico Sonntag    ): string {
6888de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
6988de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
708add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
718add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
728add1155SRico Sonntag
7388de55fdSRico Sonntag        $data = [
7488de55fdSRico Sonntag            [
7588de55fdSRico Sonntag                I18N::translate('Type'),
7688de55fdSRico Sonntag                I18N::translate('Total')
7788de55fdSRico Sonntag            ],
78dd7dd2a1SRico Sonntag        ];
79dd7dd2a1SRico Sonntag
80dd7dd2a1SRico Sonntag        if ($tot_fam || $tot_fam_source) {
81dd7dd2a1SRico Sonntag            $data[] = [
8288de55fdSRico Sonntag                I18N::translate('Without sources'),
8388de55fdSRico Sonntag                $tot_fam - $tot_fam_source
84dd7dd2a1SRico Sonntag            ];
85dd7dd2a1SRico Sonntag
86dd7dd2a1SRico Sonntag            $data[] = [
8788de55fdSRico Sonntag                I18N::translate('With sources'),
8888de55fdSRico Sonntag                $tot_fam_source
8988de55fdSRico Sonntag            ];
90dd7dd2a1SRico Sonntag        }
918add1155SRico Sonntag
926ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
938add1155SRico Sonntag
948add1155SRico Sonntag        return view(
9588de55fdSRico Sonntag            'statistics/other/charts/pie',
968add1155SRico Sonntag            [
9788de55fdSRico Sonntag                'title'            => I18N::translate('Families with sources'),
9888de55fdSRico Sonntag                'data'             => $data,
9988de55fdSRico Sonntag                'colors'           => $colors,
10088de55fdSRico Sonntag                'labeledValueText' => 'percentage',
1018add1155SRico Sonntag            ]
1028add1155SRico Sonntag        );
1038add1155SRico Sonntag    }
1048add1155SRico Sonntag}
105