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.

How 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 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 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

May be interested

  • How to use pictures as Excel chart columnsHow to use pictures as Excel chart columns
    excel offers a variety of chart types. however, you don't have to use columns; you can use images instead to make your charts more appealing.
  • Step by step guide to creating and interpreting numerology charts by nameStep by step guide to creating and interpreting numerology charts by name
    numerology reveals exactly what's in your name. from destiny to soul path, here's how to calculate your numerology chart based on your name.
  • How to draw a bar chart in ExcelHow to draw a bar chart in Excel
    how to draw a bar chart in excel. a bar chart is a chart that is used quite a lot, this is a chart that displays many different types of data with a rectangular column, the longer the bar, the greater the value. there are two types of bar charts: vertical and vertical charts
  • How to Create a Gantt ChartHow to Create a Gantt Chart
    a gantt chart is a type of bar chart for project management. this organizational resource is often in the form of a software tool, although the idea of a paper-based gantt chart is also not unfamiliar to many project managers. using a...
  • How to build a simple PHP websiteHow to build a simple PHP website
    want to learn how to build a basic website? previously, you could start with html, but for now, the best solution is php.
  • How to draw a line chart in ExcelHow to draw a line chart in Excel
    how to draw a line chart in excel. a line chart shows continuous data over time on an evenly divided axis, so a line chart is suitable for representing data trends at equal time periods like months, quarters or years.
  • 15 simple sentences that help leaders build an effective team15 simple sentences that help leaders build an effective team
    being a leader is often a difficult and complicated job, but just by using these 15 simple sayings will help you simplify things and align the team spirit.
  • Hide and show chart labels in ExcelHide and show chart labels in Excel
    the following article will guide you in detail how to show / hide chart labels in excel. for example, the following chart is created: 1. want to add revenue details for employees displayed on the chart do the following.
  • How to fix chart position in ExcelHow to fix chart position in Excel
    when you position the chart in excel, users can easily change the size of the column in the worksheet without affecting the position and size of the chart.
  • JavaScript code to create bubble chart with custom markerJavaScript code to create bubble chart with custom marker
    the example below shows a bubble chart with a customized highlight style and javascript source code that you can edit in your browser or save to your device to run locally.