0

Trying to display datalabels on the a pie chart using chartjs library in LWC. Tried both version pairs for chartjs and chartjs datalabel plugins libraries (V4.2.1- chartjs & V2.2.0 - chartjs-datalabel-plugin) & (v2.8-chartjs & v1.0-chartjs-datalabel-plugin) as mentioned in few other posts. But unable to get the datalabels displayed. Below is my code. Must be missing something basic.

testChart.html

<template>
    <div>
        <canvas></canvas>
    </div>
</template>

testChart.js

import { LightningElement } from 'lwc';
import chartjs from '@salesforce/resourceUrl/chartjs';
import { loadScript } from 'lightning/platformResourceLoader';
import ChartDataLabels from '@salesforce/resourceUrl/chartjsdatalabels';

export default class TestChart extends LightningElement {
    chartJsInitialized = false;

    renderedCallback() {
        if (this.chartJsInitialized) {
            return;
        }
        this.chartJsInitialized = true;

        Promise.all([
          loadScript(this, chartjs),loadScript(this, ChartDataLabels)
        ]).then(() => {
          console.log('ChartJS and ChartJS Datalabels scripts loaded successfully.');
            this.initializeChart();
            
        }).catch(error => {
            console.error(error);
        });
    }

    initializeChart() {
        const data = {
            labels: ['Label 1', 'Label 2', 'Label 3'],
            datasets: [{
                data: [30, 50, 20],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.6)',
                    'rgba(54, 162, 235, 0.6)',
                    'rgba(255, 206, 86, 0.6)'
                ],
                borderWidth: 1
            }]
        };
        const options = {
          responsive: false,
          plugins: {
            datalabels: {
              anchor: 'end',
              align: 'end',
              labels: {
                value: {
                  color: 'blue'
                }
              }
    
            }
          }
        };
        //chartjs.Chart.register(ChartDataLabels);
        const chart = new window.Chart(this.template.querySelector('canvas'), {
            type: 'pie',
            data: data,
            plugins: [ChartDataLabels],
            options: options
        });
    }
}

Please help in finding the solution

2
  • This may not answer your question directly, but when working with 3rd party libraries like chartjs in LWC, it may be helpful to enable the light dom feature. developer.salesforce.com/docs/component-library/documentation/… You may encounter some unexpected results when rendering content like chartjs within the shadow dom. Commented Apr 24, 2023 at 17:14
  • For what it's worth, your code works fine in standard vanilla js fiddle - it only needed a padding to show the "50" label. Still, when testing with issues it's better to use anchor: 'middle', to make sure labels are not cropped out, which is a possibility with labels positioned outside of the pie. Commented Apr 24, 2023 at 17:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.