Open In App

Chart.js Axes

Chart.js Axes provides the necessary context, labels, and scaling to make the charts more informative so that they can look visually appealing. Axes can be applied on various types of charts like lines, bars, radar, etc.

Types of Axes

Example 1: Here in this example, we have configured the title for the y-axis to display the text Revenue (in $) in the green color, and the line on the chart will also be in the green color.






<!DOCTYPE html>
<html>
 
<head>
    <title>Line Chart with Custom Y-Axis Title</title>
    <!-- Include Chart.js library -->
    <script src=
      </script>
</head>
 
<body>
    <div style="width: 80%; margin: auto;">
        <!-- Create a canvas element to render the chart -->
        <canvas id="myLineChart"></canvas>
    </div>
 
    <script>
        // Sample data for the chart
        const data = {
            labels: ['January', 'February',
                'March', 'April', 'May'],
            datasets: [{
                label: 'Monthly Revenue',
                data: [1500, 2200, 1800, 2400, 2000],
                borderColor: 'green',
                fill: false
            }]
        };
 
        // Get the canvas element
        const ctx = document.
            getElementById('myLineChart').getContext('2d');
 
        // Create the chart
        const chart = new Chart(ctx, {
            type: 'line',
            data: data,
            options: {
                scales: {
                    y: {
                        title: {
                            display: true,
                            text: 'Revenue (in $)',
                            align: 'center',
                            color: 'green',
                            font: {
                                size: 14,
                                family: 'Arial',
                                style: 'italic'
                            },
                            padding: 10
                        }
                    }
                }
            }
        });
    </script>
</body>
 
</html>

Output:



Example 2: Here in this example, we will use the some options, like we wil make the chart circular.




<!DOCTYPE html>
<html>
 
<head>
    <title>Radar Chart with
        Circular Grid Lines</title>
    <!-- Include Chart.js library -->
    <script src=
      </script>
</head>
 
<body>
    <div style="width: 80%; margin: auto;">
        <!-- Create a canvas
            element to render the radar chart -->
        <canvas id="myRadarChart"></canvas>
    </div>
 
    <script>
        // Sample data for the radar chart
        const data = {
            labels: ['Sales', 'Marketing',
                'Development', 'Design', 'Support'],
            datasets: [{
                label: 'Team Skills',
                data: [80, 70, 90, 60, 75],
                // Area fill color
                backgroundColor: 'rgba(75, 192, 192, 0.7)',
                // Border color
                borderColor: 'rgba(75, 192, 192, 1)',
                // Border width
                borderWidth: 2,
            }]
        };
 
        // Get the canvas element
        const ctx = document.getElementById('myRadarChart')
        .getContext('2d');
 
        // Create the radar chart
        // with circular grid lines
        const chart = new Chart(ctx, {
            type: 'radar',
            data: data,
            options: {
                scales: {
                    r: {
                        grid: {
                            // Set grid lines to be circular
                            circular: true,
                            // Set grid line color
                            color: 'rgba(0, 0, 255, 0.2)',
                            // Show grid lines
                            display: true,
                            // Do not draw lines beside the ticks
                            drawTicks: false,
                            // Set grid line width
                            lineWidth: 2,
                            // Set grid line length
                            // into the axis area
                            tickLength: 10,
                        }
                    }
                },
                elements: {
                    line: {
                        // Line curve tension
                        tension: 0.4,
                    }
                },
                plugins: {
                    legend: {
                        // Show legend
                        display: true,
                        // Legend position
                        position: 'top',
                    }
                }
            }
        });
    </script>
</body>
 
</html>

Output:


Article Tags :