ChartJs hiding odd index values on x-axes/labels values

👍:-1

when you are using the type:"scatter",
Scatter charts are based on basic line charts with the x axis changed to a linear axis. To use a scatter chart, data must be passed as objects containing X and Y properties. The example below creates a scatter chart with 4 pointslink for charts scatter charts

when you are not defining the values with x and y then used the x-axis and y-axis as linear and then you can manipulate the x-axes for both line and bar..if you will not provide the chart_type in config it will take axes as linear
you can
xAxisID: ‘x-axis-bar’,
xAxisID: ‘x-axis-line’,
and then hide the 1 x-axis and autoSkip=False it will show your all values.
I am attaching the all code below..

const data = {
            labels: dates,
            datasets: [{
                type: 'line',
                label: 'Line graph',
                data: line,
                fill: false,
                borderColor: '#ff5252',
                backgroundColor: '#ff5252',
                yAxisID: 'y-axis-a',
                xAxisID: 'x-axis-line',
                lineTension: 0.5,
                order:1,
            },{
                type: 'bar',
                label: 'Bar graph',
                data: diff,
                borderColor: colors,
                backgroundColor: colors,
                borderWidth: 2,
                pointRadius: 5,
                xAxisID: 'x-axis-bar',
                pointBorderWidth: 1,
                pointHoverRadius: 7,
                pointHoverBorderWidth: 2,
                fill: true,
                showLabelBackdrop:true,
                order:2
            },]
        };
        const config = {
            data: data,
            options: {
                responsive: false,
                scales: {
                    y: {
                        display:false,
                        beginAtZero: true,
                        ticks: {
                            color: "#ffffff",
                            font: {
                                size: 14,
                            },
                        },
                    },
                    'x-axis-bar':{
                        ticks:{
                            autoSkip:false,
                            stepSize:1.0,
                        }
                    },
                    'x-axis-line':{
                      display:false  
                    }
                },
                plugins: {
                    labels: {
                        ticks:{
                            font: {
                            size: 22
                        },
                            color: "#ffffff",
                        }
                    },
                    title: {
                        display: true,
                        text: title,
                        padding: {
                            top: 10,
                            bottom: 30,
                        },
                        font: {
                            size: 22,
                        },
                        color: "#ffffff"
                    }
                }
            }
        };

        const ctx = document.querySelector(`#chart${index+1}`).getContext('2d');
        
        const myChart = new Chart(ctx, config)

Leave a comment