Home  >  Q&A  >  body text

Move data labels outside of pie or donut chart - Chart.js

I'm developing a large application and it would be very helpful to place labels on a pie or donut chart outside of the chart itself.

This plugin, outerLabels is exactly what I'm looking for, but I can't get it to output anything.

I haven't been able to find a way to accomplish this using Chart.js, so this is what I'm stuck with now.

I have imported the plugin Import{} from 'chartjs-plugin-outerlabels'; Also like import 'chartjs-plugin-outerlabels';

Here is an example of how I set the chart options:

function getPieChartOptions(chartTitle: string) {
    const options: ChartOptions = {
        responsive: true,
        plugins: {
            outerLabels: {
                fontNormalSize: 12,
                fontNormalFamily: 'sans-serif',
            },
            legend: {
                display: true,
                labels: {
                    usePointStyle: true,
                    font: {
                        family: 'sans-serif',
                        size: 12,
                    }
                }
            },
            title: {
                display: true,
                text: chartTitle,
                color: 'black',
                padding: 5,
                font: {
                    size: 16,
                    family: 'sans-serif',
                }
            },
            datalabels: {
                color: 'white',
                formatter: commas.format,
            }
        },
    };
    return options;
}

The following is an example of a donut chart within the project itself:

If anyone can help get this plugin working, or has another solution to this problem, it would be greatly appreciated!

P粉356128676P粉356128676329 days ago932

reply all(1)I'll reply

  • P粉762730205

    P粉7627302052023-11-24 13:22:43

    This worked for me without the datalabels field (as it requires the Chartjs-plugin-datalabels.js library to be installed).

    So basically the ChartOptions settings are similar to the ones you provided.

    import 'chartjs-plugin-outerlabels';
    
    getPieChartOptions(chartTitle: string) {
      const options: ChartOptions<'doughnut'> = {
        responsive: true,
        plugins: {
          outerLabels: {
            fontNormalSize: 12,
            fontNormalFamily: 'sans-serif',
          },
          legend: {
            display: true,
            labels: {
              usePointStyle: true,
              font: {
                family: 'sans-serif',
                size: 12,
              },
            },
          },
          title: {
            display: true,
            text: chartTitle,
            color: 'black',
            padding: 5,
            font: {
              size: 16,
              family: 'sans-serif',
            },
          },
          tooltip: {
            enabled: false,
          },
        },
      };
      return options;
    }
    

    This is how to render a chart to a canvas element.

    @ViewChild('MyChart', { static: true }) chart: ElementRef;
    
    ngAfterViewInit() {
      const ctx = this.chart.nativeElement.getContext('2d');
      new Chart<'doughnut'>(ctx, {
        type: 'doughnut',
        data: {
          labels: [['Download', 'Sales'], ['In', 'Store', 'Sales'], 'Mail Sales'],
          datasets: [
            {
              data: [300, 500, 100],
            },
          ],
        },
        options: this.getPieChartOptions('doughnut'),
      } as ChartConfiguration<'doughnut'>).draw;
    }
    

    Demo @ StackBlitz

    reply
    0
  • Cancelreply