Open In App
Related Articles

How to create chart using bootstrap ?

Improve Article
Improve
Save Article
Save
Like Article
Like

A chart in Bootstrap is a graphical representation for data visualization, in which the data is represented by symbols. The various types of charts like bar charts, line charts, pie charts, donut charts, etc are created with the help of Bootstrap. In other words, we can say that chart is a type of diagram or graph, that organizes and represents a set of numerical or qualitative data.

Approach 1: In this approach, we will create a line chart with the help of Bootstrap and javascript. in which we have used the chart.js file for creating a chart 

Example: In this example, we are creating a line chart. The data is created according to the type of chart. The following chart has the type “line” with 2 different data both for working hours vs free hours.

HTML




<html>
<link rel=
      type="text/css" />
<script src=
<script src=
        type="text/javascript">
</script>
<script src=
<script src=
<style>
    .container {
        width: 70%;
        margin: 15px auto;
    }
 
    body {
        text-align: center;
        color: green;
    }
 
    h2 {
        text-align: center;
        font-family: "Verdana", sans-serif;
        font-size: 30px;
    }
</style>
 
<body>
    <div class="container">
        <h2>Line Chart</h2>
        <div>
            <canvas id="myChart"></canvas>
        </div>
    </div>
</body>
 
<script>
    let ctx = document.getElementById("myChart").getContext("2d");
    let myChart = new Chart(ctx, {
        type: "line",
        data: {
            labels: [
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday",
            ],
            datasets: [
                {
                    label: "work load",
                    data: [2, 9, 3, 17, 6, 3, 7],
                    backgroundColor: "rgba(153,205,1,0.6)",
                },
                {
                    label: "free hours",
                    data: [2, 2, 5, 5, 2, 1, 10],
                    backgroundColor: "rgba(155,153,10,0.6)",
                },
            ],
        },
    });
</script>
</html>


Output: 

line chart with working and free hours

Approach 2: In this approach, we will create a donut chart by using Bootstrap and javascript, Doughnut charts are the modified version of Pie Charts with the area of the center cut out.

Example: In this example, we have used the chart.js file for creating a donut chart. 

HTML




<html>
<script src=
<script src=
<link rel="stylesheet"
      href=
<link rel=
      type="text/css" />
 
<script src=
</script>
<script src=
</script>
 
<script src=
</script>
 
<style>
    body {
        text-align: center;
        color: green;
    }
 
    h2 {
        text-align: center;
        font-family: "Verdana", sans-serif;
        font-size: 40px;
    }
</style>
 
<body>
    <div class="col-xs-12 text-center">
        <h2>Donut Chart</h2>
    </div>
 
    <div id="donut-chart"></div>
 
    <script>
        let chart = bb.generate({
            data: {
                columns: [
                    ["Blue", 2],
                    ["orange", 4],
                    ["green", 3],
                ],
                type: "donut",
                onclick: function (d, i) {
                    console.log("onclick", d, i);
                },
                onover: function (d, i) {
                    console.log("onover", d, i);
                },
                onout: function (d, i) {
                    console.log("onout", d, i);
                },
            },
            donut: {
                title: "70",
            },
            bindto: "#donut-chart",
        });
    </script>
</body>
</html>


Output:

Doughnut chart


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials