xref: /webtrees/app/Statistics/Google/ChartBirth.php (revision b0fcccb03b84c12d54b4ae85014c3af52863d08c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Module\ModuleThemeInterface;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Statistics\Service\ColorService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Database\Capsule\Manager as DB;
28use stdClass;
29
30use function app;
31use function count;
32
33/**
34 * A chart showing birth by century.
35 */
36class ChartBirth
37{
38    /**
39     * @var Tree
40     */
41    private $tree;
42
43    /**
44     * @var ModuleThemeInterface
45     */
46    private $theme;
47
48    /**
49     * @var CenturyService
50     */
51    private $century_service;
52
53    /**
54     * @var ColorService
55     */
56    private $color_service;
57
58    /**
59     * Constructor.
60     *
61     * @param Tree $tree
62     */
63    public function __construct(Tree $tree)
64    {
65        $this->tree            = $tree;
66        $this->theme           = app(ModuleThemeInterface::class);
67        $this->century_service = new CenturyService();
68        $this->color_service   = new ColorService();
69    }
70
71    /**
72     * Returns the related database records.
73     *
74     * @return stdClass[]
75     */
76    private function queryRecords(): array
77    {
78        $query = DB::table('dates')
79            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
80            ->selectRaw('COUNT(*) AS total')
81            ->where('d_file', '=', $this->tree->id())
82            ->where('d_year', '<>', 0)
83            ->where('d_fact', '=', 'BIRT')
84            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
85            ->groupBy(['century'])
86            ->orderBy('century');
87
88        return $query->get()->all();
89    }
90
91    /**
92     * Create a chart of birth places.
93     *
94     * @param string|null $color_from
95     * @param string|null $color_to
96     *
97     * @return string
98     */
99    public function chartBirth(string $color_from = null, string $color_to = null): string
100    {
101        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
102        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
103        $color_from   = $color_from ?? $chart_color1;
104        $color_to     = $color_to   ?? $chart_color2;
105
106        $data = [
107            [
108                I18N::translate('Century'),
109                I18N::translate('Total')
110            ],
111        ];
112
113        foreach ($this->queryRecords() as $record) {
114            $data[] = [
115                $this->century_service->centuryName((int) $record->century),
116                $record->total
117            ];
118        }
119
120        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
121
122        return view('statistics/other/charts/pie', [
123            'title'    => I18N::translate('Births by century'),
124            'data'     => $data,
125            'colors'   => $colors,
126            'language' => I18N::languageTag(),
127        ]);
128    }
129}
130