xref: /webtrees/resources/views/statistics/other/charts/pie.phtml (revision 2ebcf907ed34213f816592af04e6c160335d6311)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\I18N;
6
7/**
8 * @var array<string> $colors
9 * @var array<array>  $data
10 * @var string        $language
11 * @var string        $title
12 */
13
14$id   = 'google-chart-' . bin2hex(random_bytes(8));
15$name = 'callback_' . bin2hex(random_bytes(12));
16?>
17
18<?php if (count($data) === 1) : ?>
19    <?= I18N::translate('This information is not available.') ?>
20<?php else : ?>
21    <div id="<?= $id ?>"></div>
22    <script>
23
24    let <?= $name ?> = function () {
25        statistics.drawPieChart(
26            <?= json_encode($id) ?>,
27            <?= json_encode($data) ?>,
28            {
29                title: <?= json_encode($title) ?>,
30
31                // Note: "legend" needs to be defined completely as Object.assign does only a shallow merge
32                legend: {
33                    alignment: 'center',
34                    labeledValueText: <?= json_encode($labeledValueText ?? 'value') ?>,
35                    position: 'labeled'
36                },
37                colors: <?= json_encode($colors) ?>
38            }
39        );
40    };
41
42    if (document.readyState === "complete"
43        || (document.readyState !== "loading" && !document.documentElement.doScroll)
44    ) {
45        statistics.init(<?= json_encode($language) ?>);
46        statistics.addCallback(<?= $name ?>);
47    } else {
48        document.addEventListener("DOMContentLoaded", function () {
49            statistics.init(<?= json_encode($language) ?>);
50            statistics.addCallback(<?= $name ?>);
51        });
52    }
53
54    </script>
55<?php endif; ?>
56