How to display the labels in doughnut chart using ng2 charts?
Asked Answered
B

2

6

I am using ng2 doughnut chart to display some items, Its all working fine as per the doughnut chart documentation but the thing i need to change is, the display of label of each slices comes only on hover but the thing i need is it should be in default in each slices.

Dashboard.component.html:

<canvas #mycanvas baseChart
    [data]="doughnutChartData"
    [labels]="doughnutChartLabels"
    [chartType]="doughnutChartType"
    [colors]="colors"
    [options]="options"
    (chartHover)="chartHovered($event)"
    (chartClick)="chartClicked($event)">
</canvas>

Dashboard.component.ts:

public doughnutChartLabels:string[] = ['Running', 'Stop', 'Idle'];
public doughnutChartData:number[] = [1, 4, 5];
public doughnutChartType:string = 'doughnut';
public colors: any[] = [{ backgroundColor: ["#008000", "#FF0000", "#FFA500"] }];

The thing i am in the need is exactly as like in the image,

enter image description here

The labels should be in default in each slices and the thing needs to be achieved only by using pure typescript and no javascript. Kindly help me to achieve the desired result.

Bedard answered 10/10, 2017 at 6:32 Comment(1)
Refer to the chart.js documentation Legend Config, you can use the input [options]="options" to pass the same options of the link aboveNeodarwinism
P
11

You can achieve that by using a Chart.js plugin called - Chart.PieceLabel.js.

first.. install it via npm :

npm install chart.piecelabel.js --save

then.. import it in your dashboard component :

import 'chart.piecelabel.js';

and finally.. set the following option in your chart­'s options config :

pieceLabel: {
   render: 'label'
}

note: this is the minimum option needs to be set to display the labels, and you can find more options here.

see - working example

Pittance answered 10/10, 2017 at 6:51 Comment(11)
Is there no option available without including any plugins?Bedard
Unfortunately.. NO ..not yetClamorous
Omg, including of plugins keeps on increasing the size of application.Bedard
Ok i will go with this plugin, is there any option to include the chartData value along with this label, Say, (March:1) like this??Bedard
Yes, there is. To learn more about options you should follow the docs.. anyway I've updated the example.. you can checkClamorous
Lol, will definetly accept it .. Thanks for your help.Bedard
No problem.. Glad to help.Clamorous
@GRUNT I tried you example but It does not display any labels. I am using chartjs 2.7.1. without ng2-charts. Any idea?Hithermost
@Pittance is it same for angular V4?Laterite
I followed the procedure, but it is not working in Angular V6 also. Please update the answer for Angular 6 alsoGorham
If label Text increases, its getting hid instead it should break to next line, is there a way to get around with this ?Pike
V
1

'chartjs-plugin-labels' is the most popular display plugin. I just tried it. It works!

npm install chartjs-plugin-labels --save

In the desired angular 2+ component, add the following:

import 'chartjs-plugin-labels';

No need to import any module.

Here is the npm information:
https://www.npmjs.com/package/chartjs-plugin-labels

Site:
https://chartjs-plugin-datalabels.netlify.com/

Demo:
https://emn178.github.io/chartjs-plugin-labels/samples/demo/

package.json : combination which worked:

"chart.js": "^2.8.0",
"chartjs-plugin-labels": "^1.1.0",
Vandalize answered 22/7, 2019 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.