Chart JS data labels getting cut
Asked Answered
V

3

18

I am using Chart JS with data label plugin and I want to show data labels next to the bar and pie chart but noticed that some of the data labels are getting cut or going out of canvas. Is their any way to fix this?

    var chartData3 = {
        labels: ["January", "February", "March", "April", "May", "June"],
    datasets: [{
      backgroundColor: "#79D1CF",
      data: [60, 90, 81, 56, 55, 40],
      datalabels: {
        align: 'end',
        anchor: 'end'
      }
    }]
  };

https://plnkr.co/edit/I906pCN8tdrrOX2hgN0W?p=preview

Thanks, MSK

Visakhapatnam answered 7/5, 2018 at 12:51 Comment(0)
T
45

You need to set padding to display the labels properly. Also, adjust the canvas width and height to account for padding so that your chart doesn't get too small.

options: {        
    layout: {
        padding: {
            left: 50,
            right: 50,
            top: 50,
            bottom: 50
        }
    }
}
Tasiatasiana answered 18/5, 2018 at 20:33 Comment(1)
Live demo: https://jsfiddle.net/vmk2ew15/.Zingg
U
6

Set max tick in chart option, for example:

this.chartOption.scales.yAxes[0].ticks.max = Math.max(...chartData3.datasets[0].data) * 1.2
Unthinking answered 31/8, 2018 at 7:48 Comment(0)
P
0

I found a workaround. If you set to true the property "ticks-display" makes the canvas grow to show labels. After that, you can set ticks color to match with the background color in order to hide ticks.

Polacre answered 29/10, 2023 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.