Open In App

Chart.js Styling Axes

Chart.js Styling Axes allows us to represent the data in a visually appealing and more informative way. The customization of axis elements, ticks, labels, and grid lines is styled here to make the data representation clearer and more engaging.

Syntax:

const config = {
    type: 'your_chart_type',
    data:,
    options: {
        scales: {
            x: { title: { 
                display: true, 
                text: 'X-Axis Title', 
                color: 'your_color' 
            }, ticks: { 
                color: 'your_color' 
            } },
            y: { title: { 
                display: true, 
                text: 'Y-Axis Title', 
                color: 'your_color' 
            }, ticks: { 
                color: 'your_color' 
            } },
        },
    },
};

Grid Line Configuration

Tick Configuration

Major Tick Configuration

Border Configuration

CDN link

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.1.2/chart.umd.js"></script>

Example 1: In the below example, we have used the Chart.js Bar chart to present the data related to GeeksforGeeks. We have used the styling axe configuration options like ticks, grid, and border to style the x and y axes of the Bar Chart with various colors and other options.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <script src=
    </script>
    <script src=
    </script>
    <title>Bar Chart - Axes Styling</title>
</head>
 
<body>
    <div>
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
         
        <h3>Axes Styling - Bar Chart </h3>
         
        <div style="width:80%;">
            <canvas id="barChart"></canvas>
        </div>
    </div>
 
    <script>
        const data = {
            labels: ['Python', 'JavaScript', 'Java', 'C++', 'HTML'],
            datasets: [{
                label: 'GeeksforGeeks Courses',
                data: [200, 350, 300, 180, 120],
                backgroundColor: 'rgba(75, 192, 192, 0.6)',
                borderColor: 'rgba(75, 192, 192, 1)',
                borderWidth: 2,
            }],
        };
 
        const config = {
            type: 'bar',
            data: data,
            options: {
                scales: {
                    x: {
                        display: true,
                        title: {
                            display: true,
                            text: 'Programming Languages',
                            color: 'blue',
                        },
                        ticks: {
                            color: 'green',
                            font: { size: 12, weight: 'bold' },
                        },
                        grid: {
                            color: ['red', 'orange',
                                    'yellow', 'green', 'blue'],
                            display: true,
                            lineWidth: 2,
                            drawOnChartArea: false,
                        },
                        border: {
                            display: true,
                            color: 'purple',
                            width: 2,
                        },
                    },
                    y: {
                        display: true,
                        title: {
                            display: true,
                            text: 'Number of Courses',
                            color: 'red',
                        },
                        ticks: {
                            color: 'orange',
                            font: { size: 12, weight: 'bold' },
                        },
                        grid: {
                            display: true,
                            drawTicks: false,
                        },
                    },
                },
            },
        };
        const myChart =
            new Chart(document.getElementById('barChart'), config);
    </script>
</body>
 
</html>

Output:



Example 2: In the below example, we have used the Radar Chart in which we have various Styling Axes options like ticks, grid, and border. As in the Radar Chart, there is only 1 axis, so we have used this option for only one axis.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <script src=
    </script>
    <script src=
    </script>
    <title>Radar Chart - Axes Styling</title>
</head>
 
<body>
    <div>
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
         
        <h3>Axes Styling - Radar Chart </h3>
         
        <div style="width:80%;">
            <canvas id="radarChart"></canvas>
        </div>
    </div>
 
    <script>
        const data = {
            labels: ['HTML', 'CSS',
                'JavaScript', 'Python', 'Java', 'C++'],
            datasets: [{
                label: 'Programming Skills',
                data: [80, 70, 90, 65, 75, 60],
                backgroundColor: 'rgba(255, 99, 132, 0.6)',
                borderColor: 'rgba(255, 99, 132, 1)',
                borderWidth: 2,
            }],
        };
 
        const config = {
            type: 'radar',
            data: data,
            options: {
                scales: {
                    r: {
                        display: true,
                        title: {
                            display: true,
                            text: 'Skill Level',
                            color: 'blue',
                        },
                        ticks: {
                            color: 'green',
                            font: { size: 12, weight: 'bold' },
                        },
                        grid: {
                            circular: true,
                            display: true,
                            lineWidth: 2,
                        },
                        border: {
                            display: true,
                            color: 'purple',
                            width: 2,
                        },
                    },
                },
            },
        };
 
        const myChart = new Chart(
            document.getElementById('radarChart'), config);
    </script>
</body>
 
</html>

Output:


Article Tags :