Skip to content Skip to sidebar Skip to footer

Chartjs Hide Labels On Small Screen Sizes

I have a problem hiding xAxes and yAxes labels on small screen sizes (mobile phones). I know there is this option: options: { scales: {

Solution 1:

Try this:

onResize: function(myChart, size) {
    myChart.options.scales.xAxes[0].ticks.display = (size.height >= 140);
}

In order to get the option on load on mobile, you should do this:

functionisMobileDevice(){
    return ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
}

var myChart = newChart(ctx, {
    options :
        scales:
        {
            xAxes: [{
                ticks:{
                    display: !isMobileDevice()
                }

            }];
        } 
})

Solution 2:

Have a try with

var myChart = newChart(ctx, {
    //chart data and options,onResize: function(myChart, size) {

     var showTicks = (size.height < 140) ? false : true;

     myChart.options = {
            scales: {
                xAxes: [{
                    ticks: {
                        display: showTicks
                    }
                }];
            }
     };

  }
});

Solution 3:

For Angular you can make the logic like this... the screen.width will counted as your viewport width

canvas: any;
  ctx: any;
  legend: any;
  font: any;
  constructor() { }

  ngOnInit(): void {
    this.canvas = document.getElementById('tpChart');
    this.ctx = this.canvas.getContext('2d');
    this.legend = (screen.width < 575) ? false : true; //when viewport will be under 575pxthis.font = (screen.width < 1200) ? 14 : 16; //when viewport will be under 1200pxlet tpChart = newChart(this.ctx, {
      type: 'doughnut',
      data: {
        datasets: [{
          borderWidth: 2,
          data: [70, 50, 40, 30],
          backgroundColor: [
            '#00CDB6',
            '#F08C2E',
            '#0F9AF0',
            '#F16C51',
          ],
        }],
        labels: [
          'United Kingdom',
          'Bangladesh',
          'United States',
          'Others',
        ]
      },
      options: {
        responsive: true,
        cutoutPercentage: 65,
        spanGaps: false,
        legend: {
          display: this.legend, //This will work dynamaticallyposition: "right",
          align: "center",
          labels: {
            fontColor: '#484848',
            boxWidth: 10,
            fontSize: this.font, //This will work dynamaticallyfontFamily: "Cabin",
            padding: 25,
            usePointStyle: true
          },
          
        }
      },
    });
  }

Post a Comment for "Chartjs Hide Labels On Small Screen Sizes"