Free Support Forum -

Chart legends

how do i set values of legends in the charts generated in aspose cells.for example as in the below link
the legend comes as series1, series 2 by default. how do i set the labels to custom values


Well, by default (when you do not specify Series names), Excel will render series names as Series1, Series2 …and so on etc. I have updated the example code (in your mentioned document) to make you understand on how it works based on the data for series names, see the updated code below:

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 value to “A1” cell

//Adding a sample value to “A2” cell

//Adding a sample value to “A3” cell

//Adding a sample value to “A4” cell

//Adding a sample value to “B1” cell

//Adding a sample value to “B2” cell

//Adding a sample value to “B3” cell

//Adding a sample value to “B4” cell

//Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 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 “A2” cell to “B4”
chart.NSeries.Add(“A2:B4”, true);

//Set the Series names for A1:B1 data
SeriesCollection nseries = chart.NSeries;
for (int i = 0; i < nseries.Count; i++)
nseries[i].Name = worksheet.Cells[0, i].Value.ToString();

//Saving the Excel file

Also, we recommend you to see the sample demos on Charts for your complete reference:

Thank you.