Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart JS data labels getting cut

Tags:

chart.js

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

like image 886
MSK Avatar asked May 07 '18 12:05

MSK


People also ask

How do you adjust the chart to show data labels?

To format data labels, select your chart, and then in the Chart Design tab, click Add Chart Element > Data Labels > More Data Label Options. Click Label Options and under Label Contains, pick the options you want. To make data labels easier to read, you can move them inside the data points or even outside of the chart.

How do you adjust a chart to show data labels in Excel?

On the Layout tab, in the Labels group, click Data Labels, and then click the option that you want. For additional data label options, click More Data Label Options, click Label Options if it's not selected, and then select the options that you want.

Is Chart JS any good?

Somehow, the creator of ChartJS got the default colors absolutely perfect and they go so well within any website. It's again another library that doesn't have a massive selection of charts but does it very well with the ones it does offer. Very easy to work with without the knowledge of JSON markup.

Does chart js use canvas?

Chart. js charts are rendered on user provided canvas elements. Thus, it is up to the user to create the canvas element in a way that is accessible. The canvas element has support in all browsers and will render on screen but the canvas content will not be accessible to screen readers.


1 Answers

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
        }
    }
}
like image 97
Zihad Ul Islam Avatar answered Sep 17 '22 08:09

Zihad Ul Islam