Pie Chart Gradient Color

Hi,

I am trying to set the gradient color for Pie chart. I am using below code. But it is not working for me.

ChartObj.NSeries[0].Points[0].Area.FillFormat.Type = Aspose.Cells.Drawing.FillType.Gradient;

ChartObj.NSeries[0].Points[0]..Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

Please let me know how can i do the same.

Thanks,

Amit

Hi Amit,

Thank you for using Aspose products.

I have tested your scenario with the latest assemblies of Aspose.Cells for .NET component and below provided source code. It worked fine for me. Please give a try to latest fix version of Aspose.Cells for .NET v7.5.3.4 on your end, and feed us back with your results.

C#

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];

//Adding a sample value to “A1” cell
worksheet.Cells[“A1”].PutValue(50);

//Adding a sample value to “A2” cell
worksheet.Cells[“A2”].PutValue(100);

//Adding a sample value to “A3” cell
worksheet.Cells[“A3”].PutValue(150);

//Adding a sample value to “B1” cell
worksheet.Cells[“B1”].PutValue(60);

//Adding a sample value to “B2” cell
worksheet.Cells[“B2”].PutValue(32);

//Adding a sample value to “B3” cell
worksheet.Cells[“B3”].PutValue(50);

//Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pie, 5, 0, 15, 5);

//Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

//Adding SeriesCollection (chart data source) to the chart ranging from “A1” cell to “B3”
chart.NSeries.Add(“A1:B3”, true);

//Filling the area of the 1st Points with a gradient
chart.NSeries[0].Points[0].Area.FillFormat.Type = Aspose.Cells.Drawing.FillType.Gradient;

//Filling the area of the 1st SeriesCollection with a gradient
chart.NSeries[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

//Save results

workbook.Save(myDir + “output.xlsx”);

Hi Amit,

I have tested your scenario/case with our latest version/fix : Aspose.Cells for .NET v7.5.3.4
it works fine. I have applied the gradient color the first point in the data series in the Pie chart, the color is applied to “Spain” slice in the Pie chart. Please see the following sample code and find attached the output Excel file for your reference:

Sample code:

Workbook workbook = new Workbook();
//Set default font
Style style = workbook.DefaultStyle;
style.Font.Name = “Tahoma”;
workbook.DefaultStyle = style;
Worksheet sheet = workbook.Worksheets[0];
//Set the name of worksheet
sheet.Name = “Data”;
Cells cells = workbook.Worksheets[0].Cells;
//Put a value into a cell
cells[“A1”].PutValue(“Region”);
cells[“A2”].PutValue(“France”);
cells[“A3”].PutValue(“Germany”);
cells[“A4”].PutValue(“England”);
cells[“A5”].PutValue(“Sweden”);
cells[“A6”].PutValue(“Italy”);
cells[“A7”].PutValue(“Spain”);
cells[“A8”].PutValue(“Portugal”);
cells[“B1”].PutValue(“Sale”);
cells[“B2”].PutValue(70000);
cells[“B3”].PutValue(55000);
cells[“B4”].PutValue(30000);
cells[“B5”].PutValue(40000);
cells[“B6”].PutValue(35000);
cells[“B7”].PutValue(32000);
cells[“B8”].PutValue(10000);

int sheetIndex = workbook.Worksheets.Add();
sheet = workbook.Worksheets[sheetIndex];
//Set the name of worksheet
sheet.Name = “Chart”;
//Create chart
int chartIndex = 0;
chartIndex = sheet.Charts.Add(ChartType.Pie, 1, 1, 25, 10);
Chart chart = sheet.Charts[chartIndex];
//Set properties of chart title
chart.Title.Text = “Sales By Region”;
chart.Title.TextFont.Color = Color.Blue;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
//Set properties of nseries
chart.NSeries.Add(“Data!B2:B8”, true);
chart.NSeries.CategoryData = “Data!A2:A8”;
chart.NSeries.IsColorVaried = true;

chart.NSeries[0].Points[0].Area.Formatting = FormattingType.Custom;
chart.NSeries[0].Points[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

//Set the ChartArea
ChartArea chartarea = chart.ChartArea;
chartarea.Area.Formatting = FormattingType.Custom;
chartarea.Area.FillFormat.Texture = TextureType.BlueTissuePaper;

//Set the Legend.
Legend legend = chart.Legend;
legend.Position = LegendPositionType.Left;
legend.Area.Formatting = FormattingType.Custom;
legend.Area.FillFormat.SetTwoColorGradient(Color.Yellow, Color.White, GradientStyleType.Horizontal, 1);

//Save the excel file
workbook.Save(“e:\test2\outpiegradient_chart1.xlsx”);


Let us know if you still have any issue.

Thank you.

Hi,

Thanks for your reply.

I saw your code but it looks like you are setting series background color as gradient in line.

//Filling the area of the 1st SeriesCollection with a gradient
chart.NSeries[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

But i wants to set the gradient color for point instead of series. Like Below.

//Filling the area of the 1st Points with a gradient
chart.NSeries[0].Points[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

I am using Pie chart.

Please let me know how can i do that.

Thanks,

Hi,

Thanks for your reply.

I saw your code but it looks like you are setting series background color as gradient in line.

//Filling the area of the 1st SeriesCollection with a gradient
chart.NSeries[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

But i wants to set the gradient color for point instead of series. Like Below.

//Filling the area of the 1st Points with a gradient
chart.NSeries[0].Points[0].Area.FillFormat.SetTwoColorGradient(Color.Red, Color.Blue, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);

I want to put gradient color for datapoint only.

Please let me know how can i do that.

Thanks,

Amit

Hi Amit,

Please see my reply in previous post in the thread, the sample code is written to accomplish your needs:
https://forum.aspose.com/t/87614

Thank you.