chart.js progress bar example

Chart.js Progress Bar Example

By Thomas Hernandez
AD

Chart.js is a powerful open-source library that brings interactive and animated charts to your web projects. One of its most useful features is the ability to create progress bars, which can be used to represent data visually, track user progress, or simply add a stylish element to your webpage. In this guide, we'll explore how to create a Chart.js progress bar, and dive into some common use cases and customization options.

Whether you're displaying user progress, showing server response times, or creating an engaging loading spinner, Chart.js progress bars offer a flexible and visually appealing solution. By harnessing the full potential of Chart.js, you can create truly dynamic and interactive user experiences.

Setting Up the Chart.js Progress Bar

Before we dive into the specifics of creating a progress bar, make sure you have included the Chart.js library in your project. You can either download the library from the official website or use a CDN. Here's how to add it using a CDN:

AD
progress and progress icons in green colors
progress and progress icons in green colors
Progress Bar
Progress Bar
How to Create Progress Bars in Google Sheets (Step-by-Step)
How to Create Progress Bars in Google Sheets (Step-by-Step)

<script src="https://cdn.jsdelivr.net/npm/chart.js@^3/dist/chart.min.js"></script>

Creating a Simple Progress Bar

Now that Chart.js is set up, let's create a simple progress bar using the Chart.js API. To do this, we'll use the doughnut chart type and configure it to display a single piece of data. Here's the code to create a basic 50% progress bar:

the progress screen for project progress
the progress screen for project progress
a sign that says i'm making progress with a running man on the top
a sign that says i'm making progress with a running man on the top
the types of progress bar in wordpress
the types of progress bar in wordpress

<canvas id="progressBar"></canvas>
<script>
const ctx = document.getElementById('progressBar').getContext('2d');
new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [ {
data: [50],
backgroundColor: ['#65DAF5'],
borderWidth: 0
} ],
labels: ['Progress']
},
options: {
cutoutPercentage: 85,
tooltips: {
enabled: false
}
}
});
</script>

This code will generate a 50% progress bar using a doughnut chart with a cutout percentage of 85. You can adjust the `data` and `options` properties to customize the appearance and behavior of the progress bar.

AD

Animating the Progress Bar

Chart.js comes with built-in animation options that can make your progress bars more engaging. To add animations to our basic progress bar, we can modify the code as follows:

the types of progress bar for an appliance in progress, as well as 5 steps
the types of progress bar for an appliance in progress, as well as 5 steps
progress chart with numbers and arrows
progress chart with numbers and arrows
Barre De Progression Moderne Entreprise Graphique Et Graphique éléments Infographiques Illustration PNG , Graphique, Diagramme, Graphique PNG et vecteur pour téléchargement gratuit
Barre De Progression Moderne Entreprise Graphique Et Graphique éléments Infographiques Illustration PNG , Graphique, Diagramme, Graphique PNG et vecteur pour téléchargement gratuit
a set of different colored buttons for web pages or infos on a white background
a set of different colored buttons for web pages or infos on a white background
Progression Bar Examples
Progression Bar Examples
the screen is showing that there are two different things in the image and one has an arrow
the screen is showing that there are two different things in the image and one has an arrow

<canvas id="progressBar"></canvas>
<script>
const ctx = document.getElementById('progressBar').getContext('2d');
new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [ {
data: [0],
backgroundColor: ['#65DAF5'],
borderWidth: 0
} ],
labels: ['Progress']
},
options: {
animation: {
duration: 1000,
easing: 'easeInOutQuart'
},
cutoutPercentage: 85,
tooltips: {
enabled: false
}
}
});
setTimeout(() => {
chart.data.datasets[0].data = [50];
chart.update();
}, 500);
</script>

In this example, we've added `animation` options to the chart configuration. The progress bar will start at 0 and animate to 50 using the specified duration and easing function. We also use `setTimeout` to update the progress bar after a short delay, demonstrating how you can dynamically update the progress bar's value over time.

AD

Customizing the Progress Bar Appearance

Chart.js offers a wide range of customization options, allowing you to create visually appealing progress bars that match the style of your project. In this section, we'll explore some of the customization options available for Chart.js progress bars.

an image of the sky with stars and lines on it, as well as text that reads
an image of the sky with stars and lines on it, as well as text that reads
Should I display progress bar for completed jobs?
Should I display progress bar for completed jobs?

Changing Colors

One of the easiest ways to customize the appearance of your progress bar is by changing its color. Chart.js allows you to specify the background color of each data set using the `backgroundColor` property. In the following example, we've changed the progress bar color to a deep blue:

[...]
backgroundColor: ['#1E90FF'],
[...]

AD

Adding Border Radius

For a more modern and stylish appearance, you can add border radius to your progress bars. To do this, you can apply CSS styles to the canvas element. In this example, we've added a border radius of 5px:

<style>
#progressBar {
border-radius: 5px;
}</style>
<canvas id="progressBar"></canvas>

Creating a Multi-colored Progress Bar

If you want to create a multi-colored progress bar, you can use multiple data sets and adjust the `cutoutPercentage` option accordingly. In this example, we've created a progress bar with two colors, displaying 30% and 20% progress:

[...]
data: {
datasets: [ {
data: [50],
backgroundColor: ['#65DAF5'],
borderWidth: 0
}, {
data: [70],
backgroundColor: ['#73C6B6'],
borderWidth: 0
} ],
labels: ['Progress 1', 'Progress 2']
},
[...]
cutoutPercentage: 65,
[...]

In this case, we've created two data sets, each representing a different stage of progress. We've also adjusted the `cutoutPercentage` to 65 to accommodate the additional data set.

Using Progress Bars in Real-world Scenarios

Now that you're familiar with creating and customizing Chart.js progress bars, let's explore some real-world use cases to inspire your own projects.

User Progress Tracking

Progress bars are an excellent way to visualize user progress within a tutorial, course, or onboarding process. In this example, we'll use a progress bar to track a user's progress through a multi-step form:

<canvas id="userProgress"></canvas>
<script>
const ctx = document.getElementById('userProgress').getContext('2d');
const steps = 5;
let progress = 0;
new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [ {
data: [progress],
backgroundColor: ['#65DAF5'],
borderWidth: 0
} ],
labels: ['Progress']
},
options: {
cutoutPercentage: 85,
tooltips: {
enabled: false
}
}
});
function updateProgress() {
progress = (progress + 1) % steps;
chart.data.datasets[0].data = [progress / (steps - 1) * 100];
chart.update();
}
</script>

In this example, we've added an `updateProgress` function that increments the progress bar by one step (out of five) and updates the chart accordingly. This allows you to dynamically update the progress bar as the user completes each step of the form.

Loading Spinners and Animations

Progress bars can also be used to create engaging loading spinners or animations, providing users with visual feedback while waiting for an action to complete. In this example, we'll create a loading spinner that continuously rotates to indicate that an operation is in progress:

[...]
animation: {
animateRotate: true,
rotation: 1,
}
[...]

By enabling `animateRotate` and setting the `rotation` property, we've created a loading spinner that continuously rotates around the center of the doughnut chart. This can be used to create a more interactive and engaging user experience while waiting for an operation to complete.

Conclusion and Next Steps

Chart.js progress bars offer a powerful and flexible solution for visualizing data, tracking user progress, and creating engaging user experiences. By mastering the techniques covered in this guide, you'll be well-equipped to tackle a wide range of progress bar use cases and customize them to meet the unique needs of your projects.

As you continue to explore the world of Chart.js progress bars, we encourage you to experiment with the many customization options available and push the boundaries of what's possible. Happy coding!

AD