2013-12-18 13 views
6

Ecco un codice per la creazione di 3 grafici. Voglio ancorare la legenda all'interno dell'area di ciascun grafico ma sto leggendo le leggende in basso. Ho usato DockedToChartArea ma era inutile. Voglio ottenere una legenda all'interno di ogni area del grafico.Inserimento di legende all'interno dell'area del grafico in 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

stai usando 'dotnethighcharts' o' highcharts'? – SPandya

+0

Sto usando la proprietà del diagramma a punti/ – user2783430

+0

Stai usando 'dotnethighcharts' o' highcharts'? – SPandya

risposta

2

Grazie.

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

È possibile regolare manualmente la posizione Chat leggenda

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

È necessario posizionare l'elemento da ElementPosition con tutti i parametri richiesti.

Oppure è anche possibile controllare Example qui che spiega come regolare la legenda all'interno dell'area del grafico.

+0

E 'stato utile? – SPandya

+0

In realtà sto aggiungendo il grafico è dinamico, quindi penso che non riesca a trovare esattamente la posizione. Un altro aspetto è che i dettagli dovrebbero essere inclusi all'interno della tabella di ogni grafico. – user2783430

+0

http://i.stack.imgur.com/bWBiU.jpg puoi vedere l'output – user2783430

0

Grazie per il suggerimento. Ho avuto problemi in VB.NET ottenere le leggende ad aggiungere ad ogni ChartArea, fino a quando ho usato il seguente:

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 
Problemi correlati