Create a Custom Chart

Use JavaScript post messages to communicate with the HTML component.

Code Level

Advanced

Published

 

Example Description

This example demonstrates how to use the HTML component to embed a chart on a page. This is just one of the many things you can embed in your site using the HTML component.

Example Code

How We Built It

Here we embed a Chart.js chart. The HTML code for the chart is entered into the HTML Component using its Edit Code button. The page and the HTML component communicate with each other using the postMessage and onMessage functions.

When the page loads and each time a year is selected using the dropdown element, the page sends a message to the HTML component using the postMessage function. The message contains the values to display in the chart. The HTML component receives the message using its onMessage function and updates the chart accordingly.

When a user clicks a bar on the chart, the HTML component sends a message to the page using its postMessage function. The message contains data about the item that was clicked. The page receives the message using the onMessage function and displays its contents to users using the text element below the chart.

The chart is based on chartjs.org. You can find their repository and license on GitHub.

Next Steps

  1. Open this example in the Editor to work with the template.

  2. Publish the site.

  3. Learn how to work with examples in Corvid.

APIs We Used

Related Articles

Related Examples

Add to Cart Gallery

Let customers to add products to their cart

INTERMEDIATE

Hide and Show Elements

Hide and show elements in response to user interactions

BEGGINER

Hide and Show Elements

Hide and show elements in response to user interactions

BEGGINER