Open In App

Chart.js Elements Configuration

Chart.js Elements Configuration provides us with the option to configure four types of elements: arcs, lines, points, and bars. Developers can use the options provided by Elements Configuration to customize these elements to change the appearance and behavior of the charts that meet their requirements.

Syntax:

let ctx = document.getElementById('myChart').getContext('2d');
let myChart = new Chart(ctx, {
type: 'chartType',
data: {
// chart data here
},
options: {
elements: {
{ /* element to configure */ }: {
// configuration options here
}
}
}
});

Point Configuration

Point elements represent individual points in line, radar, or bubble charts, we can customize points in our charts using the options provided below.



These are the following options available to customize a point:

Line Configuration

Line elements represent the lines connecting data points in a line chart. we can customize lines in our charts using the options provided below.



These are the following options available to customize lines:

Bar Configuration

Bar elements represent the bars in a bar chart. we can customize bars in our charts using options provided below.

These are the following options available to customize bar:

Arc Configuration

Arcs are used in polar area, doughnut, and pie charts. we can customize Arcs in our charts using options provided below.

These are the following options available to customize arc:

Example: In this example, a line chart using Chart.js is created to visualize monthly sales data with custom point styling.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Chart.js Example</title>
    <script src=
      </script>
 
<body>
    <canvas id="myChart" width="400" height="200">
    </canvas>
    <script>
 
        let ctx =
            document.getElementById('myChart').getContext('2d');
 
        let myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: ['January',
                    'February',
                    'March',
                    'April',
                    'May'],
                datasets: [{
                    label: 'Monthly Sales',
                    data: [12, 19, 3, 5, 2],
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 3,
                    pointBackgroundColor: 'rgba(255, 99, 132, 1)',
                    pointBorderColor: 'rgba(255, 99, 132, 1)',
                    pointRadius: 6,
                    pointHoverRadius: 8,
                }]
            },
            options: {
                elements: {
                    point: {
                        pointStyle: 'triangle',
                        rotation: 45,
                    },
                    line: {
                        tension: 0.2,
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        }
        );
 
    </script>
</body>
 
</html>

Output:

Example 2: In this example, a customized doughnut chart is created using Chart.js, featuring distinct arcs with a specified angle.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Customized Arc Example</title>
    <script src=
      </script>
</head>
 
<body>
    <canvas id="myChart"
            width="400"
            height="200">
      </canvas>
 
    <script>
        let ctx = document
                .getElementById('myChart')
                .getContext('2d');
 
        let myChart = new Chart(ctx, {
            type: 'doughnut',
            data: {
                labels: ['Red',
                         'Blue',
                         'Yellow'],
                datasets: [{
                    data: [30, 50, 20],
                    backgroundColor: ['rgba(255, 99, 132, 0.7)',
                                      'rgba(54, 162, 235, 0.7)',
                                      'rgba(255, 255, 0, 0.7)'],
                }]
            },
            options: {
                elements: {
                    arc: {
                        angle: Math.PI / 4,
                        backgroundColor: 'rgba(75, 192, 192, 0.5)',
                        borderWidth: 3,
                        borderColor: '#fff',
                        borderAlign: 'inner',
                        borderDash: [5, 5],
                        borderDashOffset: 2,
                    }
                }
            }
        });
    </script>
</body>
 
</html>

Output:


Article Tags :