I am having an issue where I am attempting to add multiple legend items to a chart control. I want the legends to align vertically down the left side of chart with the chart area on the right however I can only seem to get the legends side by side.
The nature of the chart means that there are categories with parts with each category, each part has a set of data which is plotted on a separate series. Different category might contain the same part but these are plotted as a different series. This is why there are multiple legend items each with a distinct title.
Is there a way where I can align the legends on top of each other or an alternative where I can group all series legend items into there distinct category where each item has a title in the legend?
The current code is as follows:
private Chart PointChartFromDataColumn(string name, DataTable tbl, string xid, string yid, List<string> catagories)
{
Chart c = null;
//Check the existance of the columns and that there are series to add
if (tbl.Columns.Contains(xid) && tbl.Columns.Contains(yid))
{
//Define new chart
c = new Chart();
//Set the name
c.Name = name;
//Clear chart areas, series and legends
c.ChartAreas.Clear();
c.Series.Clear();
c.Legends.Clear();
//Create the chart area
ChartArea area = new ChartArea();
area.Name = "default";
c.ChartAreas.Add(area);
//Create the custom legend for all plants
foreach (string s in catagories)
{
//Create the custom legend item and add
Legend lg = new Legend();
lg.Name = s;
lg.IsEquallySpacedItems = true;
lg.LegendStyle = LegendStyle.Column;
lg.Docking = Docking.Left;
lg.TitleAlignment = StringAlignment.Near;
lg.TableStyle = LegendTableStyle.Auto;
lg.Title = s;
lg.BackColor = Color.White;
lg.TitleFont = new Font("Verdana", 12, FontStyle.Italic);
lg.TitleSeparator = LegendSeparatorStyle.Line;
lg.TitleSeparatorColor = Color.Black;
lg.TitleForeColor = Color.DarkGray;
lg.BorderColor = Color.Black;
lg.BorderWidth = 1;
lg.BorderDashStyle = ChartDashStyle.Solid;
c.Legends.Add(lg);
}
string seriesname;
double dbl = 0;
DateTime time;
//For every row in the table
foreach (DataRow row in tbl.Rows)
{
//try and parse x and why values
if (double.TryParse(row[yid].ToString(), out dbl) && DateTime.TryParse(row[xid].ToString(), out time))
{
seriesname = row["Catagory"].ToString() + "-" + row["PartNo"].ToString();
//If the series does not exist then create it and add the legend item
if (c.Series.IndexOf(seriesname) == -1)
{
Series s = CreateSeries(seriesname, area.Name);
c.Series.Add(s);
c.Legends[row["Catagory"].ToString()].CustomItems.Add(s.Color, row["PartNo"].ToString());
}
//Add the point to the chart series
c.Series[seriesname].Points.AddXY(time, dbl);
}
}
}
return c;
}