xref: /webtrees/app/Statistics/Google/ChartCommonGiven.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
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
20*6ccdf4f0SGreg Roachuse function count;
218add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2293ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
248add1155SRico Sonntag
258add1155SRico Sonntag/**
2693ccd686SRico Sonntag * A chart showing the top given names.
278add1155SRico Sonntag */
2893ccd686SRico Sonntagclass ChartCommonGiven
298add1155SRico Sonntag{
308add1155SRico Sonntag    /**
3193ccd686SRico Sonntag     * @var ModuleThemeInterface
3293ccd686SRico Sonntag     */
3393ccd686SRico Sonntag    private $theme;
3493ccd686SRico Sonntag
3593ccd686SRico Sonntag    /**
3693ccd686SRico Sonntag     * @var ColorService
3793ccd686SRico Sonntag     */
3893ccd686SRico Sonntag    private $color_service;
3993ccd686SRico Sonntag
4093ccd686SRico Sonntag    /**
4193ccd686SRico Sonntag     * Constructor.
4293ccd686SRico Sonntag     */
4393ccd686SRico Sonntag    public function __construct()
4493ccd686SRico Sonntag    {
45cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
4693ccd686SRico Sonntag        $this->color_service = new ColorService();
4793ccd686SRico Sonntag    }
4893ccd686SRico Sonntag
4993ccd686SRico Sonntag    /**
508add1155SRico Sonntag     * Create a chart of common given names.
518add1155SRico Sonntag     *
528add1155SRico Sonntag     * @param int         $tot_indi   The total number of individuals
538add1155SRico Sonntag     * @param array       $given      The list of common given names
548add1155SRico Sonntag     * @param string|null $color_from
558add1155SRico Sonntag     * @param string|null $color_to
568add1155SRico Sonntag     *
578add1155SRico Sonntag     * @return string
588add1155SRico Sonntag     */
598add1155SRico Sonntag    public function chartCommonGiven(
608add1155SRico Sonntag        int $tot_indi,
618add1155SRico Sonntag        array $given,
628add1155SRico Sonntag        string $color_from = null,
638add1155SRico Sonntag        string $color_to = null
648add1155SRico Sonntag    ) : string {
6588de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
6688de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
678add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
688add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
698add1155SRico Sonntag
708add1155SRico Sonntag        $tot = 0;
718add1155SRico Sonntag        foreach ($given as $count) {
728add1155SRico Sonntag            $tot += $count;
738add1155SRico Sonntag        }
748add1155SRico Sonntag
7588de55fdSRico Sonntag        $data = [
7688de55fdSRico Sonntag            [
7788de55fdSRico Sonntag                I18N::translate('Name'),
7888de55fdSRico Sonntag                I18N::translate('Total')
7988de55fdSRico Sonntag            ],
8088de55fdSRico Sonntag        ];
818add1155SRico Sonntag
8288de55fdSRico Sonntag        foreach ($given as $name => $count) {
8388de55fdSRico Sonntag            $data[] = [ $name, $count ];
848add1155SRico Sonntag        }
858add1155SRico Sonntag
8688de55fdSRico Sonntag        $data[] = [
8788de55fdSRico Sonntag            I18N::translate('Other'),
8888de55fdSRico Sonntag            $tot_indi - $tot
8988de55fdSRico Sonntag        ];
908add1155SRico Sonntag
91*6ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
928add1155SRico Sonntag
938add1155SRico Sonntag        return view(
9488de55fdSRico Sonntag            'statistics/other/charts/pie',
958add1155SRico Sonntag            [
9688de55fdSRico Sonntag                'title'  => null,
9788de55fdSRico Sonntag                'data'   => $data,
9888de55fdSRico Sonntag                'colors' => $colors,
998add1155SRico Sonntag            ]
1008add1155SRico Sonntag        );
1018add1155SRico Sonntag    }
1028add1155SRico Sonntag}
103