aboutsummaryrefslogtreecommitdiff
path: root/layouts/shortcodes/chart.html
blob: 69689cde46069160494f8e9a41b17f650f338755 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

{{- $id := printf "chart-%d" (now.UnixNano) -}}
{{- $type := .Get "type" | default "bar" -}}
{{- $data := .Get "data" -}}
{{- $options := .Get "options" | default "{}" -}}

<div class="chart-container mb-5">
  <canvas id="{{ $id }}"></canvas>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
  document.addEventListener('DOMContentLoaded', function() {
    const ctx = document.getElementById('{{ $id }}').getContext('2d');
    new Chart(ctx, {
      type: '{{ $type }}',
      data: {{ $data | safeJS }},
      options: Object.assign({
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
          legend: { position: 'top' }
        }
      }, {{ $options | safeJS }})
    });
  });
</script>

<style>
  .chart-container {
    position: relative;
    height: 400px;
    width: 100%;
  }
</style>