Open In App

How to Create a Horizontal Scrolling Chart.js Line Chart with a Locked Y Axis ?

We will learn how to create a horizontal scrolling Chart.js line chart with a locked y-axis using the ChartJS CDN library.

Approach

CDN Link:

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

Syntax:

        scales: {
x: {
ticks: {
display: false
},
gridLines: {
display: false
}
},
y: {
afterFit: (c) => {
c.width = 40;
}
}
}
}
});

Example: The following code shows a simple example of a line chart showcasing a sample dataset with a list of programming languages on the x-axis and a number dataset on the y-axis, and the y-axis is locked in place with horizontal scrolling enabled if the dataset is too large.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <style>
        body {
            height: 90vh;
            background-color: rgb(239, 237, 237);
            display: flex;
            justify-content: center;
            align-items: center;
        }
 
        .chart-container {
            width: 700px;
            display: flex;
        }
 
        .small-column {
            width: 40px;
        }
 
        .large-column {
            max-width: 700px;
            overflow-x: scroll;
        }
 
        .container {
            width: calc(1000px - 40px);
            height: 500px;
        }
    </style>
    <title>Document</title>
</head>
 
<body>
    <div class="chart-box">
        <div class="chart-container">
            <div class="small-column">
                <canvas id="mySecondChart"></canvas>
            </div>
            <div class="large-column">
                <div class="container">
                    <canvas id="myChart"></canvas>
                </div>
            </div>
        </div>
    </div>
    <script src=
    <script>
        const ctx = document.getElementById('myChart');
        new Chart(ctx, {
            type: 'line',
            data: {
                labels: ['Python', 'JavaScript', 'C++', 'C', 'Java', 'PHP'],
                datasets: [{
                    label: 'Percentage (%)',
                    data: [49, 63, 22, 19, 30, 19],
                    borderColor: 'green',
                    fill: false,
                }]
            },
            options: {
                maintainAspectRatio: false,
                layout: {
                    padding: {
                        top: 11
                    }
                },
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        ticks: {
                            display: false
                        },
                        border: {
                            display: false
                        }
                    }
                }
            }
        });
 
        const ctx2 = document.getElementById('mySecondChart');
        new Chart(ctx2, {
            type: 'line',
            data: {
                datasets: [{
                    data: [49, 63, 22, 19, 30, 19],
                    fill: false,
                }]
            },
            options: {
                maintainAspectRatio: false,
                layout: {
                    padding: {
                        bottom: 36
                    }
                },
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    x: {
                        ticks: {
                            display: false
                        },
                        gridLines: {
                            display: false
                        }
                    },
                    y: {
                        afterFit: (c) => {
                            c.width = 40;
                        }
                    }
                }
            }
        });
    </script>
</body>
 
</html>

Output:




Article Tags :