xref: /webtrees/resources/views/statistics/other/charts/pie.phtml (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
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, JSON_THROW_ON_ERROR) ?>,
27            <?= json_encode($data, JSON_THROW_ON_ERROR) ?>,
28            {
29                title: <?= json_encode($title, JSON_THROW_ON_ERROR) ?>,
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', JSON_THROW_ON_ERROR) ?>,
35                    position: 'labeled'
36                },
37                colors: <?= json_encode($colors, JSON_THROW_ON_ERROR) ?>
38            }
39        );
40    };
41
42    if (document.readyState === "complete"
43        || (document.readyState !== "loading" && !document.documentElement.doScroll)
44    ) {
45        statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
46        statistics.addCallback(<?= $name ?>);
47    } else {
48        document.addEventListener("DOMContentLoaded", function () {
49            statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
50            statistics.addCallback(<?= $name ?>);
51        });
52    }
53
54    </script>
55<?php endif; ?>
56