xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 242a78626998a767db1568b24919e94ae4b38ac3)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\Family;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Statistics\AbstractGoogle;
23use Illuminate\Database\Capsule\Manager as DB;
24
25/**
26 *
27 */
28class ChartFamilyLargest extends AbstractGoogle
29{
30    /**
31     * Returns the related database records.
32     *
33     * @param int $total
34     *
35     * @return \stdClass[]
36     */
37    private function queryRecords(int $total): array
38    {
39        $query = DB::table('families')
40            ->select(['f_numchil AS total', 'f_id AS id'])
41            ->where('f_file', '=', $this->tree->id())
42            ->orderBy('total', 'desc')
43            ->limit($total);
44
45        return $query->get()->all();
46    }
47
48    /**
49     * Create a chart of the largest families.
50     *
51     * @param string|null $color_from
52     * @param string|null $color_to
53     * @param int         $total
54     *
55     * @return string
56     */
57    public function chartLargestFamilies(
58        string $color_from = null,
59        string $color_to   = null,
60        int    $total      = 10
61    ): string {
62        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
63        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
64        $color_from   = $color_from ?? $chart_color1;
65        $color_to     = $color_to   ?? $chart_color2;
66
67        $data = [
68            [
69                I18N::translate('Type'),
70                I18N::translate('Total')
71            ],
72        ];
73
74        foreach ($this->queryRecords($total) as $record) {
75            $family = Family::getInstance($record->id, $this->tree);
76
77            if ($family && $family->canShow()) {
78                $data[] = [
79                    htmlspecialchars_decode(strip_tags($family->getFullName())),
80                    $record->total
81                ];
82            }
83        }
84
85        $colors = $this->interpolateRgb($color_from, $color_to, \count($data) - 1);
86
87        return view(
88            'statistics/other/charts/pie',
89            [
90                'title'       => I18N::translate('Largest families'),
91                'data'        => $data,
92                'colors'      => $colors,
93            ]
94        );
95    }
96}
97