How to change caption of a chart?

When you have a chart with two series, how can I change the caption text of in the chart. By default it’s “series1” and “series2”?


This message was posted using Page2Forum from How to Create a Chart? - Aspose.Cells for .NET and Java

Hi,

Please use ASeries.Name to name a series. By default if you don't specify the names of the series, they will be Series1, Series2, Series3 etc.

May the following example code (related chart) help you for your need.

Workbook workbook = new Workbook();
//Set default font
Style style = workbook.DefaultStyle;
style.Font.Name = "Tahoma";
workbook.DefaultStyle = style;
Cells cells = workbook.Worksheets[0].Cells;
//Put a string into a cell
cells["A1"].PutValue("Region");
cells["A2"].PutValue("France");
cells["A3"].PutValue("Germany");
cells["A4"].PutValue("England");

cells["B1"].PutValue(2002);
cells["C1"].PutValue(2003);
cells["D1"].PutValue(2004);
cells["E1"].PutValue(2005);
cells["F1"].PutValue(2006);

cells["B2"].PutValue(40000);
cells["C2"].PutValue(45000);
cells["D2"].PutValue(50000);
cells["E2"].PutValue(55000);
cells["F2"].PutValue(70000);

cells["B3"].PutValue(10000);
cells["C3"].PutValue(25000);
cells["D3"].PutValue(40000);
cells["E3"].PutValue(55000);
cells["F3"].PutValue(80000);

cells["B4"].PutValue(65000);
cells["C4"].PutValue(50000);
cells["D4"].PutValue(35000);
cells["E4"].PutValue(30000);
cells["F4"].PutValue(20000);

Worksheet sheet = workbook.Worksheets[0];
//Set Borders around Data
Range range = sheet.Cells.CreateRange("A1","F4");
range.SetOutlineBorder(BorderType.TopBorder,CellBorderType.Thin,Color.Blue);
range.SetOutlineBorder(BorderType.LeftBorder,CellBorderType.Thin,Color.Blue);
range.SetOutlineBorder(BorderType.RightBorder,CellBorderType.Thin,Color.Blue);
range.SetOutlineBorder(BorderType.BottomBorder,CellBorderType.Thin,Color.Blue);
//Set the name of worksheet
sheet.Name = "Line";
sheet.IsGridlinesVisible = false;
//Create chart
int chartIndex = 0;
chartIndex = sheet.Charts.Add(ChartType.Line,5,1,29,10);
Chart chart = sheet.Charts[chartIndex];
chart.MajorGridLines.IsVisible = true;
chart.CategoryAxis.MajorGridLines.IsVisible = false;
chart.CategoryAxis.MinorGridLines.IsVisible = true;
chart.ValueAxis.MajorGridLines.IsVisible = true;
chart.ValueAxis.MinorGridLines.IsVisible = false;

//Set Properties of chart title
Title title = chart.Title;
title.Text = "Sales By Region For Years";
title.TextFont.Color = Color.Black;
title.TextFont.IsBold = true;
title.TextFont.Size = 12;
title.Rotation = 14;
title.TextHorizontalAlignment = TextAlignmentType.Center;
title.TextVerticalAlignment = TextAlignmentType.Center;
//Set Properties of nseries
NSeries nseries = chart.NSeries;
nseries.Add("B2:F4", false);
nseries.CategoryData = "B1:F1";
nseries.IsColorVaried = true;
cells = workbook.Worksheets[0].Cells;
for ( int i = 0 ;i < nseries.Count; i ++ )
{
nseries[i].Name = cells[i+1,0].Value.ToString();
nseries[i].MarkerStyle = ChartMarkerType.Circle;
nseries[i].MarkerBackgroundColor = Color.Teal;
nseries[i].MarkerForegroundColor = Color.Lime;
nseries[i].MarkerSize = 8;


}
//Set Properties of categoryaxis title
chart.CategoryAxis.Title.Text = "Year(2002-2006)";
chart.CategoryAxis.Title.TextFont.Color = Color.Black;
chart.CategoryAxis.Title.TextFont.IsBold = true;
chart.CategoryAxis.Title.TextFont.Size = 10;
//Set the legend position type.
chart.Legend.Position = LegendPositionType.Top;

//Save the excel file
workbook.Save("d:\\test\\myline_chart_out.xls");

Thank you.

It works. Many thanks.