Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Hide specific x axis **labels** in Chartjs?

Open Nawal00 opened this issue 6 years ago • 1 comments

I would like to hide specific x axis labels that are less than moment().valueOf(). So If I have 12:40, 12:45, 12:50 and 12:55 is the current time then I want to display 12:55 and hide the past time interval labels. Key here is to only hide the labels not to get rid of them completely.

My chart max x-axis time is always present time and increases as you pan right.

I've managed to get filtered ticks time as an array ["12:55"] but the chart still shows all the other time before 12:55 . ANy help is appreciated. Thank you

enter image description here

My xAxis configuration

 xAxes: [
                {
                    xAxisID: 'x-1',
                    type: "time",
                    time: {
                        unit: "minute",
                        displayFormats: { minute: 'HH:mm' },
                        stepSize: 5,
                        min: chartMin,
                        max: chartDisplayMax,
                    },
                    afterBuildTicks: function (axis) {
                        const { ticks } = axis;
                        const ticksToDisplay= ticks && ticks.filter(tick => tick >= moment().subtract(5, "minutes").format('HH:mm'));
                        return ticksToDisplay;
                    },
                    ticks: {
                        display: true,
                    },
                },
            ],

enter image description here

Nawal00 avatar Jul 23 '20 12:07 Nawal00

I'm not sure tick >= moment().subtract(5, "minutes").format('HH:mm') works as intended. You should compare timestamps or moment objects and not strings

Please ask questions on StackOverflow in the future: https://stackoverflow.com/questions/tagged/chart.js

benmccann avatar Jul 23 '20 12:07 benmccann