Free Support Forum - aspose.com

Chart legends

how do i set values of legends in the charts generated in aspose cells.for example as in the below link http://www.aspose.com/docs/display/cellsnet/How+to+Create+a+Chart
the legend comes as series1, series 2 by default. how do i set the labels to custom values


Hi,


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
worksheet.Cells[“A1”].PutValue(“Product1”);

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

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

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

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

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

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

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

//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
workbook.Save(“e:\test2\Book1.xls”);


Also, we recommend you to see the sample demos on Charts for your complete reference:
http://www.aspose.com/demos/.net-components/aspose.cells/csharp/chart-types/default.aspx


Thank you.