We are using Charts in one of our application. Can anybody tell me how we can set the legend text in a chart?
I can even position the legend where I want, but the text that I see in the legend is the Category Data values.
I am actually showing some currency values for 3 years. And I want to show a particular name for the legend at the bottom of the chart, but I see the year values entered by me as an input.
Please help.
Hi,
Well, by default legend entries are based on how do you specify for the data series names. Please see the following sample code to accomplish the task for your needs. Please refer to the sample code and understand it to write your own code accordingly for your requirements.
e.g
Sample code:
Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
//Put a value into a cell
cells[“A1”].PutValue(“Region”);
cells[“A2”].PutValue(2002);
cells[“A3”].PutValue(2003);
cells[“A4”].PutValue(2004);
cells[“A5”].PutValue(2005);
cells[“A6”].PutValue(2006);
cells[“B1”].PutValue(“France”);
cells[“C1”].PutValue(“Germany”);
cells[“D1”].PutValue(“England”);
cells[“B2”].PutValue(4000);
cells[“B3”].PutValue(4500);
cells[“B4”].PutValue(5000);
cells[“B5”].PutValue(5500);
cells[“B6”].PutValue(7000);
cells[“C2”].PutValue(1000);
cells[“C3”].PutValue(2500);
cells[“C4”].PutValue(4000);
cells[“C5”].PutValue(5500);
cells[“C6”].PutValue(8000);
cells[“D2”].PutValue(6500);
cells[“D3”].PutValue(5000);
cells[“D4”].PutValue(3500);
cells[“D5”].PutValue(3000);
cells[“D6”].PutValue(2000);
//Get the first sheet (default)
Worksheet sheet = workbook.Worksheets[0];
//Set the name of worksheet
sheet.Name = “Line Chart”;
sheet.IsGridlinesVisible = false;
//Create chart
int chartIndex = sheet.Charts.Add(ChartType.Line, 6, 1, 29, 15);
Chart chart = sheet.Charts[chartIndex];
//Set properties of nseries
chart.NSeries.Add(“B2:D6”, true);
chart.NSeries.CategoryData = “A2:A6”;
chart.NSeries.IsColorVaried = true;
for (int i = 0; i < chart.NSeries.Count; i++)
{
//Set the name of nseries
chart.NSeries[i].Name = cells[0,i+1].Value.ToString();
}
//Set properties of chart title
chart.Title.Text = “Sales By Region”;
chart.Title.TextFont.Color = Color.Black;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
//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 tick labels.
TickLabels tickLabels = chart.ValueAxis.TickLabels;
tickLabels.RotationAngle = 10;
tickLabels.Font.Color = Color.Red;
tickLabels.NumberFormat = “$#,##0.00;$-#,##0.00”;
//Set the position of legend
chart.Legend.Position = LegendPositionType.Top;
//Set the attributes for a series line
Series aseries;
aseries = chart.NSeries[0];
Line line = aseries.Border;
line.DashType = MsoLineDashStyle.DashDot;
line.CapType = LineCapType.Square;
line.Color = Color.Green;
line.IsVisible = true;
line.Style = LineType.Solid;
line.Weight = WeightType.WideLine;
workbook.Save(“e:\test2\outline_testchart1.xlsx”);
Hope, this helps you a bit.
Thank you.