How to add labels into the chart.js?

Member

by ryleigh , in category: Javascript , 7 months ago

How to add labels into the chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 7 months ago

@ryleigh 

To add labels into a chart created with Chart.js, you can use the "labels" property within the dataset options. Here's an example of adding labels to a pie chart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
var ctx = document.getElementById('myChart').getContext('2d');

var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'red',
                'blue',
                'yellow',
                'green',
                'purple',
                'orange'
            ]
        }]
    }
});


In this example, the labels are defined in the "labels" property of the data object. You can add labels for each data point in a similar way for other types of charts as well.