Skip to content

940650-Set pie slice color and pie data label call out in XlsIO #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pie Data Label Call Out", "Pie Data Label Call Out\Pie Data Label Call Out.csproj", "{EEE50E59-2AB6-4EFE-856D-C65C830F3B6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EEE50E59-2AB6-4EFE-856D-C65C830F3B6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEE50E59-2AB6-4EFE-856D-C65C830F3B6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEE50E59-2AB6-4EFE-856D-C65C830F3B6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEE50E59-2AB6-4EFE-856D-C65C830F3B6B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Pie_Data_Label_Call_Out</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.IO;
using Syncfusion.Drawing;
using Syncfusion.XlsIO;

namespace Pie_Data_Label_CallOuts
{
class Program
{
static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

//Assigning data to cells
worksheet.Range["A1"].Text = "Category";
worksheet.Range["B1"].Text = "Value";
worksheet.Range["A2"].Text = "Apples";
worksheet.Range["B2"].Number = 30;
worksheet.Range["A3"].Text = "Bananas";
worksheet.Range["B3"].Number = 45;
worksheet.Range["A4"].Text = "Cherries";
worksheet.Range["B4"].Number = 25;

//Add a pie chart to the worksheet
IChartShape chart = worksheet.Charts.Add();

//Set data range for the chart
chart.DataRange = worksheet.Range["A1:B4"];

//Specify chart type
chart.ChartType = ExcelChartType.Pie;

//Set chart properties
chart.IsSeriesInRows = false;
chart.ChartTitle = "Fruit Distribution";
chart.HasLegend = true;
chart.Legend.Position = ExcelLegendPosition.Right;

//Position the chart within the worksheet
chart.TopRow = 6;
chart.LeftColumn = 1;
chart.BottomRow = 20;
chart.RightColumn = 10;

//Customize data label for the first data point
IChartSerie series = chart.Series[0];
series.DataPoints[0].DataLabels.IsCategoryName = true;
series.DataPoints[0].DataLabels.IsValue = true;

//Enable data label callouts for the first data point
series.DataPoints[0].DataLabels.ShowLeaderLines = true;

//Manually resizing data label area using Manual Layout
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Left = 0.09;
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Top = 0.01;

#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose streams
outputStream.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# How to display data label callouts in pie charts using XlsIO?

Step 1: Create a New C# Console Application Project.

Step 2: Name the Project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.

```csharp
using System;
using System.IO;
using Syncfusion.Drawing;
using Syncfusion.XlsIO;
```

Step 5: Include the below code snippet in **Program.cs** to display data label callouts in pie charts.
```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

//Assigning data to cells
worksheet.Range["A1"].Text = "Category";
worksheet.Range["B1"].Text = "Value";
worksheet.Range["A2"].Text = "Apples";
worksheet.Range["B2"].Number = 30;
worksheet.Range["A3"].Text = "Bananas";
worksheet.Range["B3"].Number = 45;
worksheet.Range["A4"].Text = "Cherries";
worksheet.Range["B4"].Number = 25;

//Add a pie chart to the worksheet
IChartShape chart = worksheet.Charts.Add();

//Set data range for the chart
chart.DataRange = worksheet.Range["A1:B4"];

//Specify chart type
chart.ChartType = ExcelChartType.Pie;

//Set chart properties
chart.IsSeriesInRows = false;
chart.ChartTitle = "Fruit Distribution";
chart.HasLegend = true;
chart.Legend.Position = ExcelLegendPosition.Right;

//Position the chart within the worksheet
chart.TopRow = 6;
chart.LeftColumn = 1;
chart.BottomRow = 20;
chart.RightColumn = 10;

//Customize data label for the first data point
IChartSerie series = chart.Series[0];
series.DataPoints[0].DataLabels.IsCategoryName = true;
series.DataPoints[0].DataLabels.IsValue = true;

//Enable data label callouts for the first data point
series.DataPoints[0].DataLabels.ShowLeaderLines = true;

//Manually resizing data label area using Manual Layout
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Left = 0.09;
chart.Series[0].DataPoints[0].DataLabels.Layout.ManualLayout.Top = 0.01;

#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose streams
outputStream.Dispose();
}
```