Custom Legend Captions in Aspose Cells

Hi Team,

I received very good answer for custom bar colors. Thanks for your quick reply.

I need one more requirement, Please help me for the following scenario.

I need to show “Customized Legend Captions” instead of “chart.NSeries.CategoryData” values in the Legend.

I am using "Chartype.Column"

I am showing a chart for two categories(Gender & Salary).
In the Gender ==> Male, Female
In the Salary ==>10000, 20000, 30000, 40000, 50000,etc…

In the graph, Y axis I showed the Percentage of Gender and Salary.
In X axis I showed the Salary values.

Right now the graph shows different colors for each Gender(Male & Female).

In the Legend I want to show Male & Female with their respective colors instead of Category data in the X axis ( 10000, 20000, 30000, 40000,etc…)

Please advise me.


Hi,

Thank You for considering Aspose.

You can use Chart.NSeries[index].Name to assign the names to your nseries to be used as Legend titles. Please see the following sample code which will give you an idea of how you can assign the custom names to your nseries which are used in your legends,

Sample Code:


//Instantiating a Workbook object

Workbook workbook = new Workbook();

//Adding a new worksheet to the Excel 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("Male");

//Adding a sample value to "A2" cell

worksheet.Cells["A2"].PutValue(10000);

//Adding a sample value to "A3" cell

worksheet.Cells["A3"].PutValue(20000);

//Adding a sample value to "A4" cell

worksheet.Cells["A4"].PutValue(50000);

//Adding a sample value to "A5" cell

worksheet.Cells["A5"].PutValue(80000);

//Adding a sample value to "B1" cell

worksheet.Cells["B1"].PutValue("Female");

//Adding a sample value to "B2" cell

worksheet.Cells["B2"].PutValue(10000);

//Adding a sample value to "B3" cell

worksheet.Cells["B3"].PutValue(20000);

//Adding a sample value to "B4" cell

worksheet.Cells["B4"].PutValue(30000);

//Adding a sample value to "B5" cell

worksheet.Cells["B5"].PutValue(40000);

//Adding a chart to the worksheet

int chartIndex = worksheet.Charts.Add(Aspose.Cells.ChartType.Column, 5, 5, 15, 10);

//Accessing the instance of the newly added chart

Aspose.Cells.Chart chart = worksheet.Charts[chartIndex];

//Adding NSeries (chart data source) to the chart ranging from "A2" cell to "A5" and B2 ~ B5

chart.NSeries.Add("A2:A5",true);

chart.NSeries.Add("B2:B5", true);

//Set Series Names to be used in the Legend and there respective colors

chart.NSeries[0].Name = "Male";

chart.NSeries[0].Area.ForegroundColor = System.Drawing.Color.Blue;

chart.NSeries[1].Name = "Female";

chart.NSeries[1].Area.ForegroundColor = System.Drawing.Color.Red;

chart.Legend.Position = Aspose.Cells.LegendPositionType.Bottom;

//Saving the Excel file

workbook.Save("C:\\Chart.xls", FileFormatType.Default);

Thank You & Best Regards,