Data label in Angular Accumulation chart component
27 Apr 202424 minutes to read
Data label can be added to a chart series by enabling the visible option in the dataLabel property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
ngOnInit(): void {
this.datalabel = { visible: true };
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Note: To use the data label feature, inject the
DataLabelinto the@NgModule.providers.
Positioning
Accumulation chart provides support for placing the data label either inside or outside the chart.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
ngOnInit(): void {
this.datalabel = { visible: true, position: 'Outside' };
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));DataLabel rotation
Using angle property, you can rotate the data label by its given angle.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
ngOnInit(): void {
this.datalabel = { visible: true, angle: 90, enableRotation: true },
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Note: when the
enableRotationis true, the data label is rotated along the slice.
Smart labels
Data labels will be arranged smartly without overlapping with each other. You can enable or disable this feature using the enableSmartLabels property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" [enableSmartLabels]='enableSmartLabels'>
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
public enableSmartLabels?: boolean;
ngOnInit(): void {
this.datalabel = { visible: true, name: 'text', position: 'Outside' };
this.enableSmartLabels = true;
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Format
Data label for the accumulation chart can be formatted using format property. You can use the global formatting options, such as ‘n’, ‘p’, and ‘c’.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" [enableSmartLabels]='enableSmartLabels'>
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
public enableSmartLabels?: boolean;
ngOnInit(): void {
this.datalabel = { visible: true, format: 'n2' };
this.enableSmartLabels = true;
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));| Value | Format | Resultant Value | Description |
|---|---|---|---|
| 1000 | n1 | 1000.0 | The number is rounded to 1 decimal place. |
| 1000 | n2 | 1000.00 | The number is rounded to 2 decimal places. |
| 1000 | n3 | 1000.000 | The number is rounded to 3 decimal place. |
| 0.01 | p1 | 1.0% | The number is converted to percentage with 1 decimal place. |
| 0.01 | p2 | 1.00% | The number is converted to percentage with 2 decimal place. |
| 0.01 | p3 | 1.000% | The number is converted to percentage with 3 decimal place. |
| 1000 | c1 | $1000.0 | The currency symbol is appended to number and number is rounded to 1 decimal place. |
| 1000 | c2 | $1000.00 | The currency symbol is appended to number and number is rounded to 2 decimal place. |
DataLabel template
Label content can be formatted by using the template option. Inside the template, you can add the placeholder text ${point.x} and ${point.y} to display corresponding data points x & y value. Using
templateproperty, you can set data label template in chart.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" [enableSmartLabels]='enableSmartLabels'>
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
public enableSmartLabels?: boolean;
ngOnInit(): void {
this.datalabel = { visible: true, name: 'text', position: 'Outside', template: '<div>${point.x}</div><div>${point.y}</div>' };
this.enableSmartLabels = true;
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Connector Line
Connector line will be visible when the data label is placed outside the chart.
The connector line can be customized using the type, color, width, length and dashArray properties
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" [enableSmartLabels]='enableSmartLabels'>
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
public enableSmartLabels?: boolean;
ngOnInit(): void {
this.datalabel = { visible: true, name: 'text', position: 'Outside',
connectorStyle:{
//Length of the connector line in pixels
length: '50px',
//Width of the connector line in pixels
width: 2,
//dashArray of the connector line
dashArray: '5,3',
//Color of the connector line
color: '#f4429e',
//Specifies the type of the connector line either Line or Curve
type: 'Curve'
} };
this.enableSmartLabels = true;
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Text Mapping
The fill color and the text in the data source can be mapped to the chart using pointColorMapping in series and name in data label respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationLegendService, AccumulationTooltipService, AccumulationAnnotationService,
AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { dataMapping } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationLegendService, AccumulationTooltipService, AccumulationDataLabelService,
AccumulationAnnotationService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
ngOnInit(): void {
this.piedata = dataMapping;
this.datalabel = { visible: true, name: 'text', position: 'Outside' };
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Customization
Individual text can be customized using the textRender event.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
import { IAccTextRenderEventArgs } from '@syncfusion/ej2-charts';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" (textRender)="onTextRender($event)">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
public onTextRender(args: IAccTextRenderEventArgs): void {
if (args.text === '13.5') {
args.color = 'red';
args.border.width = 1;
}
}
ngOnInit(): void {
this.datalabel = { visible: true };
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Text wrap
When the data label text exceeds the container, the text can be wrapped by using textWrap property. End user can also wrap the data label text based on maxWidth property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' startAngle="270" endAngle="90" innerRadius="40%" radius="100%" xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata: Object[] = [];
public datalabel?: Object;
ngOnInit(): void {
this.datalabel = { visible: true, position: 'Inside' ,maxWidth:100 ,textWrap:'Wrap',name: 'text', enableRotation:true };
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Show percentages in data labels of pie chart
You can show the percentages in data labels of pie chart using textRender event and template option.
Using textRender event
You can customize the data label of pie chart using textRender event as follows to show percentage.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
import { IAccTextRenderEventArgs } from '@syncfusion/ej2-angular-charts';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container" (textRender)="onTextRender($event)">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
onTextRender: Function | any;
ngOnInit(): void {
this.datalabel = { visible: true };
this.piedata = labelData;
this.onTextRender = (args: IAccTextRenderEventArgs) => {
args.text = args.point.percentage + "%";
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Using template
You can display the percentage values in data label of pie chart using template option
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccumulationChartModule } from '@syncfusion/ej2-angular-charts'
import { PieSeriesService, AccumulationDataLabelService } from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { labelData } from './datasource';
@Component({
imports: [
AccumulationChartModule
],
providers: [PieSeriesService, AccumulationDataLabelService],
standalone: true,
selector: 'app-container',
template: `<ejs-accumulationchart id="chart-container">
<e-accumulation-series-collection>
<e-accumulation-series [dataSource]='piedata' xName='x' yName='y' [dataLabel]='datalabel'></e-accumulation-series>
</e-accumulation-series-collection>
</ejs-accumulationchart>`
})
export class AppComponent implements OnInit {
public piedata?: Object[];
public datalabel?: Object;
ngOnInit(): void {
this.datalabel = { visible: true, template: "<div id='dataLabelTemplate'>${point.percentage}%</div>" };
this.piedata = labelData;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));