Add Chart.js and chart shortcode

pull/26/head
James Panther 2021-11-03 16:35:56 +11:00
commit d476b9cf64
No known key found for this signature in database
GPG Key ID: D36F789E45745D17
10 changed files with 193 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
- Dark mode toggle with new theme parameters for managing light/dark appearance
- 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 🎉

File diff suppressed because one or more lines are too long

View File

@ -59,6 +59,42 @@ 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 -->
You can see some additional Chart.js examples on the [charts samples]({{< ref "charts" >}}) page.
## 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 +129,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.
@ -114,3 +150,5 @@ graph LR;
A[Lemons]-->B[Lemonade];
B-->C[Profit]
{{< /mermaid >}}
You can see some additional Mermaid examples on the [diagrams and flowcharts samples]({{< ref "diagrams-flowcharts" >}}) page.

View File

@ -0,0 +1,84 @@
---
title: "Charts"
date: 2019-03-06
description: "Guide to Chart.js usage in Congo"
summary: "Congo includes Chart.js for powerful charts and data visualisations."
tags: ["chart", "sample", "graph"]
---
Congo includes support for Chart.js using the `chart` shortcode. Simply wrap the chart markup within the shortcode. Congo automatically themes charts to match the configured `colorScheme` parameter, however the colours can be customised using normal Chart.js syntax.
Refer to the [chart shortcode]({{< ref "docs/shortcodes#chart" >}}) docs for more details.
The examples below are a small selection taken from the [official Chart.js docs](https://www.chartjs.org/docs/latest/samples). You can also [view the page source](https://raw.githubusercontent.com/jpanther/congo/dev/exampleSite/content/samples/charts.md) on GitHub to see the markup.
## Bar chart
<!-- prettier-ignore-start -->
{{< chart >}}
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 205, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(201, 203, 207, 0.2)'
],
borderColor: [
'rgb(255, 99, 132)',
'rgb(255, 159, 64)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(54, 162, 235)',
'rgb(153, 102, 255)',
'rgb(201, 203, 207)'
],
borderWidth: 1
}]
}
{{< /chart >}}
<!-- prettier-ignore-end -->
## Line chart
<!-- prettier-ignore-start -->
{{< chart >}}
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
tension: 0.2
}]
}
{{< /chart >}}
<!-- prettier-ignore-end -->
## Doughnut chart
<!-- prettier-ignore-start -->
{{< chart >}}
type: 'doughnut',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: 'My First Dataset',
data: [300, 50, 100],
backgroundColor: [
'rgba(255, 99, 132, 0.7)',
'rgba(54, 162, 235, 0.7)',
'rgba(255, 205, 86, 0.7)'
],
hoverOffset: 4
}]
}
{{< /chart >}}
<!-- prettier-ignore-end -->

View File

@ -8,6 +8,8 @@ tags: ["mermaid", "sample", "diagram"]
Mermaid diagrams are supported in Congo using the `mermaid` shortcode. Simply wrap the diagram markup within the shortcode. Congo automatically themes Mermaid diagrams to match the configured `colorScheme` parameter.
Refer to the [mermaid shortcode]({{< ref "docs/shortcodes#mermaid" >}}) docs for more details.
The examples below are a small selection taken from the [official Mermaid docs](https://mermaid-js.github.io/mermaid/). You can also [view the page source](https://raw.githubusercontent.com/jpanther/congo/dev/exampleSite/content/samples/diagrams-flowcharts.md) on GitHub to see the markup.
## Flowchart

View File

@ -141,6 +141,29 @@
</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")
Chart.defaults.elements.point.borderColor = css("--color-primary-400")
Chart.defaults.elements.bar.borderColor = css("--color-primary-500")
Chart.defaults.elements.bar.borderWidth = 1
Chart.defaults.elements.line.borderColor = css("--color-primary-400")
Chart.defaults.elements.radar.backgroundColor = css("--color-primary-200")
Chart.defaults.elements.radar.borderColor = css("--color-primary-400")
});
</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>

13
package-lock.json generated
View File

@ -11,6 +11,7 @@
"license": "MIT",
"devDependencies": {
"@tailwindcss/typography": "^0.4.1",
"chart.js": "^3.6.0",
"mermaid": "^8.13.3",
"prettier": "^2.3.2",
"prettier-plugin-go-template": "^0.0.11",
@ -395,6 +396,12 @@
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/chart.js": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.6.0.tgz",
"integrity": "sha512-iOzzDKePL+bj+ccIsVAgWQehCXv8xOKGbaU2fO/myivH736zcx535PGJzQGanvcSGVOqX6yuLZsN3ygcQ35UgQ==",
"dev": true
},
"node_modules/chokidar": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.2.tgz",
@ -2804,6 +2811,12 @@
"supports-color": "^7.1.0"
}
},
"chart.js": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.6.0.tgz",
"integrity": "sha512-iOzzDKePL+bj+ccIsVAgWQehCXv8xOKGbaU2fO/myivH736zcx535PGJzQGanvcSGVOqX6yuLZsN3ygcQ35UgQ==",
"dev": true
},
"chokidar": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.2.tgz",

View File

@ -29,6 +29,7 @@
"homepage": "https://github.com/jpanther/congo#readme",
"devDependencies": {
"@tailwindcss/typography": "^0.4.1",
"chart.js": "^3.6.0",
"mermaid": "^8.13.3",
"prettier": "^2.3.2",
"prettier-plugin-go-template": "^0.0.11",
@ -41,6 +42,10 @@
{
"from": "node_modules/mermaid/dist/mermaid.min.js",
"to": "assets/vendor/mermaid/mermaid.min.js"
},
{
"from": "node_modules/chart.js/dist/chart.min.js",
"to": "assets/vendor/chart/chart.min.js"
}
]
}