xref: /webtrees/resources/views/statistics/other/charts/pie.phtml (revision 728c8c2714cc197d45bad7363eb52e0bff3297bb)
188de55fdSRico Sonntag<?php
2d70512abSGreg Roach
388de55fdSRico Sonntagdeclare(strict_types=1);
488de55fdSRico Sonntag
5dd7dd2a1SRico Sonntaguse Fisharebest\Webtrees\I18N;
6dd7dd2a1SRico Sonntag
7dc270d8cSGreg Roach/**
8dc270d8cSGreg Roach * @var array<string> $colors
9dc270d8cSGreg Roach * @var array<array>  $data
10dc270d8cSGreg Roach * @var string        $language
11dc270d8cSGreg Roach * @var string        $title
12dc270d8cSGreg Roach */
13dc270d8cSGreg Roach
1488de55fdSRico Sonntag$id   = 'google-chart-' . bin2hex(random_bytes(8));
1566ce3d23SRico Sonntag$name = 'callback_' . bin2hex(random_bytes(12));
1688de55fdSRico Sonntag?>
1788de55fdSRico Sonntag
18dd7dd2a1SRico Sonntag<?php if (count($data) === 1) : ?>
1939c990acSGreg Roach    <?= I18N::translate('This information is not available.') ?>
20dd7dd2a1SRico Sonntag<?php else : ?>
2188de55fdSRico Sonntag    <div id="<?= $id ?>"></div>
2288de55fdSRico Sonntag    <script>
2388de55fdSRico Sonntag
2466ce3d23SRico Sonntag    let <?= $name ?> = function () {
2566ce3d23SRico Sonntag        statistics.drawPieChart(
26*728c8c27SGreg Roach            <?= json_encode($id, JSON_THROW_ON_ERROR) ?>,
27*728c8c27SGreg Roach            <?= json_encode($data, JSON_THROW_ON_ERROR) ?>,
2866ce3d23SRico Sonntag            {
29*728c8c27SGreg Roach                title: <?= json_encode($title, JSON_THROW_ON_ERROR) ?>,
3088de55fdSRico Sonntag
3166ce3d23SRico Sonntag                // Note: "legend" needs to be defined completely as Object.assign does only a shallow merge
3266ce3d23SRico Sonntag                legend: {
3366ce3d23SRico Sonntag                    alignment: 'center',
34*728c8c27SGreg Roach                    labeledValueText: <?= json_encode($labeledValueText ?? 'value', JSON_THROW_ON_ERROR) ?>,
3566ce3d23SRico Sonntag                    position: 'labeled'
3666ce3d23SRico Sonntag                },
37*728c8c27SGreg Roach                colors: <?= json_encode($colors, JSON_THROW_ON_ERROR) ?>
3866ce3d23SRico Sonntag            }
3988de55fdSRico Sonntag        );
4088de55fdSRico Sonntag    };
4188de55fdSRico Sonntag
4266ce3d23SRico Sonntag    if (document.readyState === "complete"
4366ce3d23SRico Sonntag        || (document.readyState !== "loading" && !document.documentElement.doScroll)
4488de55fdSRico Sonntag    ) {
45*728c8c27SGreg Roach        statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
4666ce3d23SRico Sonntag        statistics.addCallback(<?= $name ?>);
4788de55fdSRico Sonntag    } else {
4866ce3d23SRico Sonntag        document.addEventListener("DOMContentLoaded", function () {
49*728c8c27SGreg Roach            statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
5066ce3d23SRico Sonntag            statistics.addCallback(<?= $name ?>);
5166ce3d23SRico Sonntag        });
5288de55fdSRico Sonntag    }
5388de55fdSRico Sonntag
5488de55fdSRico Sonntag    </script>
55dd7dd2a1SRico Sonntag<?php endif; ?>
56