xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 65cf57062fc712114f930335533210fd95170cea)
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
228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
258add1155SRico Sonntag
2690a2f718SGreg Roachuse function app;
2771378461SGreg Roachuse function count;
2871378461SGreg Roach
298add1155SRico Sonntag/**
3093ccd686SRico Sonntag * A chart showing the top given names.
318add1155SRico Sonntag */
3293ccd686SRico Sonntagclass ChartCommonGiven
338add1155SRico Sonntag{
348add1155SRico Sonntag    /**
3593ccd686SRico Sonntag     * @var ModuleThemeInterface
3693ccd686SRico Sonntag     */
3793ccd686SRico Sonntag    private $theme;
3893ccd686SRico Sonntag
3993ccd686SRico Sonntag    /**
4093ccd686SRico Sonntag     * @var ColorService
4193ccd686SRico Sonntag     */
4293ccd686SRico Sonntag    private $color_service;
4393ccd686SRico Sonntag
4493ccd686SRico Sonntag    /**
4593ccd686SRico Sonntag     * Constructor.
4693ccd686SRico Sonntag     */
4793ccd686SRico Sonntag    public function __construct()
4893ccd686SRico Sonntag    {
49cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
5093ccd686SRico Sonntag        $this->color_service = new ColorService();
5193ccd686SRico Sonntag    }
5293ccd686SRico Sonntag
5393ccd686SRico Sonntag    /**
548add1155SRico Sonntag     * Create a chart of common given names.
558add1155SRico Sonntag     *
568add1155SRico Sonntag     * @param int         $tot_indi   The total number of individuals
578add1155SRico Sonntag     * @param array       $given      The list of common given names
588add1155SRico Sonntag     * @param string|null $color_from
598add1155SRico Sonntag     * @param string|null $color_to
608add1155SRico Sonntag     *
618add1155SRico Sonntag     * @return string
628add1155SRico Sonntag     */
638add1155SRico Sonntag    public function chartCommonGiven(
648add1155SRico Sonntag        int $tot_indi,
658add1155SRico Sonntag        array $given,
668add1155SRico Sonntag        string $color_from = null,
678add1155SRico Sonntag        string $color_to = null
688add1155SRico Sonntag    ): string {
6988de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
7088de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
718add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
728add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
738add1155SRico Sonntag
748add1155SRico Sonntag        $tot = 0;
758add1155SRico Sonntag        foreach ($given as $count) {
768add1155SRico Sonntag            $tot += $count;
778add1155SRico Sonntag        }
788add1155SRico Sonntag
7988de55fdSRico Sonntag        $data = [
8088de55fdSRico Sonntag            [
8188de55fdSRico Sonntag                I18N::translate('Name'),
8288de55fdSRico Sonntag                I18N::translate('Total')
8388de55fdSRico Sonntag            ],
8488de55fdSRico Sonntag        ];
858add1155SRico Sonntag
8688de55fdSRico Sonntag        foreach ($given as $name => $count) {
8788de55fdSRico Sonntag            $data[] = [ $name, $count ];
888add1155SRico Sonntag        }
898add1155SRico Sonntag
9088de55fdSRico Sonntag        $data[] = [
9188de55fdSRico Sonntag            I18N::translate('Other'),
9288de55fdSRico Sonntag            $tot_indi - $tot
9388de55fdSRico Sonntag        ];
948add1155SRico Sonntag
956ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
968add1155SRico Sonntag
9790a2f718SGreg Roach        return view('statistics/other/charts/pie', [
9888de55fdSRico Sonntag            'title'    => null,
9988de55fdSRico Sonntag            'data'     => $data,
10088de55fdSRico Sonntag            'colors'   => $colors,
101*65cf5706SGreg Roach            'language' => I18N::languageTag(),
10290a2f718SGreg Roach        ]);
1038add1155SRico Sonntag    }
1048add1155SRico Sonntag}
105