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 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Defined in session.php
20 *
21 * @global Tree $WT_TREE
22 */
23global $WT_TREE;
24
25use Fisharebest\Webtrees\Date\GregorianDate;
26
27define('WT_SCRIPT_NAME', 'statisticsplot.php');
28require './includes/session.php';
29
30/**
31 * Month of birth
32 *
33 * @param int   $z_axis
34 * @param int[] $z_boundaries
35 * @param Stats $stats
36 *
37 * @return int
38 */
39function month_of_birth($z_axis, array $z_boundaries, Stats $stats)
40{
41    $total = 0;
42
43    if ($z_axis === 300) {
44        $num = $stats->statsBirthQuery(false);
45        foreach ($num as $values) {
46            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
47                if ($month === $values['d_month']) {
48                    fill_y_data(0, $key, $values['total']);
49                    $total += $values['total'];
50                }
51            }
52        }
53    } elseif ($z_axis === 301) {
54        $num = $stats->statsBirthQuery(false, true);
55        foreach ($num as $values) {
56            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
57                if ($month === $values['d_month']) {
58                    if ($values['i_sex'] === 'M') {
59                        fill_y_data(0, $key, $values['total']);
60                        $total += $values['total'];
61                    } elseif ($values['i_sex'] === 'F') {
62                        fill_y_data(1, $key, $values['total']);
63                        $total += $values['total'];
64                    }
65                }
66            }
67        }
68    } else {
69        $zstart = 0;
70        foreach ($z_boundaries as $boundary) {
71            $num = $stats->statsBirthQuery(false, false, $zstart, $boundary);
72            foreach ($num as $values) {
73                foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
74                    if ($month === $values['d_month']) {
75                        fill_y_data($boundary, $key, $values['total']);
76                        $total += $values['total'];
77                    }
78                }
79            }
80            $zstart = $boundary + 1;
81        }
82    }
83
84    return $total;
85}
86
87/**
88 * Month of birth of first child in a relation
89 *
90 * @param int   $z_axis
91 * @param int[] $z_boundaries
92 * @param Stats $stats
93 *
94 * @return int
95 */
96function month_of_birth_of_first_child($z_axis, array $z_boundaries, Stats $stats)
97{
98    $total = 0;
99
100    if ($z_axis === 300) {
101        $num = $stats->monthFirstChildQuery(false);
102        foreach ($num as $values) {
103            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
104                if ($month === $values['d_month']) {
105                    fill_y_data(0, $key, $values['total']);
106                    $total += $values['total'];
107                }
108            }
109        }
110    } elseif ($z_axis === 301) {
111        $num = $stats->monthFirstChildQuery(false, true);
112        foreach ($num as $values) {
113            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
114                if ($month === $values['d_month']) {
115                    if ($values['i_sex'] === 'M') {
116                        fill_y_data(0, $key, $values['total']);
117                        $total += $values['total'];
118                    } elseif ($values['i_sex'] === 'F') {
119                        fill_y_data(1, $key, $values['total']);
120                        $total += $values['total'];
121                    }
122                }
123            }
124        }
125    } else {
126        $zstart = 0;
127        foreach ($z_boundaries as $boundary) {
128            $num = $stats->monthFirstChildQuery(false, false, $zstart, $boundary);
129            foreach ($num as $values) {
130                foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
131                    if ($month === $values['d_month']) {
132                        fill_y_data($boundary, $key, $values['total']);
133                        $total += $values['total'];
134                    }
135                }
136            }
137            $zstart = $boundary + 1;
138        }
139    }
140
141    return $total;
142}
143
144/**
145 * Month of death
146 *
147 * @param int   $z_axis
148 * @param int[] $z_boundaries
149 * @param Stats $stats
150 *
151 * @return int
152 */
153function month_of_death($z_axis, array $z_boundaries, Stats $stats)
154{
155    $total = 0;
156
157    if ($z_axis === 300) {
158        $num = $stats->statsDeathQuery(false);
159        foreach ($num as $values) {
160            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
161                if ($month === $values['d_month']) {
162                    fill_y_data(0, $key, $values['total']);
163                    $total += $values['total'];
164                }
165            }
166        }
167    } elseif ($z_axis === 301) {
168        $num = $stats->statsDeathQuery(false, true);
169        foreach ($num as $values) {
170            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
171                if ($month === $values['d_month']) {
172                    if ($values['i_sex'] === 'M') {
173                        fill_y_data(0, $key, $values['total']);
174                        $total += $values['total'];
175                    } elseif ($values['i_sex'] === 'F') {
176                        fill_y_data(1, $key, $values['total']);
177                        $total += $values['total'];
178                    }
179                }
180            }
181        }
182    } else {
183        $zstart = 0;
184        foreach ($z_boundaries as $boundary) {
185            $num = $stats->statsDeathQuery(false, false, $zstart, $boundary);
186            foreach ($num as $values) {
187                foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
188                    if ($month === $values['d_month']) {
189                        fill_y_data($boundary, $key, $values['total']);
190                        $total += $values['total'];
191                    }
192                }
193            }
194            $zstart = $boundary + 1;
195        }
196    }
197
198    return $total;
199}
200
201/**
202 * Month of marriage
203 *
204 * @param int   $z_axis
205 * @param int[] $z_boundaries
206 * @param Stats $stats
207 *
208 * @return int
209 */
210function month_of_marriage($z_axis, array $z_boundaries, Stats $stats)
211{
212    $total = 0;
213
214    if ($z_axis === 300) {
215        $num = $stats->statsMarrQuery(false, false);
216        foreach ($num as $values) {
217            foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
218                if ($month === $values['d_month']) {
219                    fill_y_data(0, $key, $values['total']);
220                    $total += $values['total'];
221                }
222            }
223        }
224    } else {
225        $zstart = 0;
226        foreach ($z_boundaries as $boundary) {
227            $num = $stats->statsMarrQuery(false, false, $zstart, $boundary);
228            foreach ($num as $values) {
229                foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
230                    if ($month === $values['d_month']) {
231                        fill_y_data($boundary, $key, $values['total']);
232                        $total += $values['total'];
233                    }
234                }
235            }
236            $zstart = $boundary + 1;
237        }
238    }
239
240    return $total;
241}
242
243/**
244 * Month of first marriage
245 *
246 * @param int   $z_axis
247 * @param int[] $z_boundaries
248 * @param Stats $stats
249 *
250 * @return int
251 */
252function month_of_first_marriage($z_axis, array $z_boundaries, Stats $stats)
253{
254    $total = 0;
255
256    if ($z_axis === 300) {
257        $num  = $stats->statsMarrQuery(false, true);
258        $indi = array();
259        $fam  = array();
260        foreach ($num as $values) {
261            if (!in_array($values['indi'], $indi) && !in_array($values['fams'], $fam)) {
262                foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
263                    if ($month === $values['month']) {
264                        fill_y_data(0, $key, 1);
265                        $total++;
266                    }
267                }
268                $indi[] = $values['indi'];
269                $fam[]  = $values['fams'];
270            }
271        }
272    } else {
273        $zstart = 0;
274        $indi   = array();
275        $fam    = array();
276        foreach ($z_boundaries as $boundary) {
277            $num = $stats->statsMarrQuery(false, true, $zstart, $boundary);
278            foreach ($num as $values) {
279                if (!in_array($values['indi'], $indi) && !in_array($values['fams'], $fam)) {
280                    foreach (array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC') as $key => $month) {
281                        if ($month === $values['month']) {
282                            fill_y_data($boundary, $key, 1);
283                            $total++;
284                        }
285                    }
286                    $indi[] = $values['indi'];
287                    $fam[]  = $values['fams'];
288                }
289            }
290            $zstart = $boundary + 1;
291        }
292    }
293
294    return $total;
295}
296
297/**
298 * Age related to birth year
299 *
300 * @param int   $z_axis
301 * @param int[] $z_boundaries
302 * @param Stats $stats
303 *
304 * @return int
305 */
306function lifespan_by_birth_year($z_axis, array $z_boundaries, Stats $stats)
307{
308    $total = 0;
309
310    if ($z_axis === 300) {
311        $num = $stats->statsAgeQuery(false, 'BIRT');
312        foreach ($num as $values) {
313            foreach ($values as $age_value) {
314                fill_y_data(0, (int) ($age_value / 365.25), 1);
315                $total++;
316            }
317        }
318    } elseif ($z_axis === 301) {
319        $num = $stats->statsAgeQuery(false, 'BIRT', 'M');
320        foreach ($num as $values) {
321            foreach ($values as $age_value) {
322                fill_y_data(0, (int) ($age_value / 365.25), 1);
323                $total++;
324            }
325        }
326        $num = $stats->statsAgeQuery(false, 'BIRT', 'F');
327        foreach ($num as $values) {
328            foreach ($values as $age_value) {
329                fill_y_data(1, (int) ($age_value / 365.25), 1);
330                $total++;
331            }
332        }
333    } else {
334        $zstart = 0;
335        foreach ($z_boundaries as $boundary) {
336            $num = $stats->statsAgeQuery(false, 'BIRT', 'BOTH', $zstart, $boundary);
337            foreach ($num as $values) {
338                foreach ($values as $age_value) {
339                    fill_y_data($boundary, (int) ($age_value / 365.25), 1);
340                    $total++;
341                }
342            }
343            $zstart = $boundary + 1;
344        }
345    }
346
347    return $total;
348}
349
350/**
351 * Age related to death year
352 *
353 * @param int   $z_axis
354 * @param int[] $z_boundaries
355 * @param Stats $stats
356 *
357 * @return int
358 */
359function lifespan_by_death_year($z_axis, array $z_boundaries, Stats $stats)
360{
361    $total = 0;
362
363    if ($z_axis === 300) {
364        $num = $stats->statsAgeQuery(false, 'DEAT');
365        foreach ($num as $values) {
366            foreach ($values as $age_value) {
367                fill_y_data(0, (int) ($age_value / 365.25), 1);
368                $total++;
369            }
370        }
371    } elseif ($z_axis === 301) {
372        $num = $stats->statsAgeQuery(false, 'DEAT', 'M');
373        foreach ($num as $values) {
374            foreach ($values as $age_value) {
375                fill_y_data(0, (int) ($age_value / 365.25), 1);
376                $total++;
377            }
378        }
379        $num = $stats->statsAgeQuery(false, 'DEAT', 'F');
380        foreach ($num as $values) {
381            foreach ($values as $age_value) {
382                fill_y_data(1, (int) ($age_value / 365.25), 1);
383                $total++;
384            }
385        }
386    } else {
387        $zstart = 0;
388        foreach ($z_boundaries as $boundary) {
389            $num = $stats->statsAgeQuery(false, 'DEAT', 'BOTH', $zstart, $boundary);
390            foreach ($num as $values) {
391                foreach ($values as $age_value) {
392                    fill_y_data($boundary, (int) ($age_value / 365.25), 1);
393                    $total++;
394                }
395            }
396            $zstart = $boundary + 1;
397        }
398    }
399
400    return $total;
401}
402
403/**
404 * Age in year of marriage
405 *
406 * @param int   $z_axis
407 * @param int[] $z_boundaries
408 * @param Stats $stats
409 *
410 * @return int
411 */
412function age_at_marriage($z_axis, array $z_boundaries, Stats $stats)
413{
414    $total = 0;
415
416    if ($z_axis === 300) {
417        $num = $stats->statsMarrAgeQuery(false, 'M');
418        foreach ($num as $values) {
419            fill_y_data(0, (int) ($values['age'] / 365.25), 1);
420            $total++;
421        }
422        $num = $stats->statsMarrAgeQuery(false, 'F');
423        foreach ($num as $values) {
424            fill_y_data(0, (int) ($values['age'] / 365.25), 1);
425            $total++;
426        }
427    } elseif ($z_axis === 301) {
428        $num = $stats->statsMarrAgeQuery(false, 'M');
429        foreach ($num as $values) {
430            fill_y_data(0, (int) ($values['age'] / 365.25), 1);
431            $total++;
432        }
433        $num = $stats->statsMarrAgeQuery(false, 'F');
434        foreach ($num as $values) {
435            fill_y_data(1, (int) ($values['age'] / 365.25), 1);
436            $total++;
437        }
438    } else {
439        $zstart = 0;
440        foreach ($z_boundaries as $boundary) {
441            $num = $stats->statsMarrAgeQuery(false, 'M', $zstart, $boundary);
442            foreach ($num as $values) {
443                fill_y_data($boundary, (int) ($values['age'] / 365.25), 1);
444                $total++;
445            }
446            $num = $stats->statsMarrAgeQuery(false, 'F', $zstart, $boundary);
447            foreach ($num as $values) {
448                fill_y_data($boundary, (int) ($values['age'] / 365.25), 1);
449                $total++;
450            }
451            $zstart = $boundary + 1;
452        }
453    }
454
455    return $total;
456}
457
458/**
459 * Age in year of first marriage
460 *
461 * @param int   $z_axis
462 * @param int[] $z_boundaries
463 * @param Stats $stats
464 *
465 * @return int
466 */
467function age_at_first_marriage($z_axis, array $z_boundaries, Stats $stats)
468{
469    $total = 0;
470
471    if ($z_axis === 300) {
472        $num  = $stats->statsMarrAgeQuery(false, 'M');
473        $indi = array();
474        foreach ($num as $values) {
475            if (!in_array($values['d_gid'], $indi)) {
476                fill_y_data(0, (int) ($values['age'] / 365.25), 1);
477                $total++;
478                $indi[] = $values['d_gid'];
479            }
480        }
481        $num  = $stats->statsMarrAgeQuery(false, 'F');
482        $indi = array();
483        foreach ($num as $values) {
484            if (!in_array($values['d_gid'], $indi)) {
485                fill_y_data(0, (int) ($values['age'] / 365.25), 1);
486                $total++;
487                $indi[] = $values['d_gid'];
488            }
489        }
490    } elseif ($z_axis === 301) {
491        $num  = $stats->statsMarrAgeQuery(false, 'M');
492        $indi = array();
493        foreach ($num as $values) {
494            if (!in_array($values['d_gid'], $indi)) {
495                fill_y_data(0, (int) ($values['age'] / 365.25), 1);
496                $total++;
497                $indi[] = $values['d_gid'];
498            }
499        }
500        $num  = $stats->statsMarrAgeQuery(false, 'F');
501        $indi = array();
502        foreach ($num as $values) {
503            if (!in_array($values['d_gid'], $indi)) {
504                fill_y_data(1, (int) ($values['age'] / 365.25), 1);
505                $total++;
506                $indi[] = $values['d_gid'];
507            }
508        }
509    } else {
510        $zstart = 0;
511        $indi   = array();
512        foreach ($z_boundaries as $boundary) {
513            $num = $stats->statsMarrAgeQuery(false, 'M', $zstart, $boundary);
514            foreach ($num as $values) {
515                if (!in_array($values['d_gid'], $indi)) {
516                    fill_y_data($boundary, (int) ($values['age'] / 365.25), 1);
517                    $total++;
518                    $indi[] = $values['d_gid'];
519                }
520            }
521            $num = $stats->statsMarrAgeQuery(false, 'F', $zstart, $boundary);
522            foreach ($num as $values) {
523                if (!in_array($values['d_gid'], $indi)) {
524                    fill_y_data($boundary, (int) ($values['age'] / 365.25), 1);
525                    $total++;
526                    $indi[] = $values['d_gid'];
527                }
528            }
529            $zstart = $boundary + 1;
530        }
531    }
532
533    return $total;
534}
535
536/**
537 * Number of children
538 *
539 * @param int   $z_axis
540 * @param int[] $z_boundaries
541 * @param Stats $stats
542 *
543 * @return int
544 */
545function number_of_children($z_axis, array $z_boundaries, Stats $stats)
546{
547    $total = 0;
548
549    if ($z_axis === 300) {
550        $num = $stats->statsChildrenQuery(false);
551        foreach ($num as $values) {
552            fill_y_data(0, $values['f_numchil'], $values['total']);
553            $total += $values['f_numchil'] * $values['total'];
554        }
555    } elseif ($z_axis === 301) {
556        $num = $stats->statsChildrenQuery(false, 'M');
557        foreach ($num as $values) {
558            fill_y_data(0, $values['num'], $values['total']);
559            $total += $values['num'] * $values['total'];
560        }
561        $num = $stats->statsChildrenQuery(false, 'F');
562        foreach ($num as $values) {
563            fill_y_data(1, $values['num'], $values['total']);
564            $total += $values['num'] * $values['total'];
565        }
566    } else {
567        $zstart = 0;
568        foreach ($z_boundaries as $boundary) {
569            $num = $stats->statsChildrenQuery(false, 'BOTH', $zstart, $boundary);
570            foreach ($num as $values) {
571                fill_y_data($boundary, $values['f_numchil'], $values['total']);
572                $total += $values['f_numchil'] * $values['total'];
573            }
574            $zstart = $boundary + 1;
575        }
576    }
577
578    return $total;
579}
580
581/**
582 * Calculate the Y axis.
583 *
584 * @param int $z
585 * @param int $x
586 * @param int $val
587 */
588function fill_y_data($z, $x, $val)
589{
590    global $ydata, $xmax, $x_boundaries, $zmax, $z_boundaries, $xgiven, $zgiven;
591    //-- calculate index $i out of given z value
592    //-- calculate index $j out of given x value
593    if ($xgiven) {
594        $j = $x;
595    } else {
596        $j = 0;
597        while (($x > $x_boundaries[$j]) && ($j < $xmax)) {
598            $j++;
599        }
600    }
601    if ($zgiven) {
602        $i = $z;
603    } else {
604        $i = 0;
605        while (($z > $z_boundaries[$i]) && ($i < $zmax)) {
606            $i++;
607        }
608    }
609    if (isset($ydata[$i][$j])) {
610        $ydata[$i][$j] += $val;
611    } else {
612        $ydata[$i][$j] = $val;
613    }
614}
615
616/**
617 * Plot the data.
618 *
619 * @param string      $mytitle
620 * @param integer[][] $xdata
621 * @param string      $xtitle
622 * @param integer[][] $ydata
623 * @param string      $ytitle
624 * @param string[]    $legend
625 */
626function my_plot($mytitle, $xdata, $xtitle, $ydata, $ytitle, $legend)
627{
628    global $percentage, $male_female, $ymax, $scalefactor, $datastring, $imgurl;
629
630    // Google Chart API only allows text encoding for numbers less than 100
631    // and it does not allow adjusting the y-axis range, so we must find the maximum y-value
632    // in order to adjust beforehand by changing the numbers
633
634    if ($male_female) {
635        $stop = 2;
636    } else {
637        $stop = count($ydata);
638    }
639    if ($percentage) {
640        $ypercentmax = 0;
641        $yt          = array();
642        for ($i = 0; $i < $stop; $i++) {
643            if (isset($ydata[$i])) {
644                $ymax   = max($ydata[$i]);
645                $yt[$i] = array_sum($ydata[$i]);
646                if ($yt[$i] > 0) {
647                    $ypercent    = round($ymax / $yt[$i] * 100, 1);
648                    $ypercentmax = max($ypercentmax, $ypercent);
649                }
650            }
651        }
652        $ymax = $ypercentmax;
653        if ($ymax > 0) {
654            $scalefactor = 100.0 / $ymax;
655        } else {
656            $scalefactor = 0;
657        }
658        $datastring = 'chd=t:';
659        for ($i = 0; $i < $stop; $i++) {
660            if (isset($ydata[$i])) {
661                foreach ($ydata[$i] as $j => $data) {
662                    if ($j > 0) {
663                        $datastring .= ',';
664                    }
665                    if ($yt[$i] > 0) {
666                        $datastring .= round($data / $yt[$i] * 100 * $scalefactor, 1);
667                    } else {
668                        $datastring .= '0';
669                    }
670                }
671                if ($i !== $stop - 1) {
672                    $datastring .= '|';
673                }
674            }
675        }
676    } else {
677        for ($i = 0; $i < $stop; $i++) {
678            $ymax = max($ymax, max($ydata[$i]));
679        }
680        if ($ymax > 0) {
681            $scalefactor = 100.0 / $ymax;
682        } else {
683            $scalefactor = 0;
684        }
685        $datastring = 'chd=t:';
686        for ($i = 0; $i < $stop; $i++) {
687            foreach ($ydata[$i] as $j => $data) {
688                if ($j > 0) {
689                    $datastring .= ',';
690                }
691                $datastring .= round($data * $scalefactor, 1);
692            }
693            if ($i !== $stop - 1) {
694                $datastring .= '|';
695            }
696        }
697    }
698    $colors      = array('0000FF', 'FFA0CB', '9F00FF', 'FF7000', '905030', 'FF0000', '00FF00', 'F0F000');
699    $colorstring = 'chco=';
700    for ($i = 0; $i < $stop; $i++) {
701        if (isset($colors[$i])) {
702            $colorstring .= $colors[$i];
703            if ($i !== ($stop - 1)) {
704                $colorstring .= ',';
705            }
706        }
707    }
708
709    $titleLength = strpos($mytitle . "\n", "\n");
710    $title       = substr($mytitle, 0, $titleLength);
711
712    $imgurl = 'https://chart.googleapis.com/chart?cht=bvg&amp;chs=950x300&amp;chf=bg,s,ffffff00|c,s,ffffff00&amp;chtt=' . rawurlencode($title) . '&amp;' . $datastring . '&amp;' . $colorstring . '&amp;chbh=';
713    if (count($ydata) > 3) {
714        $imgurl .= '5,1';
715    } elseif (count($ydata) < 2) {
716        $imgurl .= '45,1';
717    } else {
718        $imgurl .= '20,3';
719    }
720    $imgurl .= '&amp;chxt=x,x,y,y&amp;chxl=0:|';
721    foreach ($xdata as $data) {
722        $imgurl .= rawurlencode($data) . '|';
723    }
724
725    $imgurl .= '1:||||' . rawurlencode($xtitle) . '|2:|';
726    $imgurl .= '0|';
727    if ($percentage) {
728        for ($i = 1; $i < 11; $i++) {
729            if ($ymax < 11) {
730                $imgurl .= round($ymax * $i / 10, 1) . '|';
731            } else {
732                $imgurl .= round($ymax * $i / 10, 0) . '|';
733            }
734        }
735        $imgurl .= '3:||%|';
736    } else {
737        if ($ymax < 11) {
738            for ($i = 1; $i < $ymax + 1; $i++) {
739                $imgurl .= round($ymax * $i / ($ymax), 0) . '|';
740            }
741        } else {
742            for ($i = 1; $i < 11; $i++) {
743                $imgurl .= round($ymax * $i / 10, 0) . '|';
744            }
745        }
746        $imgurl .= '3:||' . rawurlencode($ytitle) . '|';
747    }
748    // Only show legend if y-data is non-2-dimensional
749    if (count($ydata) > 1) {
750        $imgurl .= '&amp;chdl=';
751        foreach ($legend as $i => $data) {
752            if ($i > 0) {
753                $imgurl .= '|';
754            }
755            $imgurl .= rawurlencode($data);
756        }
757    }
758    $title = strstr($mytitle, '|', true);
759    echo '<img src="', $imgurl, '" width="950" height="300" alt="', Filter::escapeHtml($title), '" title="', Filter::escapeHtml($title), '">';
760}
761
762/**
763 * Create the X azxs.
764 *
765 * @param string $x_axis_boundaries
766 */
767function calculate_axis($x_axis_boundaries)
768{
769    global $x_axis, $xdata, $xmax, $x_boundaries;
770
771    // Calculate xdata and zdata elements out of chart values
772    $hulpar = explode(',', $x_axis_boundaries);
773    $i      = 1;
774    if ($x_axis === 21 && $hulpar[0] == 1) {
775        $xdata[0] = 0;
776    } else {
777        $xdata[0] = format_range_of_numbers(0, $hulpar[0]);
778    }
779    $x_boundaries[0] = $hulpar[0] - 1;
780    while (isset($hulpar[$i])) {
781        $i1 = $i - 1;
782        if (($hulpar[$i] - $hulpar[$i1]) === 1) {
783            $xdata[$i]        = $hulpar[$i1];
784            $x_boundaries[$i] = $hulpar[$i1];
785        } elseif ($hulpar[$i1] === $hulpar[0]) {
786            $xdata[$i]        = format_range_of_numbers($hulpar[$i1], $hulpar[$i]);
787            $x_boundaries[$i] = $hulpar[$i];
788        } else {
789            $xdata[$i]        = format_range_of_numbers($hulpar[$i1] + 1, $hulpar[$i]);
790            $x_boundaries[$i] = $hulpar[$i];
791        }
792        $i++;
793    }
794    $xdata[$i]        = $hulpar[$i - 1];
795    $x_boundaries[$i] = $hulpar[$i - 1];
796    if ($hulpar[$i - 1] === $i) {
797        $xmax = $i + 1;
798    } else {
799        $xmax = $i;
800    }
801    $xdata[$xmax]        = /* I18N: Label on a graph; 40+ means 40 or more */ I18N::translate('%s+', I18N::number($hulpar[$i - 1]));
802    $x_boundaries[$xmax] = 10000;
803    $xmax                = $xmax + 1;
804    if ($xmax > 20) {
805        $xmax = 20;
806    }
807}
808
809/**
810 * A range of integers.
811 *
812 * @param int $x
813 * @param int $y
814 *
815 * @return string
816 */
817function format_range_of_numbers($x, $y)
818{
819    return /* I18N: A range of numbers */ I18N::translate(
820        '%1$s–%2$s',
821        I18N::number($x),
822        I18N::number($y)
823    );
824}
825
826/**
827 * Calculate the Z axis.
828 *
829 * @param string $boundaries_z_axis
830 */
831function calculate_legend($boundaries_z_axis)
832{
833    global $legend, $zmax, $z_boundaries;
834
835    // calculate the legend values
836    $hulpar          = explode(',', $boundaries_z_axis);
837    $i               = 1;
838    $date            = new Date('BEF ' . $hulpar[0]);
839    $legend[0]       = strip_tags($date->display());
840    $z_boundaries[0] = $hulpar[0] - 1;
841    while (isset($hulpar[$i])) {
842        $i1               = $i - 1;
843        $date             = new Date('BET ' . $hulpar[$i1] . ' AND ' . ($hulpar[$i] - 1));
844        $legend[$i]       = strip_tags($date->display());
845        $z_boundaries[$i] = $hulpar[$i] - 1;
846        $i++;
847    }
848    $zmax                = $i;
849    $zmax1               = $zmax - 1;
850    $date                = new Date('AFT ' . $hulpar[$zmax1]);
851    $legend[$zmax]       = strip_tags($date->display());
852    $z_boundaries[$zmax] = 10000;
853    $zmax                = $zmax + 1;
854    if ($zmax > 8) {
855        $zmax = 8;
856    }
857}
858
859global $legend, $xdata, $ydata, $xmax, $zmax, $z_boundaries, $xgiven, $zgiven, $percentage, $male_female;
860
861$x_axis       = Filter::getInteger('x-as', 1, 21, 11);
862$y_axis       = Filter::getInteger('y-as', 201, 202, 201);
863$z_axis       = Filter::getInteger('z-as', 300, 302, 302);
864$stats        = new Stats($WT_TREE);
865$z_boundaries = array();
866
867echo '<div class="statistics_chart" title="', I18N::translate('Statistics chart'), '">';
868
869switch ($x_axis) {
870    case '1':
871        echo $stats->chartDistribution(array(Filter::get('chart_shows'), Filter::get('chart_type'), Filter::get('SURN')));
872        break;
873    case '2':
874        echo $stats->chartDistribution(array(Filter::get('chart_shows'), 'birth_distribution_chart'));
875        break;
876    case '3':
877        echo $stats->chartDistribution(array(Filter::get('chart_shows'), 'death_distribution_chart'));
878        break;
879    case '4':
880        echo $stats->chartDistribution(array(Filter::get('chart_shows'), 'marriage_distribution_chart'));
881        break;
882    case '11':
883        $monthdata = array();
884        for ($i = 0; $i < 12; ++$i) {
885            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
886        }
887        $xgiven            = true;
888        $zgiven            = false;
889        $title             = I18N::translate('Month of birth');
890        $xtitle            = I18N::translate('month');
891        $ytitle            = I18N::translate('numbers');
892        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
893        $xdata             = $monthdata;
894        $xmax              = 12;
895        if ($z_axis !== 300 && $z_axis !== 301) {
896            calculate_legend($boundaries_z_axis);
897        }
898        $percentage = false;
899        if ($y_axis === 201) {
900            $percentage = false;
901            $ytitle     = I18N::translate('Individuals');
902        } elseif ($y_axis === 202) {
903            $percentage = true;
904            $ytitle     = I18N::translate('percentage');
905        }
906        $male_female = false;
907        if ($z_axis === 300) {
908            $zgiven          = false;
909            $legend[0]       = 'all';
910            $zmax            = 1;
911            $z_boundaries[0] = 100000;
912        } elseif ($z_axis === 301) {
913            $male_female = true;
914            $zgiven      = true;
915            $legend[0]   = I18N::translate('Male');
916            $legend[1]   = I18N::translate('Female');
917            $zmax        = 2;
918            $xtitle      = $xtitle . I18N::translate(' per gender');
919        } elseif ($z_axis === 302) {
920            $xtitle = $xtitle . I18N::translate(' per time period');
921        }
922        //-- reset the data array
923        for ($i = 0; $i < $zmax; $i++) {
924            for ($j = 0; $j < $xmax; $j++) {
925                $ydata[$i][$j] = 0;
926            }
927        }
928        $total = month_of_birth($z_axis, $z_boundaries, $stats);
929        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
930        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
931        break;
932    case '12':
933        $monthdata = array();
934        for ($i = 0; $i < 12; ++$i) {
935            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
936        }
937        $xgiven            = true;
938        $zgiven            = false;
939        $title             = I18N::translate('Month of death');
940        $xtitle            = I18N::translate('month');
941        $ytitle            = I18N::translate('numbers');
942        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
943        $xdata             = $monthdata;
944        $xmax              = 12;
945        if ($z_axis !== 300 && $z_axis !== 301) {
946            calculate_legend($boundaries_z_axis);
947        }
948        $percentage = false;
949        if ($y_axis === 201) {
950            $percentage = false;
951            $ytitle     = I18N::translate('Individuals');
952        } elseif ($y_axis === 202) {
953            $percentage = true;
954            $ytitle     = I18N::translate('percentage');
955        }
956        $male_female = false;
957        if ($z_axis === 300) {
958            $zgiven          = false;
959            $legend[0]       = 'all';
960            $zmax            = 1;
961            $z_boundaries[0] = 100000;
962        } elseif ($z_axis === 301) {
963            $male_female = true;
964            $zgiven      = true;
965            $legend[0]   = I18N::translate('Male');
966            $legend[1]   = I18N::translate('Female');
967            $zmax        = 2;
968            $xtitle      = $xtitle . I18N::translate(' per gender');
969        } elseif ($z_axis === 302) {
970            $xtitle = $xtitle . I18N::translate(' per time period');
971        }
972        //-- reset the data array
973        for ($i = 0; $i < $zmax; $i++) {
974            for ($j = 0; $j < $xmax; $j++) {
975                $ydata[$i][$j] = 0;
976            }
977        }
978        $total = month_of_death($z_axis, $z_boundaries, $stats);
979        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
980        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
981        break;
982    case '13':
983        $monthdata = array();
984        for ($i = 0; $i < 12; ++$i) {
985            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
986        }
987
988        if ($z_axis === 301) {
989            $z_axis = 300;
990        }
991        $xgiven            = true;
992        $zgiven            = false;
993        $title             = I18N::translate('Month of marriage');
994        $xtitle            = I18N::translate('month');
995        $ytitle            = I18N::translate('numbers');
996        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
997        $xdata             = $monthdata;
998        $xmax              = 12;
999        if ($z_axis !== 300 && $z_axis !== 301) {
1000            calculate_legend($boundaries_z_axis);
1001        }
1002        $percentage = false;
1003        if ($y_axis === 201) {
1004            $percentage = false;
1005            $ytitle     = I18N::translate('Families');
1006        } elseif ($y_axis === 202) {
1007            $percentage = true;
1008            $ytitle     = I18N::translate('percentage');
1009        }
1010        $male_female = false;
1011        if ($z_axis === 300) {
1012            $zgiven          = false;
1013            $legend[0]       = 'all';
1014            $zmax            = 1;
1015            $z_boundaries[0] = 100000;
1016        } elseif ($z_axis === 301) {
1017            $male_female = true;
1018            $zgiven      = true;
1019            $legend[0]   = I18N::translate('Male');
1020            $legend[1]   = I18N::translate('Female');
1021            $zmax        = 2;
1022            $xtitle      = $xtitle . I18N::translate(' per gender');
1023        } elseif ($z_axis === 302) {
1024            $xtitle = $xtitle . I18N::translate(' per time period');
1025        }
1026        //-- reset the data array
1027        for ($i = 0; $i < $zmax; $i++) {
1028            for ($j = 0; $j < $xmax; $j++) {
1029                $ydata[$i][$j] = 0;
1030            }
1031        }
1032        $total = month_of_marriage($z_axis, $z_boundaries, $stats);
1033        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalFamilies();
1034        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1035        break;
1036    case '14':
1037        $monthdata = array();
1038        for ($i = 0; $i < 12; ++$i) {
1039            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1040        }
1041        $xgiven            = true;
1042        $zgiven            = false;
1043        $title             = I18N::translate('Month of birth of first child in a relation');
1044        $xtitle            = I18N::translate('month');
1045        $ytitle            = I18N::translate('numbers');
1046        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1047        $xdata             = $monthdata;
1048        $xmax              = 12;
1049        if ($z_axis !== 300 && $z_axis !== 301) {
1050            calculate_legend($boundaries_z_axis);
1051        }
1052        $percentage = false;
1053        if ($y_axis === 201) {
1054            $percentage = false;
1055            $ytitle     = I18N::translate('Children');
1056        } elseif ($y_axis === 202) {
1057            $percentage = true;
1058            $ytitle     = I18N::translate('percentage');
1059        }
1060        $male_female = false;
1061        if ($z_axis === 300) {
1062            $zgiven          = false;
1063            $legend[0]       = 'all';
1064            $zmax            = 1;
1065            $z_boundaries[0] = 100000;
1066        } elseif ($z_axis === 301) {
1067            $male_female = true;
1068            $zgiven      = true;
1069            $legend[0]   = I18N::translate('Male');
1070            $legend[1]   = I18N::translate('Female');
1071            $zmax        = 2;
1072            $xtitle      = $xtitle . I18N::translate(' per gender');
1073        } elseif ($z_axis === 302) {
1074            $xtitle = $xtitle . I18N::translate(' per time period');
1075        }
1076        //-- reset the data array
1077        for ($i = 0; $i < $zmax; $i++) {
1078            for ($j = 0; $j < $xmax; $j++) {
1079                $ydata[$i][$j] = 0;
1080            }
1081        }
1082        $total = month_of_birth_of_first_child($z_axis, $z_boundaries, $stats);
1083        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalFamilies();
1084        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1085        break;
1086    case '15':
1087        $monthdata = array();
1088        for ($i = 0; $i < 12; ++$i) {
1089            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1090        }
1091
1092        if ($z_axis === 301) {
1093            $z_axis = 300;
1094        }
1095        $xgiven            = true;
1096        $zgiven            = false;
1097        $title             = I18N::translate('Month of first marriage');
1098        $xtitle            = I18N::translate('month');
1099        $ytitle            = I18N::translate('numbers');
1100        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1101        $xdata             = $monthdata;
1102        $xmax              = 12;
1103        if ($z_axis !== 300 && $z_axis !== 301) {
1104            calculate_legend($boundaries_z_axis);
1105        }
1106        $percentage = false;
1107        if ($y_axis === 201) {
1108            $percentage = false;
1109            $ytitle     = I18N::translate('Families');
1110        } elseif ($y_axis === 202) {
1111            $percentage = true;
1112            $ytitle     = I18N::translate('percentage');
1113        }
1114        $male_female = false;
1115        if ($z_axis === 300) {
1116            $zgiven          = false;
1117            $legend[0]       = 'all';
1118            $zmax            = 1;
1119            $z_boundaries[0] = 100000;
1120        } elseif ($z_axis === 301) {
1121            $male_female = true;
1122            $zgiven      = true;
1123            $legend[0]   = I18N::translate('Male');
1124            $legend[1]   = I18N::translate('Female');
1125            $zmax        = 2;
1126            $xtitle      = $xtitle . I18N::translate(' per gender');
1127        } elseif ($z_axis === 302) {
1128            $xtitle = $xtitle . I18N::translate(' per time period');
1129        }
1130        //-- reset the data array
1131        for ($i = 0; $i < $zmax; $i++) {
1132            for ($j = 0; $j < $xmax; $j++) {
1133                $ydata[$i][$j] = 0;
1134            }
1135        }
1136        $total = month_of_first_marriage($z_axis, $z_boundaries, $stats);
1137        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalFamilies();
1138        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1139        break;
1140    case '17':
1141        $monthdata = array();
1142        for ($i = 0; $i < 12; ++$i) {
1143            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1144        }
1145        $xgiven            = false;
1146        $zgiven            = false;
1147        $title             = I18N::translate('Age related to birth year');
1148        $xtitle            = I18N::translate('age');
1149        $ytitle            = I18N::translate('numbers');
1150        $boundaries_x_axis = Filter::get('x-axis-boundaries-ages');
1151        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1152        calculate_axis($boundaries_x_axis);
1153        if ($z_axis !== 300 && $z_axis !== 301) {
1154            calculate_legend($boundaries_z_axis);
1155        }
1156        $percentage = false;
1157        if ($y_axis === 201) {
1158            $percentage = false;
1159            $ytitle     = I18N::translate('Individuals');
1160        } elseif ($y_axis === 202) {
1161            $percentage = true;
1162            $ytitle     = I18N::translate('percentage');
1163        }
1164        $male_female = false;
1165        if ($z_axis === 300) {
1166            $zgiven          = false;
1167            $legend[0]       = 'all';
1168            $zmax            = 1;
1169            $z_boundaries[0] = 100000;
1170        } elseif ($z_axis === 301) {
1171            $male_female = true;
1172            $zgiven      = true;
1173            $legend[0]   = I18N::translate('Male');
1174            $legend[1]   = I18N::translate('Female');
1175            $zmax        = 2;
1176            $xtitle      = $xtitle . I18N::translate(' per gender');
1177        } elseif ($z_axis === 302) {
1178            $xtitle = $xtitle . I18N::translate(' per time period');
1179        }
1180        //-- reset the data array
1181        for ($i = 0; $i < $zmax; $i++) {
1182            for ($j = 0; $j < $xmax; $j++) {
1183                $ydata[$i][$j] = 0;
1184            }
1185        }
1186        $total = lifespan_by_birth_year($z_axis, $z_boundaries, $stats);
1187        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
1188        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1189        break;
1190    case '18':
1191        $monthdata = array();
1192        for ($i = 0; $i < 12; ++$i) {
1193            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1194        }
1195        $xgiven            = false;
1196        $zgiven            = false;
1197        $title             = I18N::translate('Age related to death year');
1198        $xtitle            = I18N::translate('age');
1199        $ytitle            = I18N::translate('numbers');
1200        $boundaries_x_axis = Filter::get('x-axis-boundaries-ages');
1201        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1202        calculate_axis($boundaries_x_axis);
1203        if ($z_axis !== 300 && $z_axis !== 301) {
1204            calculate_legend($boundaries_z_axis);
1205        }
1206        $percentage = false;
1207        if ($y_axis === 201) {
1208            $percentage = false;
1209            $ytitle     = I18N::translate('Individuals');
1210        } elseif ($y_axis === 202) {
1211            $percentage = true;
1212            $ytitle     = I18N::translate('percentage');
1213        }
1214        $male_female = false;
1215        if ($z_axis === 300) {
1216            $zgiven          = false;
1217            $legend[0]       = 'all';
1218            $zmax            = 1;
1219            $z_boundaries[0] = 100000;
1220        } elseif ($z_axis === 301) {
1221            $male_female = true;
1222            $zgiven      = true;
1223            $legend[0]   = I18N::translate('Male');
1224            $legend[1]   = I18N::translate('Female');
1225            $zmax        = 2;
1226            $xtitle      = $xtitle . I18N::translate(' per gender');
1227        } elseif ($z_axis === 302) {
1228            $xtitle = $xtitle . I18N::translate(' per time period');
1229        }
1230        //-- reset the data array
1231        for ($i = 0; $i < $zmax; $i++) {
1232            for ($j = 0; $j < $xmax; $j++) {
1233                $ydata[$i][$j] = 0;
1234            }
1235        }
1236        $total = lifespan_by_death_year($z_axis, $z_boundaries, $stats);
1237        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
1238        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1239        break;
1240    case '19':
1241        $monthdata = array();
1242        for ($i = 0; $i < 12; ++$i) {
1243            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1244        }
1245        $xgiven            = false;
1246        $zgiven            = false;
1247        $title             = I18N::translate('Age in year of marriage');
1248        $xtitle            = I18N::translate('age');
1249        $ytitle            = I18N::translate('numbers');
1250        $boundaries_x_axis = Filter::get('x-axis-boundaries-ages_m');
1251        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1252        calculate_axis($boundaries_x_axis);
1253        if ($z_axis !== 300 && $z_axis !== 301) {
1254            calculate_legend($boundaries_z_axis);
1255        }
1256        $percentage = false;
1257        if ($y_axis === 201) {
1258            $percentage = false;
1259            $ytitle     = I18N::translate('Individuals');
1260        } elseif ($y_axis === 202) {
1261            $percentage = true;
1262            $ytitle     = I18N::translate('percentage');
1263        }
1264        $male_female     = false;
1265        $z_boundaries[0] = 100000;
1266        if ($z_axis === 300) {
1267            $zgiven          = false;
1268            $legend[0]       = 'all';
1269            $zmax            = 1;
1270        } elseif ($z_axis === 301) {
1271            $male_female = true;
1272            $zgiven      = true;
1273            $legend[0]   = I18N::translate('Male');
1274            $legend[1]   = I18N::translate('Female');
1275            $zmax        = 2;
1276            $xtitle      = $xtitle . I18N::translate(' per gender');
1277        } elseif ($z_axis === 302) {
1278            $xtitle = $xtitle . I18N::translate(' per time period');
1279        }
1280        //-- reset the data array
1281        for ($i = 0; $i < $zmax; $i++) {
1282            for ($j = 0; $j < $xmax; $j++) {
1283                $ydata[$i][$j] = 0;
1284            }
1285        }
1286        $total = age_at_marriage($z_axis, $z_boundaries, $stats);
1287        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
1288        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1289        break;
1290    case '20':
1291        $monthdata = array();
1292        for ($i = 0; $i < 12; ++$i) {
1293            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1294        }
1295        $xgiven            = false;
1296        $zgiven            = false;
1297        $title             = I18N::translate('Age in year of first marriage');
1298        $xtitle            = I18N::translate('age');
1299        $ytitle            = I18N::translate('numbers');
1300        $boundaries_x_axis = Filter::get('x-axis-boundaries-ages_m');
1301        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1302        calculate_axis($boundaries_x_axis);
1303        if ($z_axis !== 300 && $z_axis !== 301) {
1304            calculate_legend($boundaries_z_axis);
1305        }
1306        $percentage = false;
1307        if ($y_axis === 201) {
1308            $percentage = false;
1309            $ytitle     = I18N::translate('Individuals');
1310        } elseif ($y_axis === 202) {
1311            $percentage = true;
1312            $ytitle     = I18N::translate('percentage');
1313        }
1314        $male_female = false;
1315        if ($z_axis === 300) {
1316            $zgiven          = false;
1317            $legend[0]       = 'all';
1318            $zmax            = 1;
1319            $z_boundaries[0] = 100000;
1320        } elseif ($z_axis === 301) {
1321            $male_female = true;
1322            $zgiven      = true;
1323            $legend[0]   = I18N::translate('Male');
1324            $legend[1]   = I18N::translate('Female');
1325            $zmax        = 2;
1326            $xtitle      = $xtitle . I18N::translate(' per gender');
1327        } elseif ($z_axis === 302) {
1328            $xtitle = $xtitle . I18N::translate(' per time period');
1329        }
1330        //-- reset the data array
1331        for ($i = 0; $i < $zmax; $i++) {
1332            for ($j = 0; $j < $xmax; $j++) {
1333                $ydata[$i][$j] = 0;
1334            }
1335        }
1336        $total = age_at_first_marriage($z_axis, $z_boundaries, $stats);
1337        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalIndividuals();
1338        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1339        break;
1340    case '21':
1341        $monthdata = array();
1342        for ($i = 0; $i < 12; ++$i) {
1343            $monthdata[$i] = GregorianDate::monthNameNominativeCase($i + 1, false);
1344        }
1345        $xgiven            = false;
1346        $zgiven            = false;
1347        $title             = I18N::translate('Number of children');
1348        $xtitle            = I18N::translate('children');
1349        $ytitle            = I18N::translate('numbers');
1350        $boundaries_x_axis = Filter::get('x-axis-boundaries-numbers');
1351        $boundaries_z_axis = Filter::get('z-axis-boundaries-periods', null, '0');
1352        calculate_axis($boundaries_x_axis);
1353        if ($z_axis !== 300 && $z_axis !== 301) {
1354            calculate_legend($boundaries_z_axis);
1355        }
1356        $percentage = false;
1357        if ($y_axis === 201) {
1358            $percentage = false;
1359            $ytitle     = I18N::translate('Families');
1360        } elseif ($y_axis === 202) {
1361            $percentage = true;
1362            $ytitle     = I18N::translate('percentage');
1363        }
1364        $male_female = false;
1365        if ($z_axis === 300) {
1366            $zgiven          = false;
1367            $legend[0]       = 'all';
1368            $zmax            = 1;
1369            $z_boundaries[0] = 100000;
1370        } elseif ($z_axis === 301) {
1371            $male_female = true;
1372            $zgiven      = true;
1373            $legend[0]   = I18N::translate('Male');
1374            $legend[1]   = I18N::translate('Female');
1375            $zmax        = 2;
1376            $xtitle      = $xtitle . I18N::translate(' per gender');
1377        } elseif ($z_axis === 302) {
1378            $xtitle = $xtitle . I18N::translate(' per time period');
1379        }
1380        //-- reset the data array
1381        for ($i = 0; $i < $zmax; $i++) {
1382            for ($j = 0; $j < $xmax; $j++) {
1383                $ydata[$i][$j] = 0;
1384            }
1385        }
1386        $total = number_of_children($z_axis, $z_boundaries, $stats);
1387        $hstr  = $title . '|' . I18N::translate('Counts ') . ' ' . I18N::number($total) . ' ' . I18N::translate('of') . ' ' . $stats->totalChildren();
1388        my_plot($hstr, $xdata, $xtitle, $ydata, $ytitle, $legend);
1389        break;
1390    default:
1391        echo '<i class="icon-loading-large"></i>';
1392        break;
1393}
1394echo '</div>';
1395