How to display half chart.js donut graph?

Member

by addison , in category: Javascript , 2 months ago

How to display half chart.js donut graph?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 2 months ago

@addison 

To display a half chart.js donut graph, you can achieve this by setting the startAngle and endAngle properties of the chart configuration. Here is an example of how to create a half donut graph using Chart.js:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Data for the donut graph
var data = {
    labels: ['A', 'B'],
    datasets: [{
        data: [30, 70],
        backgroundColor: [
            '#FF6384',
            '#36A2EB'
        ]
    }]
};

// Configuration for the donut graph
var options = {
    cutoutPercentage: 75,
    rotation: -0.5 * Math.PI,
    circumference: Math.PI
};

// Get the canvas element
var ctx = document.getElementById('myChart').getContext('2d');

// Create the donut graph
var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: data,
    options: options
});


In the above code snippet, the key points to creating a half donut graph are:

  1. Setting the cutoutPercentage property to create a half-circle effect.
  2. Setting the rotation property to -0.5 * Math.PI to start the graph at the top instead of the default 0 degrees
  3. Setting the circumference property to Math.PI to show only half of the circle.


By setting these properties in the Chart.js configuration options, you can create a half donut graph with your desired data and styling.