How to limit number of data points on x-axis using chart.js?

Member

by lottie , in category: Javascript , 2 months ago

How to limit number of data points on x-axis using chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 2 months ago

@lottie 

To limit the number of data points shown on the x-axis using Chart.js, you can use the maxTicksLimit option in the x-axis configuration. This option allows you to specify the maximum number of ticks (data points) to display on the x-axis.


Here's an example of how you can use the maxTicksLimit option to limit the number of data points on the x-axis:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May'],
        datasets: [{
            label: 'My Dataset',
            data: [10, 20, 30, 40, 50],
        }]
    },
    options: {
        scales: {
            x: {
                ticks: {
                    maxTicksLimit: 3 // Limit to 3 data points on the x-axis
                }
            }
        }
    }
});


In this example, the maxTicksLimit option is set to 3, which will limit the number of data points displayed on the x-axis to 3. You can adjust this value to display the desired number of data points on the x-axis in your Chart.js chart.