How to build a simple chart with Chart.js

Add beautiful charts and graphs to your web or app quickly with Chart.js. Below are detailed instructions.

Add beautiful charts and graphs to your web or app quickly with Chart.js . Below are detailed instructions.

How to build a simple chart with Chart.js Picture 1How to build a simple chart with Chart.js Picture 1

Visualizing data in a way people can understand is important when making data processing decisions. Charts and graphs are a way of visualizing data, helping us understand complex data, trends, and patterns easily.

Let's explore how to create a simple chart using Chart.js, a popular JavaScript library for data illustration.

What is Chart.js?

Chart.js is a free tool that helps programmers create interactive charts for web applications. The HTML5 and CSS3 canvas component renders charts, allowing them to work in modern browsers.

Features of Chart.js

  1. Stands out for its user-friendly approach. With minimal code, programmers can create interactive, visually appealing charts.
  2. This library is flexible and supports different chart types such as line, bar, pie, and radar charts. It can meet diverse data presentation needs.
  3. The chart design works well on desktop and mobile devices. They are responsive and adaptable.
  4. Chart.js charts can be changed using multiple options instead of the default settings. Programmers can fine-tune the chart to meet specific conditions.

Set up the environment

You can set up the library in one of two ways:

  1. Use CDN: Simply include the following script tag in the head of your HTML document.
  1. Use the Package manager. If you prefer a package manager, you can install Chart.js using npm - Node Package Manager:
npm install chart.js

Or Yarn:

yarn add chart.js

Basic HTML structure

To embed a chart, you need a canvas element in HTML. Here is the basic structure:

 Document 

Welcome to My Data Representation

To style the page, create a file, style.css and add the following CSS to it:

@import url("https://fonts.googleapis.com/css2?family=Tilt+Neon&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; } html { font-size: 62.5%; } body { font-family: "Tilt Neon", sans-serif; padding: 2rem 4rem; } h1 { font-size: 3rem; color: #333; text-align: center; padding: 3rem; }

Create your first chart: Bar chart

This chart type is ideal for comparing individual data points by category.

1. In the script tag at the bottom of the HTML, start by selecting the canvas element by its ID attribute:

let canvas = document.getElementById('myChart');

2. Next, get context for how your chart appears. Here is a 2D drawing.

let ctx = canvas.getContext('2d');

3. Next, initialize a new chart on the canvas using the Chart() function. This function takes the canvas context as its first argument, then an object of options that includes the data currently in the chart.

 

let options = {}; let myChart = new Chart(canvas, options);

4. Next, fill in the optional object that defines the chart type, data, and labels you want in the chart.

let options = { type: "bar", data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: "Total number of votes on favourite color", data: [12, 19, 3, 5, 2, 3], }], }, };

Currently, your chart will look like this:

How to build a simple chart with Chart.js Picture 2How to build a simple chart with Chart.js Picture 2

Style and customize charts

Chart.js provides countless options for customizing charts such as:

  1. Colors : Customize chart colors, from bar backgrounds to line borders, with Chart.js.
  2. Legends : Place legends at the top, bottom, left, or right to increase clarity.
  3. Tooltips : Use tooltips for detailed overview reports on data points when hovered.
  4. Animations : Set animation style and speed for dynamic display.

As a simple example, you can set some basic styles for the dataset by changing the options object as follows:

let options = { type: "bar", data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: "Total number of votes on favourite color", data: [12, 19, 3, 5, 2, 3], backgroundColor: "rgba(75, 192, 192, 0.2)", borderColor: "rgba(75, 192, 192, 1)", borderWidth: 1, }], }, }; 

Your chart will look like this:

How to build a simple chart with Chart.js Picture 3How to build a simple chart with Chart.js Picture 3

Practical applications and tips to increase efficiency

To ensure optimal performance when rendering charts:

  1. Limit the data points used in Chart.js for faster rendering and better user experience.
  2. If you update the chart regularly, use the destroy() method to remove the old chart before displaying the new chart.

Tips to avoid common pitfalls

  1. Make sure data is always formatted in a way that prevents unexpected problems.
  2. To improve performance, it is best to limit animations, although they help improve the user experience but cause too many problems.

Chart.js is a useful tool when you want to display data interactively in an engaging way. Easily create beautiful data illustrations, provide insights, and make informed decisions.

Chart.js provides a powerful solution for data visualization for both new and experienced users.

4.5 ★ | 2 Vote