xref: /webtrees/resources/views/statistics/other/charts/geo.phtml (revision b63ebfff10c4f748c10374a14a84550d3b653912)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\I18N;
6
7/**
8 * @var string                                            $chart_color2
9 * @var string                                            $chart_color3
10 * @var string                                            $chart_title
11 * @var array<int,array<int|string|array<string,string>>> $data
12 * @var string                                            $language
13 * @var string                                            $region
14 */
15
16$id   = 'google-chart-' . bin2hex(random_bytes(8));
17$name = 'callback_' . bin2hex(random_bytes(12));
18?>
19
20<?php if (count($data) === 1) : ?>
21    <?= I18N::translate('This information is not available.') ?>
22<?php else : ?>
23    <div class="d-grid mb-3">
24        <div class="card">
25            <div class="card-header">
26                <?= $chart_title ?>
27            </div>
28            <div class="card-body">
29                <div id="<?= $id ?>" title="<?= $chart_title ?>"></div>
30            </div>
31        </div>
32    </div>
33    <script>
34
35    let <?= $name ?> = function () {
36        statistics.drawGeoChart(
37            <?= json_encode($id, JSON_THROW_ON_ERROR) ?>,
38            <?= json_encode($data, JSON_THROW_ON_ERROR) ?>,
39            {
40                title: <?= json_encode($chart_title, JSON_THROW_ON_ERROR) ?>,
41                region: <?= json_encode($region, JSON_THROW_ON_ERROR) ?>,
42                colorAxis: {
43                    colors: [
44                        <?= json_encode('#' . $chart_color3, JSON_THROW_ON_ERROR) ?>,
45                        <?= json_encode('#' . $chart_color2, JSON_THROW_ON_ERROR) ?>
46                    ]
47                }
48            }
49        );
50    };
51
52    if (document.readyState === "complete"
53        || (document.readyState !== "loading" && !document.documentElement.doScroll)
54    ) {
55        statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
56        statistics.addCallback(<?= $name ?>);
57    } else {
58        document.addEventListener("DOMContentLoaded", function () {
59            statistics.init(<?= json_encode($language, JSON_THROW_ON_ERROR) ?>);
60            statistics.addCallback(<?= $name ?>);
61        });
62    }
63
64    </script>
65<?php endif; ?>
66