How to integrate Chart.js into Webflow and display the chart on a subpage?

TL;DR
  • Add Chart.js via CDN link to Webflow's Custom Code in Project Settings.
  • Create a subpage, add a Custom Code Element with chart container HTML and JavaScript to initialize Chart.js.
  • Configure data and options within JavaScript, publish, and verify the display on your site.

Integrating Chart.js in Webflow can enhance your website by visualizing data through interactive charts. Here is how you can achieve that:

1. Prepare Your Chart.js Script

  • Go to the Chart.js Website and download the latest version of Chart.js or use a CDN link.
  • Use the CDN link by adding it to your Webflow project’s header or footer under Custom Code in the Project Settings.

2. Set Up Your Subpage

  • Create or navigate to the subpage in Webflow where you want the chart to appear.
  • Add a Custom Code Element from the add panel and place it where you want the chart to be displayed on the page.

3. Insert Chart HTML and JavaScript

  • In the Custom Code Element, insert the following HTML code for the chart container: <canvas id="myChart"></canvas>.
  • Below the HTML, add a <script> tag to include JavaScript code that initializes your Chart.js chart:
  • Inside the <script>, use var ctx = document.getElementById('myChart').getContext('2d');.
  • Define a new Chart object with new Chart(ctx, { ... });, using your desired chart type and data.

4. Define Chart Data and Configurations

  • Inside the JavaScript code, configure your chart data and options:
  • Use type to specify the chart type (e.g., 'bar', 'line').
  • Within data, define datasets and labels for your chart.
  • Include options for customizing the chart appearance and behavior.

5. Publish and Verify

  • Publish your Webflow site to allow custom code execution.
  • Open the subpage to verify that the chart displays as expected.

Summary

To integrate Chart.js into a Webflow subpage, add the Chart.js CDN to your project, insert HTML and JavaScript code for the chart, and publish your site to see the chart in action. This process allows you to visualize data effectively on your Webflow website.

Rate this answer

Other Webflow Questions