Add chart shortcode

pull/26/head
James Panther 2021-10-28 15:32:27 +11:00
parent 04b5cb0f92
commit 68c05b97b2
No known key found for this signature in database
GPG Key ID: D36F789E45745D17
5 changed files with 66 additions and 1 deletions

View File

@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added
- Chart.js support using `chart` shortcode
- French translation ([#18](https://github.com/jpanther/congo/pull/18))
- Grouping by year can now be specificed in front matter on list pages

View File

@ -20,6 +20,7 @@ Congo is designed to be a simple, lightweight theme for [Hugo](https://gohugo.io
- Flexible with any content types, taxonomies and menus
- Ability to link to posts on third-party websites
- Diagrams and visualisations using Mermaid
- Charts using Chart.js
- SVG icons from FontAwesome 5
- Heading anchors, Buttons, Badges and more
- HTML and Emoji support in articles 🎉

View File

@ -59,6 +59,40 @@ Call to action
Call to action
{{< /button >}}
## Chart
`chart` uses the Chart.js library to embed charts into articles using simple structured data. It supports a number of [different chart styles](https://www.chartjs.org/docs/latest/samples/) and everything can be configured from within the shortcode. Simply provide the chart parameters between the shortcode tags and Chart.js will do the rest.
Refer to the [official Chart.js docs](https://www.chartjs.org/docs/latest/general/) for details on syntax and supported chart types.
**Example:**
```js
{{</* chart */>}}
type: 'bar',
data: {
labels: ['Tomato', 'Blueberry', 'Banana', 'Lime', 'Orange'],
datasets: [{
label: '# of votes',
data: [12, 19, 3, 5, 2, 3],
}]
}
{{</* /chart */>}}
```
<!-- prettier-ignore-start -->
{{< chart >}}
type: 'bar',
data: {
labels: ['Tomato', 'Blueberry', 'Banana', 'Lime', 'Orange'],
datasets: [{
label: '# of votes',
data: [12, 19, 3, 5, 3],
}]
}
{{< /chart >}}
<!-- prettier-ignore-end -->
## Icon
`icon` outputs an SVG icon and takes the icon name as its only parameter. The icon is scaled to match the current text size.
@ -93,7 +127,7 @@ When life gives you lemons, make lemonade.
## Mermaid
`mermaid` allows you to draw detailed diagrams and visualisations using text. It uses MermaidJS under the hood and supports a wide variety of diagrams, charts and other output formats.
`mermaid` allows you to draw detailed diagrams and visualisations using text. It uses Mermaid under the hood and supports a wide variety of diagrams, charts and other output formats.
Simply write your Mermaid syntax within the `mermaid` shortcode and let the plugin do the rest.

View File

@ -116,6 +116,23 @@
</script>
{{ end }}
{{ end }}
{{/* Chart */}}
{{ if .Page.HasShortcode "chart" }}
{{ $chartJS := resources.Get "vendor/chart/chart.min.js" }}
{{ if $chartJS }}
{{ $chartJS := $chartJS | resources.Fingerprint "sha512" }}
<script defer src="{{ $chartJS.RelPermalink }}" integrity="{{ $chartJS.Data.Integrity }}"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
function css(name) {
return getComputedStyle(document.documentElement).getPropertyValue(name);
}
Chart.defaults.font.size = 14
Chart.defaults.backgroundColor = css("--color-primary-300")
});
</script>
{{ end }}
{{ end }}
{{/* Analytics */}}
{{ partialCached "analytics.html" .Site }}
{{/* Extend head - eg. for custom analytics scripts, etc. */}}

View File

@ -0,0 +1,12 @@
<div class="chart">
{{ $id := delimit (shuffle (seq 1 9)) "" }}
<canvas id="{{ $id }}"></canvas>
<script>
document.addEventListener("DOMContentLoaded", (event) => {
const ctx = document.getElementById("{{ $id }}");
const chart = new Chart(ctx, {
{{ .Inner | safeJS }}
});
});
</script>
</div>