Chartjs-plugin-datalabels: Padding and lineHeight has no effect

Created on 10 Oct 2019  路  7Comments  路  Source: chartjs/chartjs-plugin-datalabels

Hi!
Screenshot_2019-10-10

I am trying to vertically center the datalabels in my horizontal bar chart, so they are perfectly vertically aligned in the middle of the bar. As you can see they are a little bit more to the top of the bar.
To achieve this I tried to give some padding or a font.lineHeight, but both options have zero effect at all, as they were ignored.
I could use the offset with bottom alignment, but this is already used for the left/right positioning of the labels. So not many options are left. Why is the padding not working?

This is my configuration:

        datalabels: {
          font: {
            size: 10,
            weight: "bold",
          },
          clip: true,
          anchor: "start",
          align: "start",
          offset: ({ dataset, dataIndex }) => {
            const value = dataset.data[dataIndex];
            return value <= 10 ? 
               0 : 
               value % 1 ? -39 : -31;
          },
          formatter: value => localeUtils.toLocaleString(value, "de-DE", 1) + "%", 
        }

Tried it like that:

        datalabels: {
          font: {
            size: 10,
            weight: "bold",
            lineHeight: 2,
          },
...
       }

Or

        datalabels: {
          padding: { top: 20 }
...
       }

Nothing happens.

needs test case support

Most helpful comment

While I've created a jsfiddle, the thought occured to me, that this problem can be a part of a custom webfont. Is this possible? To solve this, the line height or padding parameter should be a option.

And here I got the same problem. Both parameter have no effect to the labels. Do you have an idea?

https://jsfiddle.net/bool/xapqvnh3/20/

All 7 comments

Hi @aldipower. padding should work, can you please create a codepen / jsfiddle that reproduces your issue with the latest version so we can debug what's going wrong.

Did this ever get resolved? Same issue with the latest version. Line height and padding do nothing.

While I've created a jsfiddle, the thought occured to me, that this problem can be a part of a custom webfont. Is this possible? To solve this, the line height or padding parameter should be a option.

And here I got the same problem. Both parameter have no effect to the labels. Do you have an idea?

https://jsfiddle.net/bool/xapqvnh3/20/

FYI My project was using a custom font: Open Sans

I'm having the same problem. No custom font. However, I noticed if you just supply a number to padding, it does have an affect. It appears to add right padding
padding: 20

I'm also using a custom font and ran into the same issue.
I've solved it by using the formatter and including an empty line before my value.
With that, the lineHeight property seems to be working and I've managed to center my text vertically

datalabels: {
    color: '#fff',
        anchor :'end',
        align :'start',
        font: {
        size: "18",
            family: "Hind Guntur,sans-serif",
            weight: "600",
            lineHeight: 0.6
    },
    padding: {
        right: 20
    },
    formatter: function(value) {
        return '\n' + value;
    }
}

Got some similar problem, the padding.bottom stopped to work suddently (I'm using Vue.js) fixed it with a :

 options: {
          plugins: {
            datalabels: {
              formatter: value => value+ "\n\n"
            },
          },
        },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

012attack picture 012attack  路  8Comments

sidv93 picture sidv93  路  3Comments

stockiNail picture stockiNail  路  12Comments

Laroosta picture Laroosta  路  8Comments

jedskie picture jedskie  路  6Comments