使用 Microsoft Chart Controls Part 4 - 指定 Series 的 Legend 到正確的 Chart Legends

摘要:使用 Microsoft Chart Controls Part 4 - 指定 Series 的 Legend 到正確的 Chart Legends

之前的專案中,會遇到一些 Chart,這一陣子因為需求的關係,必須將 Chart 動態的產生,那每一個 Chart 都會需要一個屬於自己的 Legend,倘若沒指定,那麼 Legend 就會被集中到頭或是尾嚕。如圖:



那要如何才能讓 Legend 到正確的 Chart 中呢!?

Code:

MyDataContext db = new MyDataContext();

public class newItem
{
     public string dt { get; set; }
     public int total { get; set; }
}

public ActionResult Index()
{
     Chart c = GetChart();
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     c.SaveImage(ms, ChartImageFormat.Png);
     ms.Seek(0, System.IO.SeekOrigin.Begin);

     return File(ms, "image/Png");
}

private Chart GetChart()
{
     Chart c = new Chart();

     for (int i = 0; i < 3; i++)
     {
          c.ID = "Chart" + i.ToString();
          c.Height = System.Web.UI.WebControls.Unit.Pixel(600);
          c.Width = System.Web.UI.WebControls.Unit.Pixel(800);
          c.Palette = ChartColorPalette.BrightPastel;
          c.ImageType = ChartImageType.Png;
          c.BorderlineDashStyle = ChartDashStyle.Solid;
          c.BackSecondaryColor = System.Drawing.Color.White;
          c.BackGradientStyle = GradientStyle.TopBottom;
          c.BorderlineWidth = 2;
          c.BackColor = System.Drawing.ColorTranslator.FromHtml("#D3DFF0");

          c.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;

          ChartArea ca = new ChartArea();
          ca.Name = "ChartArea" + i.ToString();
          ca.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
          ca.BorderDashStyle = ChartDashStyle.Solid;
          ca.BackSecondaryColor = System.Drawing.Color.White;
          ca.BackColor = System.Drawing.Color.FromArgb(64, 165, 191, 228);
          ca.ShadowColor = System.Drawing.Color.Transparent;
          ca.BackGradientStyle = GradientStyle.TopBottom;
          ca.Area3DStyle.Rotation = 10;
          ca.Area3DStyle.Perspective = 10;
          ca.Area3DStyle.Inclination = 15;
          ca.Area3DStyle.IsRightAngleAxes = false;
          ca.Area3DStyle.WallWidth = 0;
          ca.Area3DStyle.IsClustered = false;
          ca.AxisY.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
          ca.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25f, System.Drawing.FontStyle.Bold);
          ca.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
          ca.AxisX.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
          ca.AxisX.LabelStyle.Font = new System .Drawing.Font("Trebuchet MS", 8.25f, System.Drawing.FontStyle.Bold);
          ca.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
          c.ChartAreas.Add(ca);

          Legend cl = new Legend();
          cl.Name = "Default" + i.ToString();
          cl.IsTextAutoFit = true;
          cl.BackColor = System.Drawing.Color.Transparent;
          cl.Font = new System.Drawing.Font("Trebuchet MS", 8.25f, System.Drawing.FontStyle.Bold);
          cl.IsDockedInsideChartArea = false;
          cl.DockedToChartArea = "ChartArea" + i.ToString();
          c.Legends.Add(cl);

          //讓顯示的X,不會跳著顯示
          c.ChartAreas["ChartArea" + i.ToString()].AxisX.Interval = 1;

          Series s = new Series();
          s.Name = "Series " + i.ToString();
          c.Series.Add("Series " + i.ToString());

          var result = from e in db.Employees
                           group e by e.TitleOfCourtesy into g
                           orderby g.Key ascending
                           select new newItem
                           {
                               dt = g.Key,
                               total = g.Count()
                           };

          c.Series["Series " + i.ToString()].Points.DataBind(result, "dt", "total", null);

          c.Series["Series " + i.ToString()].ChartArea = "ChartArea" + i.ToString();

          //這行就是定義,哪一個 Series 的 Legend 應該顯示在哪一個 Chart 的 Legends
          c.Series["Series " + i.ToString()].Legend = "Default" + i.ToString();

          //將說明在下方顯示並至中
          c.Legends["Default" + i.ToString()].Docking = Docking.Bottom;
          c.Legends["Default" + i.ToString()].Alignment = System.Drawing.StringAlignment.Center;

          c.Series["Series " + i.ToString()]["PointWidth"] = "0.6";
     }

     return c;

結果: