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