xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 2c6f1bd538f46b93645991518398bb087011cb42)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg 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\Statistics\Service\ColorService;
248add1155SRico Sonntag
2571378461SGreg Roachuse function count;
2634b20f29SGreg Roachuse function view;
2771378461SGreg Roach
288add1155SRico Sonntag/**
2993ccd686SRico Sonntag * A chart showing the top given names.
308add1155SRico Sonntag */
3193ccd686SRico Sonntagclass ChartCommonGiven
328add1155SRico Sonntag{
3334b20f29SGreg Roach    private ColorService $color_service;
3493ccd686SRico Sonntag
3593ccd686SRico Sonntag    /**
36f78da678SGreg Roach     * @param ColorService $color_service
3793ccd686SRico Sonntag     */
38f78da678SGreg Roach    public function __construct(ColorService $color_service)
3993ccd686SRico Sonntag    {
40f78da678SGreg Roach        $this->color_service = $color_service;
4193ccd686SRico Sonntag    }
4293ccd686SRico Sonntag
4393ccd686SRico Sonntag    /**
448add1155SRico Sonntag     * Create a chart of common given names.
458add1155SRico Sonntag     *
468add1155SRico Sonntag     * @param int         $tot_indi   The total number of individuals
4776d39c55SGreg Roach     * @param array<int>  $given      The list of common given names
488add1155SRico Sonntag     * @param string|null $color_from
498add1155SRico Sonntag     * @param string|null $color_to
508add1155SRico Sonntag     *
518add1155SRico Sonntag     * @return string
528add1155SRico Sonntag     */
538add1155SRico Sonntag    public function chartCommonGiven(
548add1155SRico Sonntag        int $tot_indi,
558add1155SRico Sonntag        array $given,
56*2c6f1bd5SGreg Roach        string|null $color_from = null,
578add1155SRico Sonntag        string $color_to = null
588add1155SRico Sonntag    ): string {
593529c469SGreg Roach        $color_from ??= 'ffffff';
603529c469SGreg Roach        $color_to ??= '84beff';
618add1155SRico Sonntag
628add1155SRico Sonntag        $tot = 0;
638add1155SRico Sonntag        foreach ($given as $count) {
648add1155SRico Sonntag            $tot += $count;
658add1155SRico Sonntag        }
668add1155SRico Sonntag
6788de55fdSRico Sonntag        $data = [
6888de55fdSRico Sonntag            [
6988de55fdSRico Sonntag                I18N::translate('Name'),
7088de55fdSRico Sonntag                I18N::translate('Total')
7188de55fdSRico Sonntag            ],
7288de55fdSRico Sonntag        ];
738add1155SRico Sonntag
7488de55fdSRico Sonntag        foreach ($given as $name => $count) {
7588de55fdSRico Sonntag            $data[] = [$name, $count];
768add1155SRico Sonntag        }
778add1155SRico Sonntag
7888de55fdSRico Sonntag        $data[] = [
7988de55fdSRico Sonntag            I18N::translate('Other'),
8088de55fdSRico Sonntag            $tot_indi - $tot
8188de55fdSRico Sonntag        ];
828add1155SRico Sonntag
836ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
848add1155SRico Sonntag
8590a2f718SGreg Roach        return view('statistics/other/charts/pie', [
8688de55fdSRico Sonntag            'title'    => null,
8788de55fdSRico Sonntag            'data'     => $data,
8888de55fdSRico Sonntag            'colors'   => $colors,
8965cf5706SGreg Roach            'language' => I18N::languageTag(),
9090a2f718SGreg Roach        ]);
918add1155SRico Sonntag    }
928add1155SRico Sonntag}
93