Add Chart.js in HUGO ๐Ÿ“ˆ

Add Chart.js in HUGO ๐Ÿ“ˆ

Add Charts in HUGO with chart.js ๐Ÿง‘โ€๐Ÿ’ป

Add Chart.js in HUGO ๐Ÿ“ˆ
1: This article

How to Add Chart.js as a Shortcode in Hugo ๐ŸŽจ๐Ÿ“Š

Want to make your Hugo blog or study notes pop with beautiful charts? With Chart.js and a simple Hugo shortcode, you can add interactive graphs to any pageโ€”no coding headaches! Letโ€™s walk through the process step by step, with easy examples and emojis to help you remember. ๐Ÿš€

What is Chart.js?

Chart.js is a popular JavaScript library that lets you create stunning charts (like bar, line, pie, and even Venn diagrams!) using just a little bit of code. It works perfectly with Hugo, a fast static site generator.

Step 1: Add Chart.js and Plugins to Your Site

First, include Chart.js and any plugins you want in your siteโ€™s HTML. You can do this by adding these lines to your layouts/partials/head.html or directly in your page:

layouts/partials/head.html
<!-- Chart.js core -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Data labels plugin -->
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
<!-- Annotation plugin -->
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation@1"></script>
<!-- Venn diagram plugin -->
<script src="https://cdn.jsdelivr.net/npm/chartjs-chart-venn"></script>

These scripts let you use all the cool features Chart.js offers, including datalabels, annotations, and Venn diagrams! โœจ

Step 2: Create a Hugo Shortcode for Charts

Letโ€™s make a shortcode called chart.html in your layouts/shortcodes/ folder. This shortcode will handle rendering any Chart.js chart you want!

Hereโ€™s a simple version:

{{ $id := delimit (slice "chart" (now.UnixNano)) "-" }}
<div class="chart">
  <canvas id="{{ $id }}"></canvas>
  <script type="text/javascript">
    window.addEventListener("DOMContentLoaded", function() {
      const ctx = document.getElementById("{{ $id }}");
      new Chart(ctx, {
        {{ .Inner | safeJS }}
      });
    });
  </script>
</div>

What does this do?

  • Generates a unique ID for each chart so you can have many on one page.
  • Sets up the HTML <canvas> where the chart will appear.
  • Runs JavaScript to create the chart using the config you provide inside the shortcode.

Step 3: Use the Shortcode in Your Content

Now, you can easily add charts in your Markdown files like this:

๐Ÿ“ˆ Line Chart Example


๐Ÿ“Š Bar Chart Example

Thatโ€™s it! Hugo will render these as interactive charts. ๐Ÿง‘โ€๐Ÿ’ป

Step 4: Add More Chart Types (Even Venn Diagrams!)

Want to show set intersections? Just use the Venn plugin:

๐ŸŸข Venn Diagram Example

Tips & Tricks for Students ๐Ÿ“

  • Line charts: Great for trends (like marks over months).
  • Bar charts: Perfect for comparing categories (like favorite fruits).
  • Venn diagrams: Awesome for set theory in math!
  • Annotations: Highlight important points in your data.
  • Datalabels: Show exact values on your charts for clarity.

Summary Table

Chart TypeUse Case ExampleHow to Add with Shortcode
LineTrends over timetype: 'line', ...
BarCategory comparisontype: 'bar', ...
VennSet relationshipstype: 'venn', ...

Conclusion

With just a few steps, you can turn your Hugo site into a data-visualization powerhouse! Whether youโ€™re explaining math concepts, showing survey results, or just making your blog more fun, Chart.js shortcodes make it easy. Give it a try and make your study notes come alive! ๐Ÿš€๐Ÿ“š

If you want more advanced examples or run into any trouble, just ask! Happy charting! ๐Ÿ˜Š