DEV Community

Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How To Create Bootstrap Charts using Bootstrap 5

What are charts and uses?

Data in its raw form is usually hard to comprehend and make meaning off. To make use of data or be able to read meaning from it, it needs to be presented in a way that is easy to understand and this is where Charts come in. A chart is basically a graphical representation of data and in this article, we’ll be looking at how to add charts to our webpage using chartjs and contrast.

AD-BANNER

Prerequisites

The following are nice haves if you want to follow and understand the article well:

  • Basic HTML Knowledge
  • Basic CSS Knowledge
  • Basic Bootstrap knowledge

Setup(using chartjs)

First thing we do is to create a folder that will hold all our files for this tutorial, I named mine bootstrap-chart, you can choose whatever name of your choice. Then we go ahead to create an index.html file which will contain most of our codes. In order to use contrast and chartjs in our project we have to add the CDN links to our code.

After adding the required CDN links, our index.html file should look like this.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <link rel="stylesheet"
    href="../contrast-bootstrap-pro/css/bootstrap.min.css"/>
  <link
    rel="stylesheet"
    href="../contrast-bootstrap-pro/css/cdb.css" />
  <script
    src="../contrast-bootstrap-pro/js/cdb.js"></script>
  <script
    src="../contrast-bootstrap-pro/js/bootstrap.min.js">
  </script>
  <script src="https://kit.fontawesome.com/9d1d9a82d2.js"
  crossorigin="anonymous"></script>

  <title>How to create bootstrap charts using bootstrap 5</title>
</head>

<body>

</body>

<script
  src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js">
</script>

Enter fullscreen mode Exit fullscreen mode

Now, let’s go ahead to create our charts.

Line Chart.

The first type of chart we’ll be using is the line chart. His is the simplest chart that we can actually use in our website. It is a plotting of data points on a line usually on a 2-dimensional graph. Let’s add a style tag so we can style our charts container.

<style>
  .chart-container {
    width: 50%;
    height: 50%;
    margin: auto;
  }
</style>

Enter fullscreen mode Exit fullscreen mode

Then add the following to our body tag.

<div class="card chart-container">
  <canvas id="chart"></canvas>
</div>

Enter fullscreen mode Exit fullscreen mode

Now let’s add a script tag where we select the chart canvas and apply the chart settings to it. ...

<script>
      const ctx = document.getElementById("chart").getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'line',
        data: {
          labels: ["sunday", "monday", "tuesday",
          "wednesday", "thursday", "friday", "saturday"],
          datasets: [{
            label: 'Last week',
            backgroundColor: 'rgba(161, 198, 247, 1)',
            borderColor: 'rgb(47, 128, 237)',
            data: [3000, 4000, 2000, 5000, 8000, 9000, 2000],
          }]
        },
        options: {
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true,
              }
            }]
          }
        },
      });
</script>

Enter fullscreen mode Exit fullscreen mode

In the code above, the first thing we do is selecting the chart element, then we create a new chart instance using ChartJs and give it the properties we need such as the type of chart, data we want to use and other options that will help us customize the chart.

Now our code should look like this.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <link
    rel="stylesheet"
    href="../contrast-bootstrap-pro/css/bootstrap.min.css" />
  <link
    rel="stylesheet"
    href="../contrast-bootstrap-pro/css/cdb.css" />
  <script
    src="../contrast-bootstrap-pro/js/cdb.js"></script>
  <script
    src="../contrast-bootstrap-pro/js/bootstrap.min.js"></script>
  <script
    src="https://kit.fontawesome.com/9d1d9a82d2.js"
    crossorigin="anonymous"></script>

  <title>How to create bootstrap charts using bootstrap 5</title>
</head>
<style>
  .chart-container {
    width: 50%;
    height: 50%;
    margin: auto;
  }
</style>

<body>
  <div class="card chart-container">
    <canvas id="chart"></canvas>
  </div>

</body>

<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js">
</script>
<script>
      const ctx = document.getElementById("chart").getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'line',
        data: {
          labels: ["sunday", "monday", "tuesday",
          "wednesday", "thursday", "friday", "saturday"],
          datasets: [{
            label: 'Last week',
            backgroundColor: 'rgba(161, 198, 247, 1)',
            borderColor: 'rgb(47, 128, 237)',
            data: [3000, 4000, 2000, 5000, 8000, 9000, 2000],
          }]
        },
        options: {
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true,
              }
            }]
          }
        },
      });
</script>

</html>

Enter fullscreen mode Exit fullscreen mode

With this, we have a chart that looks like it.

Bootstrap Charts

Bar Chart.

A bar chart is a type of chart that represents values as vertical or horizontal bars. They are usually used to compare multiple data or show distribution of data points. The code for our bar chart is shown below. Taking a good look, the only things that change are the type of chart option, data and labels.

<script>
      const ctx = document.getElementById("chart").getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'bar',
        data: {
          labels: ["rice", "yam", "tomato", "potato",
          "beans", "maize", "oil"],
          datasets: [{
            label: 'food Items',
            backgroundColor: 'rgba(161, 198, 247, 1)',
            borderColor: 'rgb(47, 128, 237)',
            data: [300, 400, 200, 500, 800, 900, 200],
          }]
        },
        options: {
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true,
              }
            }]
          }
        },
      });
</script>

Enter fullscreen mode Exit fullscreen mode

Bootstrap Charts

Pie Chart

A pie chart is a type of chart where a circle is divided up into parts(slices) which represent a proportion of the available data

<script>
      const ctx = document.getElementById("chart").getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'pie',
        data: {
          labels: ["rice", "yam", "tomato", "potato",
          "beans", "maize", "oil"],
          datasets: [{
            label: 'food Items',
            backgroundColor: 'rgba(161, 198, 247, 1)',
            borderColor: 'rgb(47, 128, 237)',
            data: [30, 40, 20, 50, 80, 90, 20],
          }]
        },
      });
</script>

Enter fullscreen mode Exit fullscreen mode

The code above translates to the pie chart in the image below.

Bootstrap Charts

Doughnut

This chart is similar to the pie chart except that it has a cutout in its center which makes it resemble a doughnut, hence the name.

<script>
      const ctx = document.getElementById("chart").getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'doughnut',
        data: {
          labels: ["rice", "yam", "tomato", "potato", "beans",
           "maize", "oil"],
          datasets: [{
            label: 'food Items',
            data: [30, 40, 20, 50, 80, 90, 20],
            backgroundColor: ["#0074D9", "#FF4136", "#2ECC40",
            "#FF851B", "#7FDBFF", "#B10DC9", "#FFDC00",
            "#001f3f", "#39CCCC", "#01FF70", "#85144b",
            "#F012BE", "#3D9970", "#111111", "#AAAAAA"]
          }]
        },
      });
</script>

Enter fullscreen mode Exit fullscreen mode

In the doughnut chart, we add change the background color opton from a single value to an array of values so that each segment in the doughnut will have its own color as shown in the image below.

Bootstrap Charts

There are a lot of other variations and options for charts which can not all be covered in this article but you can check them out in the official chartjs documentation. With this, we now know how to create and use charts in our projects.

Create Stunning Websites and Web Apps

Building different custom components in react for your web apps or websites can get very stressful. Thats why we decided to build contrast. We have put together a UI kit with over 10000+ components, 5 admin dashboards and 23 additional different pages template for building almost any type of web app or webpage into a single product called Contrast Pro. Try contrast pro!

Contrast Design Boostrap

Contrast React Bootstrap PRO is a Multipurpose pro template, UI kit to build your next landing, admin, SAAS, prelaunch, etc., project with a clean, well documented, well crafted template and UI components. Learn more about Contrast Pro

Resources

Top comments (0)