Open In App

Chart.js Cartesian Axes

Axes play a crucial role in Chart.js, providing context, labels, and scaling to enhance the informativeness and visual appeal of the charts. Axes apply to various types of charts, including line charts, bar charts, radar charts, and more.

Types of Axes

Syntax:

const config = {
type: 'chart type',
data: data,
options: {
scales: {
x: {
'component': {
//configuration you want
},
},
y: {
'component': {
//configuration you want
},
},
},
},
};

Example 1: In this example, we are creating a linear (numeric) x-axis, with grid lines.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Chart.js Line Chart with Linear X-axis</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
 
    </style>
</head>
 
<body>
 
    <div style="width:80%;">
        <canvas id="myChart"></canvas>
    </div>
 
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const data = {
                datasets: [{
                    label: 'Monthly Sales',
                    data: [
                        { x: 1, y: 50 },
                        { x: 2, y: 75 },
                        { x: 3, y: 120 },
                        { x: 4, y: 90 },
                        { x: 5, y: 100 },
                    ],
                    borderColor: 'purple',
                    borderWidth: 2,
                    fill: false,
                }],
            };
 
            const config = {
                type: 'line',
                data: data,
                options: {
                    plugins: {
                        legend: {
                            display: false,
                        },
                    },
                    elements: {
                        line: {
                            tension: 0,
                        },
                    },
                    scales: {
                        x: {
                            type: 'linear',
                            position: 'bottom',
                            grid: {
                                color: 'red',
                                borderColor: 'blue',
                                tickColor: 'grey'
                            },
                        },
                        y: {
                            // You can customize the y-axis if needed
                        },
                    },
                },
            };
 
            const chart = new Chart(document.getElementById('myChart'), config);
        });
    </script>
 
</body>
 
</html>

Output:



Example 2: we have to create a line chart. The chart displays dynamic data with customized axes titles and styles. The X-axis represents categories, and the Y-axis starts at zero.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Chart.js Line Chart with Customized Category Axes</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
 
    </style>
</head>
 
<body>
 
    <div style="width:80%;">
        <canvas id="myChart"></canvas>
    </div>
 
    <script>
        document.addEventListener('DOMContentLoaded',
        function () {
            const data = {
                labels: ['Category 1', 'Category 2', 'Category 3',
                'Category 4', 'Category 5'],
                datasets: [{
                    label: 'Dynamic Data',
                    data: [50, 75, 120, 90, 100],
                    borderColor: 'purple',
                    borderWidth: 2,
                    fill: false,
                }],
            };
 
            const config = {
                type: 'line',
                data: data,
                options: {
                    plugins: {
                        legend: {
                            display: false,
                        },
                    },
                    elements: {
                        line: {
                            tension: 0,
                        },
                    },
                    scales: {
                        x: {
                            type: 'category',
                            title: {
                                display: true,
                                text: 'Category X-Axis',
                                color: 'red', // Customize title color
                            },
                        },
                        y: {
                            beginAtZero: true,
                        },
                    },
                },
            };
 
            const chart = new Chart(document
            .getElementById('myChart'), config);
        });
    </script>
 
</body>
 
</html>

Output:


Article Tags :