xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 90a2f71801fb87cd8a5109b7c84d162b5549b38c)
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 */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
22*90a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
26*90a2f718SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
278add1155SRico Sonntag
28*90a2f718SGreg Roachuse function app;
29*90a2f718SGreg Roachuse function assert;
3071378461SGreg Roachuse function count;
3171378461SGreg Roach
328add1155SRico Sonntag/**
3393ccd686SRico Sonntag * A chart showing the top given names.
348add1155SRico Sonntag */
3593ccd686SRico Sonntagclass ChartCommonGiven
368add1155SRico Sonntag{
378add1155SRico Sonntag    /**
3893ccd686SRico Sonntag     * @var ModuleThemeInterface
3993ccd686SRico Sonntag     */
4093ccd686SRico Sonntag    private $theme;
4193ccd686SRico Sonntag
4293ccd686SRico Sonntag    /**
4393ccd686SRico Sonntag     * @var ColorService
4493ccd686SRico Sonntag     */
4593ccd686SRico Sonntag    private $color_service;
4693ccd686SRico Sonntag
4793ccd686SRico Sonntag    /**
4893ccd686SRico Sonntag     * Constructor.
4993ccd686SRico Sonntag     */
5093ccd686SRico Sonntag    public function __construct()
5193ccd686SRico Sonntag    {
52cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
5393ccd686SRico Sonntag        $this->color_service = new ColorService();
5493ccd686SRico Sonntag    }
5593ccd686SRico Sonntag
5693ccd686SRico Sonntag    /**
578add1155SRico Sonntag     * Create a chart of common given names.
588add1155SRico Sonntag     *
598add1155SRico Sonntag     * @param int         $tot_indi   The total number of individuals
608add1155SRico Sonntag     * @param array       $given      The list of common given names
618add1155SRico Sonntag     * @param string|null $color_from
628add1155SRico Sonntag     * @param string|null $color_to
638add1155SRico Sonntag     *
648add1155SRico Sonntag     * @return string
658add1155SRico Sonntag     */
668add1155SRico Sonntag    public function chartCommonGiven(
678add1155SRico Sonntag        int $tot_indi,
688add1155SRico Sonntag        array $given,
698add1155SRico Sonntag        string $color_from = null,
708add1155SRico Sonntag        string $color_to = null
718add1155SRico Sonntag    ): string {
7288de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
7388de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
748add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
758add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
768add1155SRico Sonntag
778add1155SRico Sonntag        $tot = 0;
788add1155SRico Sonntag        foreach ($given as $count) {
798add1155SRico Sonntag            $tot += $count;
808add1155SRico Sonntag        }
818add1155SRico Sonntag
8288de55fdSRico Sonntag        $data = [
8388de55fdSRico Sonntag            [
8488de55fdSRico Sonntag                I18N::translate('Name'),
8588de55fdSRico Sonntag                I18N::translate('Total')
8688de55fdSRico Sonntag            ],
8788de55fdSRico Sonntag        ];
888add1155SRico Sonntag
8988de55fdSRico Sonntag        foreach ($given as $name => $count) {
9088de55fdSRico Sonntag            $data[] = [ $name, $count ];
918add1155SRico Sonntag        }
928add1155SRico Sonntag
9388de55fdSRico Sonntag        $data[] = [
9488de55fdSRico Sonntag            I18N::translate('Other'),
9588de55fdSRico Sonntag            $tot_indi - $tot
9688de55fdSRico Sonntag        ];
978add1155SRico Sonntag
986ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
998add1155SRico Sonntag
100*90a2f718SGreg Roach        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
101*90a2f718SGreg Roach        assert($locale instanceof LocaleInterface);
102*90a2f718SGreg Roach
103*90a2f718SGreg Roach        return view('statistics/other/charts/pie', [
10488de55fdSRico Sonntag            'title'    => null,
10588de55fdSRico Sonntag            'data'     => $data,
10688de55fdSRico Sonntag            'colors'   => $colors,
107*90a2f718SGreg Roach            'language' => $locale->languageTag(),
108*90a2f718SGreg Roach        ]);
1098add1155SRico Sonntag    }
1108add1155SRico Sonntag}
111