xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 3a3c35160db2b0ae62787c386f75885682bfd3bd)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
23455a30feSGreg Roachuse Fisharebest\Webtrees\Registry;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
258add1155SRico Sonntag
2671378461SGreg Roachuse function count;
2734b20f29SGreg Roachuse function view;
2871378461SGreg Roach
298add1155SRico Sonntag/**
3093ccd686SRico Sonntag * A chart showing the top used media types.
318add1155SRico Sonntag */
3293ccd686SRico Sonntagclass ChartMedia
338add1155SRico Sonntag{
3434b20f29SGreg Roach    private ColorService $color_service;
3593ccd686SRico Sonntag
3693ccd686SRico Sonntag    /**
37f78da678SGreg Roach     * @param ColorService $color_service
3893ccd686SRico Sonntag     */
39f78da678SGreg Roach    public function __construct(ColorService $color_service)
4093ccd686SRico Sonntag    {
41f78da678SGreg Roach        $this->color_service = $color_service;
4293ccd686SRico Sonntag    }
4393ccd686SRico Sonntag
4493ccd686SRico Sonntag    /**
458add1155SRico Sonntag     * Create a chart of media types.
468add1155SRico Sonntag     *
47bfe98399SGreg Roach     * @param array<string,int> $media      The list of media types to display
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 chartMedia(
548add1155SRico Sonntag        array $media,
558add1155SRico Sonntag        string $color_from = null,
568add1155SRico Sonntag        string $color_to = null
578add1155SRico Sonntag    ): string {
5834b20f29SGreg Roach        $color_from = $color_from ?? 'ffffff';
5934b20f29SGreg Roach        $color_to   = $color_to ?? '84beff';
608add1155SRico Sonntag
6188de55fdSRico Sonntag        $data = [
6288de55fdSRico Sonntag            [
6388de55fdSRico Sonntag                I18N::translate('Type'),
6488de55fdSRico Sonntag                I18N::translate('Total')
6588de55fdSRico Sonntag            ],
6688de55fdSRico Sonntag        ];
678add1155SRico Sonntag
68*3a3c3516SGreg Roach        $element = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE');
69*3a3c3516SGreg Roach        $values  = $element->values();
70455a30feSGreg Roach
718add1155SRico Sonntag        foreach ($media as $type => $count) {
7288de55fdSRico Sonntag            $data[] = [
73*3a3c3516SGreg Roach                $values[$element->canonical($type)] ?? $type,
7488de55fdSRico Sonntag                $count
7588de55fdSRico Sonntag            ];
768add1155SRico Sonntag        }
778add1155SRico Sonntag
786ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
798add1155SRico Sonntag
8090a2f718SGreg Roach        return view('statistics/other/charts/pie', [
8188de55fdSRico Sonntag            'title'    => null,
8288de55fdSRico Sonntag            'data'     => $data,
8388de55fdSRico Sonntag            'colors'   => $colors,
8465cf5706SGreg Roach            'language' => I18N::languageTag(),
8590a2f718SGreg Roach        ]);
868add1155SRico Sonntag    }
878add1155SRico Sonntag}
88