在c#中的图表区域内停靠图例

时间:2013-12-18 10:42:48

标签: c# asp.net-charts

以下是创建3个图表的代码。 我想将图例停靠在每个图表的区域内,但我在底部传说。我使用了DockedToChartArea,但没用。我想在每个图表区域内获得一个传奇。

enter image description here

for (int t = 0; t < ARF.Rows.Count; t += 1)
{   
    dv.RowFilter = FilterString1.ToString();

    Chart1.Series.Add("Series1" + t.ToString());
    Chart1.ChartAreas.Add("ChartArea1" + t.ToString());
    Chart1.Series[t].ChartArea = "ChartArea1" + t.ToString();

    if (dtReportDetails.Rows[0].ItemArray[7].ToString() == "Bar")
    {
        Chart1.Series[t].ChartType = SeriesChartType.Column;
        Chart1.Series[t].BorderWidth = 2;
        Chart1.Series[t].ToolTip = "(#VALX,#VALY)";
        Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisX.Title = "Learning Domains";
        Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Title = "Covered";
        Chart1.Legends.Add("Legend1" + t.ToString());
        Chart1.Legends["Legend1" + t.ToString()].Enabled = false;
        // Chart1.Legends["Legend1" + t.ToString()].Docking = Docking.Bottom;
        Chart1.Legends["Legend1" + t.ToString()].Alignment = System.Drawing.StringAlignment.Center;
        Chart1.Legends["Legend1" + t.ToString()].DockedToChartArea = "ChartArea1" + t.ToString();
        // Chart1.Legends["Legend1" + t.ToString()].IsDockedInsideChartArea = false;
        Random random = new Random();
        foreach (var item in Graph.Series[0].Points)
        {
            System.Drawing.Color c = System.Drawing.Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
            item.Color = c;

         }

         Chart1.Series[t]["PointWidth"] = "0.2";
         Chart1.Series[t]["BarLabelStyle"] = "Center";
         Chart1.Series[t]["PixelPointDepth"] = "99";
         Chart1.Series[t]["DrawingStyle"] = "Cylinder";
     }
     else if (dtReportDetails.Rows[0].ItemArray[7].ToString() == "Pie")
     {
         Chart1.Series[t].ChartType = SeriesChartType.Pie;
         Chart1.Legends.Add("Legend1" + t.ToString());
         Chart1.Legends["Legend1" + t.ToString()].Enabled = true;
         Chart1.Legends["Legend1" + t.ToString()].Docking = Docking.Bottom;
         Title t2 = new Title();
         t2.Name = "ChartArea1" + t.ToString();
         // Chart1.Legends[t].DockedToChartArea = t2.Name;
         //Chart1.Legends[t].IsDockedInsideChartArea = true;
         //Chart1.Legends["Legend1" + t.ToString()].Alignment = System.Drawing.StringAlignment.Center;
         Chart1.Legends[t].Enabled = true;
         //  Chart1.Legends[t].Docking = Docking.Bottom;
         //Chart1.Legends[t].IsDockedInsideChartArea = true;
         Chart1.Legends[t].Alignment = System.Drawing.StringAlignment.Center;
         Chart1.Series[t].Label = "#PERCENT{P2}";
         Chart1.Series[t].LegendText = "#VALX";
      }
      else if (dtReportDetails.Rows[0].ItemArray[7].ToString() == "Line")
      {
         Chart1.Series[t].ChartType = SeriesChartType.Line;
         Chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
         Chart1.Legends.Add("Legend1" + t.ToString());
         Chart1.Legends["Legend1" + t.ToString()].Enabled = false;
         //Chart1.Legends["Legend1" + t.ToString()].Docking = Docking.Bottom;
         Chart1.Legends["Legend1" + t.ToString()].Alignment = System.Drawing.StringAlignment.Center;
         Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisX.LabelStyle.Format = "dd-MMM-yyy";
         Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisX.Title = "Date";
         Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Title = "Average Score (%) ";
         Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Minimum = 0;
         Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Maximum = 100;
         // Chart1.ChartAreas["ChartArea1"].AxisY.Interval = 10;
         Chart1.Series[t].ToolTip = "Date :#VALX Avg Score(%) :#VALY";
         Chart1.Legends["Legend1" + t.ToString()].IsDockedInsideChartArea = false;
      }
      else
      {
          ///
      }
      Title tt = new Title();
      tt.Name = "ChartArea1" + t.ToString();
      Title l1 = new Title();
      l1.Name = "Legend1" + t.ToString();
      // tt.Text = "Performance Warehouses";
      Chart1.Titles.Add(tt);
      Chart1.Titles[tt.Name].Text = char.ToUpper(UserName[0]) + UserName.Substring(1);//char.ToUpper(s[0]) + s.Substring(1)
      Chart1.Titles[tt.Name].DockedToChartArea = tt.Name;
      //Chart1.Legends[l1].DockedToChartArea = tt.Name;
      Chart1.Titles[tt.Name].IsDockedInsideChartArea = false;
      Chart1.Titles[tt.Name].Font = new System.Drawing.Font("Arial", 13);
      Chart1.Titles[tt.Name].ForeColor = System.Drawing.Color.Black;
      Chart1.Series[("Series1" + t).ToString()].Points.DataBindXY(dv, dtReportDetails.Rows[0].ItemArray[4].ToString(), dv, dtReportDetails.Rows[0].ItemArray[5].ToString());
}    

3 个答案:

答案 0 :(得分:3)

您可以通过

手动调整聊天图例位置
Chart1.Legends["Legend1"].Position.Auto = false;
Chart1.Legends["Legend1"].Position = new ElementPosition(x, y, width, hight);

您需要将ElementPosition元素放置在所有必需参数中。

或者您也可以在此处查看Example,其中介绍了如何在图表区域内调整图例。

答案 1 :(得分:2)

谢谢。

if (t == ARF.Rows.Count)
{
    for (int s = 0; s < ARF.Rows.Count; s++)
    {
        Chart1.Legends.Add(new Legend("Legends1" + s.ToString()));
        Chart1.Series["Series1" + s.ToString()].Legend = "Legends1" + s.ToString();
        Chart1.Legends["Legends1" + s.ToString()].DockedToChartArea = "ChartArea1" + s.ToString();
    }
}

答案 2 :(得分:0)

感谢您的建议。我在VB.NET中遇到问题,将图例添加到每个图表区域,直到我使用以下内容:

  For k = 1 To NumChartAreas
      chart1.ChartAreas.Add(k - 1)
      chart1.ChartAreas(k - 1).Name = k - 1

      'Add new legend to each chartarea
      chart1.Legends.Add(New Legend("Legends" + k.ToString()))
      chart1.Legends("Legends" + k.ToString()).DockedToChartArea = chart1.ChartAreas(k - 1).Name
      chart1.Legends("Legends" + k.ToString()).Docking = Docking.Right

      'Populate series data for this chartarea
      Cnt = -1
      For j = 1 To NumSeries 'Number of series in this chartarea
          Cnt += 1
          chart1.Series.Add(Cnt)
          For i = 1 To NumDataPoints ' Number of datapoints in this series
              chart1.Series(Cnt).Points.AddXY(i, Rnd())
          Next
          chart1.Series(Cnt).Legend = "Legends" + k.ToString
      Next j
  Next k