Chart.js Doughnut and Pie Charts (original) (raw)

Last Updated : 23 Jul, 2024

Doughnut and Pie Charts

Doughnut and Pie Charts Dataset Properties

Below are some of the dataset properties of Doughnuts and Pie Charts.

Approach

Syntax

new Chart($("#ID"), {
type: 'doughnut'/'pie'
data: { ... },
options: { ... }
});

**Example 1: In this example, we have implemented the Doughnut Chart that represents the data related to GeeksforGeeks course distribution. Each ring of the chart is been customized with a different color.

HTML `

Courses Distribution

GeeksforGeeks

    <h3>Chart JS Doughnut Chart </h3> 
    
    <div> 
        <canvas id="coursesDoughnutChart"
            width="700" height="250"></canvas> 
    </div> 
</div> 

<script> 
    const coursesData = { 
        labels: ['Python', 'JavaScript', 
            'Java', 'C++', 'Data Structures'], 
        datasets: [{ 
            data: [30, 20, 15, 10, 25], 
            backgroundColor: ['#FF6384', '#36A2EB', 
                '#FFCE56', '#4CAF50', '#9C27B0'], 
        }], 
    }; 

    const config = { 
        type: 'doughnut', 
        data: coursesData, 
        options: { 
            plugins: { 
                title: { 
                    display: true, 
                    text: 'GeeksforGeeks Courses Distribution', 
                }, 
            }, 
        }, 
    }; 
    const ctx = document.getElementById( 
        'coursesDoughnutChart').getContext('2d'); 
        
    new Chart(ctx, config); 
</script> 

`

**Output:

**Example 2: In this example, we have implemented the Pie Chart, where we are representing the GeeksforGeeks Programming Language Usage visuals in the slices form. We have customized the slices with different colors.

HTML `

Programming Languages Usage

GeeksforGeeks

    <h3>Chart JS Pie Chart </h3> 
    
    <div> 
        <canvas id="programmingLanguagesPieChart"
            width="700" height="250"></canvas> 
    </div> 
</div> 
<script> 
    const languagesData = { 
        labels: ['Python', 'JavaScript', 'Java', 'C++', 'C#'], 
        datasets: [{ 
            data: [25, 20, 15, 10, 30], 
            backgroundColor: ['#FF6384', '#36A2EB', 
                '#FFCE56', '#4CAF50', '#9C27B0'], 
        }], 
    }; 
    const config = { 
        type: 'pie', 
        data: languagesData, 
        options: { 
            plugins: { 
                title: { 
                    display: true, 
                    text: 'GeeksforGeeks Programming Languages Usage', 
                }, 
            }, 
        }, 
    }; 
    const ctx = document.getElementById( 
        'programmingLanguagesPieChart').getContext('2d'); 
        
    new Chart(ctx, config); 
</script> 

`