xref: /webtrees/app/Statistics/Google/ChartBirth.php (revision 71378461661e7642e52abe7d41c9cfffb3e5369b)
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 */
178add1155SRico Sonntagdeclare(strict_types=1);
188add1155SRico Sonntag
198add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
208add1155SRico Sonntag
218add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2293ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
268add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
276ccdf4f0SGreg Roachuse stdClass;
288add1155SRico Sonntag
29*71378461SGreg Roachuse function count;
30*71378461SGreg Roach
318add1155SRico Sonntag/**
3293ccd686SRico Sonntag * A chart showing birth by century.
338add1155SRico Sonntag */
3493ccd686SRico Sonntagclass ChartBirth
358add1155SRico Sonntag{
368add1155SRico Sonntag    /**
3793ccd686SRico Sonntag     * @var Tree
388add1155SRico Sonntag     */
3993ccd686SRico Sonntag    private $tree;
4093ccd686SRico Sonntag
4193ccd686SRico Sonntag    /**
4293ccd686SRico Sonntag     * @var ModuleThemeInterface
4393ccd686SRico Sonntag     */
4493ccd686SRico Sonntag    private $theme;
4593ccd686SRico Sonntag
4693ccd686SRico Sonntag    /**
4793ccd686SRico Sonntag     * @var CenturyService
4893ccd686SRico Sonntag     */
4993ccd686SRico Sonntag    private $century_service;
5093ccd686SRico Sonntag
5193ccd686SRico Sonntag    /**
5293ccd686SRico Sonntag     * @var ColorService
5393ccd686SRico Sonntag     */
5493ccd686SRico Sonntag    private $color_service;
558add1155SRico Sonntag
568add1155SRico Sonntag    /**
578add1155SRico Sonntag     * Constructor.
588add1155SRico Sonntag     *
598add1155SRico Sonntag     * @param Tree $tree
608add1155SRico Sonntag     */
618add1155SRico Sonntag    public function __construct(Tree $tree)
628add1155SRico Sonntag    {
6393ccd686SRico Sonntag        $this->tree            = $tree;
64cab242e7SGreg Roach        $this->theme           = app(ModuleThemeInterface::class);
6593ccd686SRico Sonntag        $this->century_service = new CenturyService();
6693ccd686SRico Sonntag        $this->color_service   = new ColorService();
678add1155SRico Sonntag    }
688add1155SRico Sonntag
698add1155SRico Sonntag    /**
708add1155SRico Sonntag     * Returns the related database records.
718add1155SRico Sonntag     *
726ccdf4f0SGreg Roach     * @return stdClass[]
738add1155SRico Sonntag     */
748add1155SRico Sonntag    private function queryRecords(): array
758add1155SRico Sonntag    {
768add1155SRico Sonntag        $query = DB::table('dates')
773413ec75SRico Sonntag            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
788add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
798add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
808add1155SRico Sonntag            ->where('d_year', '<>', 0)
818add1155SRico Sonntag            ->where('d_fact', '=', 'BIRT')
828add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
838add1155SRico Sonntag            ->groupBy(['century'])
848add1155SRico Sonntag            ->orderBy('century');
858add1155SRico Sonntag
868add1155SRico Sonntag        return $query->get()->all();
878add1155SRico Sonntag    }
888add1155SRico Sonntag
898add1155SRico Sonntag    /**
908add1155SRico Sonntag     * Create a chart of birth places.
918add1155SRico Sonntag     *
928add1155SRico Sonntag     * @param string|null $color_from
938add1155SRico Sonntag     * @param string|null $color_to
948add1155SRico Sonntag     *
958add1155SRico Sonntag     * @return string
968add1155SRico Sonntag     */
9788de55fdSRico Sonntag    public function chartBirth(string $color_from = null, string $color_to = null): string
988add1155SRico Sonntag    {
9988de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
10088de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
1018add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
1028add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
1038add1155SRico Sonntag
10488de55fdSRico Sonntag        $data = [
10588de55fdSRico Sonntag            [
10688de55fdSRico Sonntag                I18N::translate('Century'),
10788de55fdSRico Sonntag                I18N::translate('Total')
10888de55fdSRico Sonntag            ],
10988de55fdSRico Sonntag        ];
1108add1155SRico Sonntag
11188de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
11288de55fdSRico Sonntag            $data[] = [
11393ccd686SRico Sonntag                $this->century_service->centuryName((int) $record->century),
11488de55fdSRico Sonntag                $record->total
11588de55fdSRico Sonntag            ];
1168add1155SRico Sonntag        }
1178add1155SRico Sonntag
1186ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1198add1155SRico Sonntag
1208add1155SRico Sonntag        return view(
12188de55fdSRico Sonntag            'statistics/other/charts/pie',
1228add1155SRico Sonntag            [
12388de55fdSRico Sonntag                'title'  => I18N::translate('Births by century'),
12488de55fdSRico Sonntag                'data'   => $data,
12588de55fdSRico Sonntag                'colors' => $colors,
1268add1155SRico Sonntag            ]
1278add1155SRico Sonntag        );
1288add1155SRico Sonntag    }
1298add1155SRico Sonntag}
130