Chartjs-plugin-datalabels: dynamic anchor/alignment/offset support

Created on 3 Mar 2018  路  2Comments  路  Source: chartjs/chartjs-plugin-datalabels

Hi,

Is there any way to make the anchor/alignment/offset dynamic but fixed? Like make it a callback or something else?

I am setting up the position of labels in the chart but for some cases the position of labels is not perfect, like the 11% in the pic
screen shot 2018-03-03 at 4 18 06 pm

resolved support

Most helpful comment

You can use scriptable options, for example (jsfiddle):

options: {
  plugins: {
    datalabels: {
      align: function(context) {
        return context.dataset.data[context.dataIndex] < 15 ? 'end' : 'start';
      },
      color: function(context) {
        return context.dataset.data[context.dataIndex] < 15 ? 'black' : 'white';
      }
      // ...
    }
  }
}

All 2 comments

You can use scriptable options, for example (jsfiddle):

options: {
  plugins: {
    datalabels: {
      align: function(context) {
        return context.dataset.data[context.dataIndex] < 15 ? 'end' : 'start';
      },
      color: function(context) {
        return context.dataset.data[context.dataIndex] < 15 ? 'black' : 'white';
      }
      // ...
    }
  }
}

Thanks! It worked

I tried this way, maybe I missed a comma or other mistakes at that time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ed1nh0 picture ed1nh0  路  10Comments

012attack picture 012attack  路  8Comments

Lenophie picture Lenophie  路  14Comments

timdiggins picture timdiggins  路  8Comments

Laroosta picture Laroosta  路  8Comments