How to set and format data labels for Excel charts in C#

There are articles in our tutorials that introduce how to add trendline, error bars and data tables to Excel charts in C# using Spire.XLS. It's worthy of mention that Spire.XLS also supports data labels which are widely used to quickly identify a data series in a chart. In label options, we could set whether label contains series name, category name, value, percentages (pie chart) and legend key. This article is going to introduce the method to set and format data labels for Excel charts in C# using Spire.XLS.

Note: before start, please download the latest version of Spire.XLS and add the .dll in the bin folder as the reference of Visual Studio.

Step 1: Create an Excel document and add sample data.

            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Name = "Demo";
            sheet.Range["A1"].Value = "Month";
            sheet.Range["A2"].Value = "Jan";
            sheet.Range["A3"].Value = "Feb";
            sheet.Range["A4"].Value = "Mar";
            sheet.Range["A5"].Value = "Apr";
            sheet.Range["A6"].Value = "May";
            sheet.Range["A7"].Value = "Jun";
            sheet.Range["B1"].Value = "Peter";
            sheet.Range["B2"].NumberValue = 25;
            sheet.Range["B3"].NumberValue = 18;
            sheet.Range["B4"].NumberValue = 8;
            sheet.Range["B5"].NumberValue = 13;
            sheet.Range["B6"].NumberValue = 22;
            sheet.Range["B7"].NumberValue = 28;

Step 2: Create a line markers chart based on the sample data.

            Chart chart = sheet.Charts.Add(ExcelChartType.LineMarkers);
            chart.DataRange = sheet.Range["B1:B7"];
            chart.PlotArea.Visible = false;
            chart.SeriesDataFromRange = false;
            chart.TopRow = 5;
            chart.BottomRow = 26;
            chart.LeftColumn = 2;
            chart.RightColumn =11;
            chart.ChartTitle = "Data Labels Demo";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];      
            cs1.CategoryLabels = sheet.Range["A2:A7"];

Step 3: Set which parts are displayed in the data labels and the delimiter to separate them.

            cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasLegendKey = false;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = false;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasSeriesName = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasCategoryName = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.Delimiter = ". ";

Step 4: Set the font, position and fill effects for data labels in the chart.

            cs1.DataPoints.DefaultDataPoint.DataLabels.Size = 9;
            cs1.DataPoints.DefaultDataPoint.DataLabels.Color = Color.Red;
            cs1.DataPoints.DefaultDataPoint.DataLabels.FontName = "Calibri";
            cs1.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;
            cs1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.Texture = GradientTextureType.Papyrus;

Step 5: Save the document as Excel 2010.

            workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("S3.xlsx");

Effects:

How to set and format data labels for Excel charts in C#

Full Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
using System.Drawing;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Name = "Demo";
            sheet.Range["A1"].Value = "Month";
            sheet.Range["A2"].Value = "Jan";
            sheet.Range["A3"].Value = "Feb";
            sheet.Range["A4"].Value = "Mar";
            sheet.Range["A5"].Value = "Apr";
            sheet.Range["A6"].Value = "May";
            sheet.Range["A7"].Value = "Jun";
            sheet.Range["B1"].Value = "Peter";
            sheet.Range["B2"].NumberValue = 25;
            sheet.Range["B3"].NumberValue = 18;
            sheet.Range["B4"].NumberValue = 8;
            sheet.Range["B5"].NumberValue = 13;
            sheet.Range["B6"].NumberValue = 22;
            sheet.Range["B7"].NumberValue = 28;
           
            Chart chart = sheet.Charts.Add(ExcelChartType.LineMarkers);
            chart.DataRange = sheet.Range["B1:B7"];
            chart.PlotArea.Visible = false;
            chart.SeriesDataFromRange = false;
            chart.TopRow = 5;
            chart.BottomRow = 26;
            chart.LeftColumn = 2;
            chart.RightColumn =11;
            chart.ChartTitle = "Data Labels Demo";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];      
            cs1.CategoryLabels = sheet.Range["A2:A7"];
          
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasLegendKey = false;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = false;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasSeriesName = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.HasCategoryName = true;
            cs1.DataPoints.DefaultDataPoint.DataLabels.Delimiter = ". ";

            cs1.DataPoints.DefaultDataPoint.DataLabels.Size = 9;
            cs1.DataPoints.DefaultDataPoint.DataLabels.Color = Color.Red;
            cs1.DataPoints.DefaultDataPoint.DataLabels.FontName = "Calibri";
            cs1.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;
            cs1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.Texture = GradientTextureType.Papyrus;
        
            workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("S3.xlsx");
        }
    }
}