2013-12-18 12 views
6

Oto kod tworzenia 3 wykresów. Chcę zadokować legendę wewnątrz obszaru każdego wykresu, ale ja dostaję legendy na dole. Użyłem DockedToChartArea, ale to nie miało sensu. Chcę umieścić legendę w każdym obszarze wykresu.Dokowanie legend w obszarze wykresu w C#

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()); 
}  
+0

używasz 'dotnethighcharts' lub' highcharts'? – SPandya

+0

Używam właściwości dot net chart/ – user2783430

+0

Czy używasz 'dotnethighcharts' lub' highcharts'? – SPandya

Odpowiedz

2

Dziękuję.

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

można ręcznie wyregulować położenie czatu legendy przez

Chart1.Legends["Legend1"].Position.Auto = false; 
Chart1.Legends["Legend1"].Position = new ElementPosition(x, y, width, hight); 

Musisz ustawić elementu przez ElementPosition z wszystkimi wymaganymi parametrami.

Możesz także sprawdzić tutaj Example, która wyjaśnia, jak dopasować legendę w obszarze wykresu.

+0

Czy to było dla ciebie przydatne? – SPandya

+0

W rzeczywistości dodawanie wykresu jest dynamiczne, więc myślę, że nie mogę znaleźć dokładnie pozycji.Jednym z nich jest szczegółowo wydany powinien wejść w chartarea każdego wykresu. – user2783430

+0

http://i.stack.imgur.com/bWBiU.jpg możesz zobaczyć wyjście – user2783430

0

Dzięki za sugestię. Miałem problemy w VB.NET uzyskanie legendy, aby dodać do każdego chartarea, dopóki nie stosuje się następującą:

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