Customize Chart Data Labels
30 Aug 202412 minutes to read
Data Labels on a chart make it easier to understand. They show important information about the lines or points on the chart. Using DocIO, you can customize the data labels in the chart.
Enable Data Labels in Chart
The following code snippet illustrates how to visible the data label in chart.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Enable the datalabel in chart.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
//Enable the datalabel in chart.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
' Enable the datalabel in chart.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.IsValue = True
Customize the Data Labels
The following code snippet illustrates how to customize the data label in chart.
// Set the font size of the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Size = 8;
// Change the color of the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Red;
// Make the data labels bold.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Bold = true;
// Set the font name for the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri";
// Make the data labels italic.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Italic = true;
// Set the font size of the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Size = 8;
// Change the color of the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Red;
// Make the data labels bold.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Bold = true;
// Set the font name for the data labels.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri";
// Make the data labels italic.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Italic = true;
' Set the font size of the data labels.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.Size = 8
' Change the color of the data labels.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Red
' Make the data labels bold.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.Bold = True
' Set the font name for the data labels.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri"
' Make the data labels italic.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.Italic = True
Set the Position of Data Labels
The following code snippet illustrates how to set the position of the data label in chart.
// Set the position of data labels for the first series.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
// Set the position of data labels for the first series.
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
' Set the position of data labels for the first series.
chart.Series(0).DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center
The complete code snippet illustrating the above options is shown below.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing document from file system through constructor of WordDocument class.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Get the paragraph.
WParagraph paragraph = document.LastParagraph;
//Get the chart entity.
WChart chart = paragraph.ChildEntities[0] as WChart;
for (int i = 0; i < chart.Series.Count; i++)
{
//Enable the datalabel in chart.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
// Set the font size of the data labels.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Size = 10;
// Change the color of the data labels.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Black;
// Make the data labels bold.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Bold = true;
// Set the position of data labels for the first series.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
}
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//Save the Word file.
document.Save(outputStream, FormatType.Docx);
}
}
using (WordDocument document = new WordDocument("Template.docx"))
{
//Get the paragraph.
WParagraph paragraph = document.LastParagraph;
//Get the chart entity.
WChart chart = paragraph.ChildEntities[0] as WChart;
for (int i = 0; i < chart.Series.Count; i++)
{
//Enable the datalabel in chart.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
// Set the font size of the data labels.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Size = 10;
// Change the color of the data labels.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Black;
// Make the data labels bold.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Bold = true;
// Set the position of data labels for the first series.
chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
}
//Save the Word file.
document.Save("Sample.docx");
}
Using document As New WordDocument("Template.docx")
' Get the paragraph.
Dim paragraph As WParagraph = document.LastParagraph
' Get the chart entity.
Dim chart As WChart = TryCast(paragraph.ChildEntities(0), WChart)
For i As Integer = 0 To chart.Series.Count - 1
' Enable the datalabel in chart.
chart.Series(i).DataPoints.DefaultDataPoint.DataLabels.IsValue = True
' Set the font size of the data labels.
chart.Series(i).DataPoints.DefaultDataPoint.DataLabels.Size = 10
' Change the color of the data labels.
chart.Series(i).DataPoints.DefaultDataPoint.DataLabels.Color = OfficeKnownColors.Black
' Make the data labels bold.
chart.Series(i).DataPoints.DefaultDataPoint.DataLabels.Bold = True
' Set the position of data labels for the first series.
chart.Series(i).DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center
Next
' Save the Word file.
document.Save("Sample.docx")
End Using
You can download a complete working sample from GitHub.
Resize the Data Labels
The following code snippet illustrates how to resize the data label in chart.
//Manually resizing data label area using Layout.
chart.Series[0].DataPoints[0].DataLabels.Layout.Left = 3;
chart.Series[0].DataPoints[0].DataLabels.Layout.Top = 3;
//Manually resizing data label area using Manual Layout.
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Left = 3;
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Top = 3;
//Manually resizing data label area using Layout.
chart.Series[0].DataPoints[0].DataLabels.Layout.Left = 3;
chart.Series[0].DataPoints[0].DataLabels.Layout.Top = 3;
//Manually resizing data label area using Manual Layout.
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Left = 3;
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Top = 3;
' Manually resizing data label area using Layout.
chart.Series(0).DataPoints(0).DataLabels.Layout.Left = 3
chart.Series(0).DataPoints(0).DataLabels.Layout.Top = 3
' Manually resizing data label area using Manual Layout.
chart.Series(0).DataPoints(0).DataLabels.Layout.ManualLayout.Left = 3
chart.Series(0).DataPoints(0).DataLabels.Layout.ManualLayout.Top = 3