1
votes

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;
    }
1

1 Answers

1
votes

Managed to resolve the issue (see code below) by redrawing the entire chart area based on the known max size of legend item. I also set the legend item as

lg.LegendStyle = LegendStyle.Column;
lg.Docking = Docking.Top;

This means the docking function stacks them vertically. This function is called every time a chart resizes and will accordingly be re painted.

    /// <summary>
    /// Re-draw chart area
    /// </summary>
    /// <param name="c">Size of the chart</param>
    /// <param name="index">Index of chart area</param>
    private void ReDrawChartArea(Chart c, int index)
    {       
        //Set known max legend size (can't calculate programmatically as charts are stupid)
        float legendmaxsize = 140;

        //Calculate the percentage of the total size
        float maxpercentage = (legendmaxsize / (float)c.Width) * 100;

        //Get the chart area
        ChartArea area = c.ChartAreas[index];

        //Disable auto size
        area.Position.Auto = false;

        //Set the height as full and width as the remaining percentage
        area.Position.Height = 100;
        area.Position.Width = 100F - maxpercentage;

        //Set the area as top and start position as end of legend
        area.Position.Y = 0;
        area.Position.X = maxpercentage;        
    }