Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data label alignment issue #305

Open
Karthika-08 opened this issue Jan 19, 2022 · 3 comments
Open

Data label alignment issue #305

Karthika-08 opened this issue Jan 19, 2022 · 3 comments

Comments

@Karthika-08
Copy link

Hi,
I have an alignment issue in the data label of the stacked bar chart. When toggling legends, data labels getting overlap with the bar and also have alignment issues in the case of negative values.
Screenshot 2022-01-19 at 14-53-41 “What If” Opportunity (TBA)

Here is my code:
plugins: {

  datalabels: {
   
    anchor:'end',
    align:  'top',
    color: '#737373',
    font: {
      weight: 'bold' 
    },
 offset: (value,ctx)=> {

      return '10'
   },
    formatter: (value, ctx) => {
    
      let datasets = ctx.chart.data.datasets.filter(
        (ds, datasetIndex) => ctx.chart.isDatasetVisible(datasetIndex)
      )
  
      if (ctx.datasetIndex === datasets.length - 1 ) {
       
        let sum = 0;
        datasets.map(dataset => {
          sum += dataset.data[ctx.dataIndex];
        });
        if(sum >0){
          return '$'+Math.round(sum);
        }else{
          return '-$'+Math.round(Math.abs(sum));
        }
        
      }
      else {
        return '';
      }
    }
    },
   
  }
},

Any help would be greatly appreciated!
Thanks!

@alinesouzadev
Copy link

Try to update this datalabels info to:
datalabels: { align: "start", anchor: "start"}

@Karthika-08
Copy link
Author

Try to update this datalabels info to: datalabels: { align: "start", anchor: "start"}
I have already tried it,but not working

@simonbrunel
Copy link
Member

@Karthika-08 If you still need support on your issue, please provide a CodePen (or similar) that reproduces your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants