Move data labels outside of pie or donut chart - Chart.js
P粉356128676
P粉356128676 2023-11-23 15:07:41
0
1
832

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粉356128676
P粉356128676

reply all(1)
P粉762730205

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 = {
    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(ctx, {
    type: 'doughnut',
    data: {
      labels: [['Download', 'Sales'], ['In', 'Store', 'Sales'], 'Mail Sales'],
      datasets: [
        {
          data: [300, 500, 100],
        },
      ],
    },
    options: this.getPieChartOptions('doughnut'),
  } as ChartConfiguration).draw;
}

Demo @ StackBlitz

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!