Thanks, John now is working as expected.
Hi Team,
We have tried creating pie charts using Aspose.Cells, but the output does not meet our expectations. Specifically, shadows are appearing in the pie chart display, which differs from our intended result.
Could you please assist us in resolving this issue?
Thank you for your support.
static void Main()
{
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Pie Chart";
string[] categories = { "Auditory Digital", "Auditory", "Kinaesthetic", "Visual" };
int[] percentages = { 45, 18, 18, 18 };
worksheet.Cells["A1"].Value = "Category";
worksheet.Cells["B1"].Value = "Percentage";
for (int i = 0; i < categories.Length; i++)
{
worksheet.Cells[$"A{i + 2}"].Value = categories[i];
worksheet.Cells[$"B{i + 2}"].Value = percentages[i];
}
int rowCount = categories.Length;
string dataRange = $"B2:B{rowCount + 1}";
string categoryRange = $"A2:A{rowCount + 1}";
int chartIndex = worksheet.Charts.Add(ChartType.Pie, 5, 1, 25, 8);
Chart chart = worksheet.Charts[chartIndex];
chart.NSeries.Add(dataRange, true);
chart.NSeries.CategoryData = categoryRange;
//chart.ChartArea.Border.IsVisible = false;
for (int i = 0; i < chart.NSeries.Count; i++)
{
chart.NSeries[i].Explosion = 1; // Exploded pie chart
chart.NSeries[i].Has3DEffect = true;
}
//chart.NSeries[0].DataLabels.ShowValue = true;
chart.NSeries[0].DataLabels.ShowPercentage = true;
DataLabels dataLabels = chart.NSeries[0].DataLabels;
dataLabels.ShowCategoryName = true;
dataLabels.ShowPercentage = true;
dataLabels.SeparatorValue = ",\n ";
dataLabels.Position = LabelPositionType.OutsideEnd;
dataLabels.Font.Color = System.Drawing.ColorTranslator.FromHtml("#5f79a7");
chart.ShowLegend = false;
string[] sliceColors = { "#e9f0ff", "#c0d3f3", "#9cb5ed", "#375ba0" };
for (int i = 0; i < chart.NSeries[0].Points.Count; i++)
{
Console.WriteLine(chart.NSeries[0].Points.Count);
chart.NSeries[0].Points[i].Area.ForegroundColor = System.Drawing.ColorTranslator.FromHtml(sliceColors[i]);
}
//chart.PlotArea.Width = 400; // Adjust plot area width to make the chart circular
//chart.PlotArea.Height = 400;
//chart.DepthPercent = 20; // Set the depth of the 3D effect
//chart.Perspective = 30; // Set perspective for a realistic 3D look
//ChartArea chartArea = chart.ChartArea;
//chartArea.Area.Formatting = FormattingType.Custom;
//chartArea.Area.FillFormat.Texture = TextureType.BlueTissuePaper;
chart.PlotArea.Area.BackgroundColor = Color.White;
chart.FirstSliceAngle = 180;
// Save the workbook
string filePath = @"D:\PieChartOutput.xlsx";
workbook.Save(filePath);
System.Console.WriteLine($"Pie chart saved to {filePath}");
PieChartOutput.zip (13.9 KB)
We have attempted to create Doughnut charts using Aspose.Cells, but the output does not align with our expectations. Specifically, we are encountering an issue where shadows are appearing in the Doughnut chart display, which differs from our intended result.
Could you please guide us on resolving this issue or share any relevant code adjustments to achieve the expected output?
Thank you for your support.
Workbook workbook = new Workbook();
// Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = “Pie Chart 2”;
// Define the data for the chart
string[] categories = { “Expressed”, “Required” };
int[] values = { 177, 270 };
// Add headers
worksheet.Cells[“A1”].Value = “Category”;
worksheet.Cells[“B1”].Value = “Value”;
// Populate data dynamically
for (int i = 0; i < categories.Length; i++)
{
worksheet.Cells[$“A{i + 2}”].Value = categories[i];
worksheet.Cells[$“B{i + 2}”].Value = values[i];
}
int rowCount = categories.Length;
string dataRange = $“B2:B{rowCount + 1}”;
string categoryRange = $“A2:A{rowCount + 1}”;
// Add a doughnut chart
int chartIndex = worksheet.Charts.Add(ChartType.Doughnut, 5, 1, 20, 5);
Chart chart = worksheet.Charts[chartIndex];
int seriesIndex = chart.NSeries.Add(dataRange, true);
chart.NSeries.CategoryData = categoryRange;
Series series = chart.NSeries[seriesIndex];
for (int i = 0; i < chart.NSeries.Count; i++)
{
chart.NSeries[i].Explosion = 1; // Exploded pie chart
chart.NSeries[i].Has3DEffect = true;
}
//series.Type = ChartType.Line;
// Set the data source for the chart
//chart.NSeries[chartIndex].Area.Formatting = FormattingType.None;
series.DoughnutHoleSize = 10;
// Configure data labels to display inside the chart
DataLabels dataLabels = chart.NSeries[0].DataLabels;
dataLabels.ShowCategoryName = true; // Show category names
dataLabels.ShowValue = true; // Show values
dataLabels.SeparatorValue = “,\n “; // Separator between category and value
dataLabels.Position = LabelPositionType.Center; // Center the labels
chart.ShowLegend = false;
dataLabels.Font.Color = System.Drawing.ColorTranslator.FromHtml(”#000”);
// Set the doughnut hole size to create the circular effect
//chart.d = 60; // Adjust this value for the desired hole size
// Add a total percentage in the center
chart.NSeries[0].DataLabels.ShowPercentage = false; // Show percentage in the center
// Customize the chart appearance
string[] sliceColors = { “#4b7bd0”, “#e8efff” };
for (int i = 0; i < values.Length; i++)
{
chart.NSeries[0].Points[i].Area.ForegroundColor = System.Drawing.ColorTranslator.FromHtml(sliceColors[i]);
}
// Rotate the chart for better alignment
chart.FirstSliceAngle = 10;
Aspose.Cells.Drawing.TextBox textBox = chart.Shapes.AddTextBoxInChart(1600, 1500, 800, 1200);
textBox.Text = “66%”;
textBox.TextHorizontalAlignment = TextAlignmentType.Center;
textBox.TextVerticalAlignment = TextAlignmentType.Center;
// Customize font
textBox.Font.Size = 16;
textBox.Font.Color = System.Drawing.ColorTranslator.FromHtml(“#2f5597”);
textBox.LineFormat.IsVisible = false;
textBox.Font.IsBold = true;
// Save the workbook
string filePath = @“D:\DoughnutChart.xlsx”;
workbook.Save(filePath);
DoughnutChart.zip (14.2 KB)
Please change the line of code:
chart.PlotArea.Area.BackgroundColor = Color.White;
to:
chart.PlotArea.Area.ForegroundColo = Color.White;
it will work Ok.
You may try to set shading color of the chart’s plot area to white and hide the border line of plot area. You may add the following two lines to your code segment.
chart.PlotArea.Area.ForegroundColor = System.Drawing.Color.White;
chart.PlotArea.Border.IsVisible = false;
Thanks for your update but the output does not align with our expectations. Please help us resolve this issue or share any relevant code adjustments to achieve the expected output?
DoughnutChartnew.zip (14.9 KB)
Thanks for the sample Excel file containing the desired chart.
I have written the sample code to accomplish your task. Please refer to it and write/update your code accordingly by yourselves for your custom needs. The code also exhibits on how to apply 3D formattings to the chart series. I have also attached the output Excel file containing the chart for your reference.
e.g.,
Sample code:
// Create a new workbook
Workbook workbook = new Workbook();
// Add sample data to the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].PutValue("Category");
worksheet.Cells["A2"].PutValue("Required");
worksheet.Cells["A3"].PutValue("Expressed");
worksheet.Cells["B1"].PutValue("Values");
worksheet.Cells["B2"].PutValue(270);
worksheet.Cells["B3"].PutValue(177);
// Add a chart to the worksheet
int chartIndex = worksheet.Charts.Add(ChartType.Doughnut, 5, 1, 23, 7);
Chart chart = worksheet.Charts[chartIndex];
// Set chart title
chart.Title.Text = "Sample Doughnut Chart";
chart.Title.Font.IsBold = true;
chart.Title.Font.Size = 12;
// Add the data series
chart.NSeries.Add("B2:B3", true);
chart.NSeries.CategoryData = "A2:A3";
// Configure data labels to display inside the chart
DataLabels dataLabels = chart.NSeries[0].DataLabels;
dataLabels.ShowCategoryName = true; // Show category names
dataLabels.ShowValue = true; // Show values
dataLabels.SeparatorValue = ",\n "; // Separator between category and value
dataLabels.Position = LabelPositionType.Center; // Center the labels
chart.ShowLegend = false;
dataLabels.Font.Color = System.Drawing.ColorTranslator.FromHtml("#000");
// Get the series
Aspose.Cells.Charts.Series series = chart.NSeries[0];
// Set some properties
series.Explosion = 0;
series.Has3DEffect = true;
series.DoughnutHoleSize = 39;
// Set font color of the data lables in the slices of the doughnut chart
series.DataLabels.Font.Color= System.Drawing.Color.White;
series.DataLabels.Font.IsBold = true;
// Apply the 3-D formatting to the chart series
ShapePropertyCollection spPr = series.ShapeProperties;
Format3D fmt3d = spPr.Format3D;
// Specify Bevel with its height/width
Bevel bevel = fmt3d.TopBevel;
bevel.Type = Aspose.Cells.Drawing.BevelPresetType.Circle;
bevel.Height = 4;
bevel.Width = 4;
// Specify Surface material type
fmt3d.SurfaceMaterialType = PresetMaterialType.WarmMatte;
// Specify surface lighting type
fmt3d.SurfaceLightingType = LightRigType.ThreePoint;
//Apply white shading color to plot area
chart.PlotArea.Area.ForegroundColor = System.Drawing.Color.White;
chart.PlotArea.Border.IsVisible = false;
// Save the workbook
workbook.Save("e:\\test2\\DoughnutChart1.xlsx");
DoughnutChart1.zip (7.9 KB)
Hope, this helps a bit.