xref: /webtrees/app/Statistics/Google/ChartChildren.php (revision 3d0d3c4eac0d9885ecf85cce0ac6e76860c614d4)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\I18N;
21use Fisharebest\Webtrees\Statistics\AbstractGoogle;
22use Fisharebest\Webtrees\Statistics\Helper\Century;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Database\Capsule\Manager as DB;
25use Illuminate\Database\Query\JoinClause;
26
27/**
28 *
29 */
30class ChartChildren extends AbstractGoogle
31{
32    /**
33     * @var Tree
34     */
35    private $tree;
36
37    /**
38     * @var Century
39     */
40    private $centuryHelper;
41
42    /**
43     * Constructor.
44     *
45     * @param Tree $tree
46     */
47    public function __construct(Tree $tree)
48    {
49        $this->tree          = $tree;
50        $this->centuryHelper = new Century();
51    }
52
53    /**
54     * Returns the related database records.
55     *
56     * @return \stdClass[]
57     */
58    private function queryRecords(): array
59    {
60        $query = DB::table('families')
61            ->selectRaw('ROUND(AVG(f_numchil),2) AS num')
62            ->selectRaw('ROUND((d_year - 50) / 100) AS century')
63            ->join('dates', function (JoinClause $join) {
64                $join->on('d_file', '=', 'f_file')
65                    ->on('d_gid', '=', 'f_id');
66            })
67            ->where('f_file', '=', $this->tree->id())
68            ->where('d_julianday1', '<>', 0)
69            ->where('d_fact', '=', 'MARR')
70            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
71            ->groupBy(['century'])
72            ->orderBy('century');
73
74        return $query->get()->all();
75    }
76
77    /**
78     * General query on familes/children.
79     *
80     * @param string $size
81     *
82     * @return string
83     */
84    public function chartChildren(string $size = '220x200'): string
85    {
86        $sizes = explode('x', $size);
87        $max   = 0;
88        $rows  = $this->queryRecords();
89
90        if (empty($rows)) {
91            return '';
92        }
93
94        foreach ($rows as $values) {
95            $values->num = (int) $values->num;
96            if ($max < $values->num) {
97                $max = $values->num;
98            }
99        }
100
101        $chm    = '';
102        $chxl   = '0:|';
103        $i      = 0;
104        $counts = [];
105
106        foreach ($rows as $values) {
107            $chxl .= $this->centuryHelper->centuryName((int) $values->century) . '|';
108            if ($max <= 5) {
109                $counts[] = (int) ($values->num * 819.2 - 1);
110            } elseif ($max <= 10) {
111                $counts[] = (int) ($values->num * 409.6);
112            } else {
113                $counts[] = (int) ($values->num * 204.8);
114            }
115            $chm .= 't' . $values->num . ',000000,0,' . $i . ',11,1|';
116            $i++;
117        }
118
119        $chd = $this->arrayToExtendedEncoding($counts);
120        $chm = substr($chm, 0, -1);
121
122        if ($max <= 5) {
123            $chxl .= '1:||' . I18N::translate('century') . '|2:|0|1|2|3|4|5|3:||' . I18N::translate('Number of children') . '|';
124        } elseif ($max <= 10) {
125            $chxl .= '1:||' . I18N::translate('century') . '|2:|0|1|2|3|4|5|6|7|8|9|10|3:||' . I18N::translate('Number of children') . '|';
126        } else {
127            $chxl .= '1:||' . I18N::translate('century') . '|2:|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|3:||' . I18N::translate('Number of children') . '|';
128        }
129
130        $chart_url = 'https://chart.googleapis.com/chart?cht=bvg&amp;chs=' . $sizes[0] . 'x' . $sizes[1]
131            . '&amp;chf=bg,s,ffffff00|c,s,ffffff00&amp;chm=D,FF0000,0,0,3,1|' . $chm
132            . '&amp;chd=e:' . $chd . '&amp;chco=0000FF&amp;chbh=30,3&amp;chxt=x,x,y,y&amp;chxl='
133            . rawurlencode($chxl);
134
135        return view(
136            'statistics/other/chart-google',
137            [
138                'chart_title' => I18N::translate('Average number of children per family'),
139                'chart_url'   => $chart_url,
140                'sizes'       => $sizes,
141            ]
142        );
143    }
144}
145