News Category

How to Add Data Callout Labels to Charts in Excel in C#

2016-12-09 07:47:39 Written by  support iceblue
Rate this item
(0 votes)

Excel 2013 has provided some new charting features, for example, it enables users to set data callout labels which makes it easier to show the details about the data series or its individual data points in a clear and easy-to-read format. This article is going to introduce how to add data callout labels to a chart using Spire.XLS.

Step 1: Initialize a new instance of Workbook class and set the Excel version as 2013.

Workbook wb = new Workbook();
wb.Version = ExcelVersion.Version2013;

Step 2: Get the first sheet from workbook.

Worksheet ws = wb.Worksheets[0];

Step 3: Insert some data.

ws.Range["A2"].Text = "Product 1";
ws.Range["A3"].Text = "Product 2";
ws.Range["A4"].Text = "Product 3";
ws.Range["A5"].Text = "Product 4";
ws.Range["B1"].Text = "Sales";
ws.Range["B1"].Style.Font.IsBold = true;
ws.Range["B2"].NumberValue = 251;
ws.Range["B3"].NumberValue = 515;
ws.Range["B4"].NumberValue = 454;
ws.Range["B5"].NumberValue = 874;

Step 4: Create a Clustered Column Chart based on the data from range A1:B5.

Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = ws.Range["A1:B5"];
chart.SeriesDataFromRange = false;
chart.PrimaryValueAxis.HasMajorGridLines = false;

Step 5: Set the chart position.

chart.LeftColumn = 4;
chart.TopRow = 2;
chart.RightColumn = 12;
chart.BottomRow = 22;

Step 6: Set the HasWedgeCallout property as true to display callout labels in a chart.

foreach (ChartSerie cs in chart.Series)
{
    cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
    cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
}

Step 7: Save the file.

wb.SaveToFile("result.xlsx", FileFormat.Version2013);

Output:

How to Add Data Callout Labels to Charts in Excel in C#

Full Code:

using Spire.Xls;
using Spire.Xls.Charts;

namespace AddCalloutLabels
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.Version = ExcelVersion.Version2013;
            Worksheet ws = wb.Worksheets[0];

            ws.Range["A2"].Text = "Product 1";
            ws.Range["A3"].Text = "Product 2";
            ws.Range["A4"].Text = "Product 3";
            ws.Range["A5"].Text = "Product 4";
            ws.Range["B1"].Text = "Sales";
            ws.Range["B1"].Style.Font.IsBold = true;
            ws.Range["B2"].NumberValue = 251;
            ws.Range["B3"].NumberValue = 515;
            ws.Range["B4"].NumberValue = 454;
            ws.Range["B5"].NumberValue = 874;

            Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = ws.Range["A1:B5"];
            chart.SeriesDataFromRange = false;
            chart.PrimaryValueAxis.HasMajorGridLines = false;

            chart.LeftColumn = 4;
            chart.TopRow = 2;
            chart.RightColumn = 12;
            chart.BottomRow = 22;

            foreach (ChartSerie cs in chart.Series)
            {
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
                cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
            }
            wb.SaveToFile("result.xlsx", FileFormat.Version2013);
        }
    }
}

Additional Info

  • tutorial_title: Add Data Callout Labels to Charts in Excel in C#
Last modified on Monday, 06 September 2021 02:15