2016-06-13 143 views
0

,因爲我做了這樣的事情它已經有一段時間,但是我想創建從DataVisualization.Chart類派生的自定義圖表類, 我有以下C#創建自定義圖表類

public class clsCustomChart:System.Windows.Forms.DataVisualization.Charting.Chart 
{ 

    public clsCustomChart(string strChartTitle, double[] dblX, double[] dblY) 
    { 
     // Create the chart 

     // Create the chart 
     Chart chartReturn = new Chart(); 
     chartReturn.BackColor = Color.FromArgb(50, Color.DarkGray); 
     chartReturn.BorderlineDashStyle = ChartDashStyle.Solid; 
     chartReturn.BorderlineColor = Color.Black; 
     chartReturn.Width = 300; 
     chartReturn.Height = 300; 

     // Create the legend 
     Legend l = new Legend("Legend"); 
     l.Docking = Docking.Bottom; 
     l.BackColor = Color.Transparent; 
     chartReturn.Legends.Add(l); 

     // Create the chart area 
     ChartArea a = new ChartArea("ChartArea1"); 
     a.Area3DStyle.Enable3D = false; 
     a.Area3DStyle.WallWidth = 0; 
     a.BackColor = Color.FromArgb(100, Color.Black); 

     chartReturn.ChartAreas.Add(a); 

     // Create the axis 
     a.AxisX.LineColor = Color.Silver; 
     a.AxisX.MajorGrid.Enabled = true; 
     a.AxisX.MinorGrid.Enabled = false; 
     a.AxisX.MajorGrid.LineColor = Color.FromArgb(50, Color.Black); 
     a.AxisX.LabelStyle.Font = new System.Drawing.Font("Arial", 8F); 

     a.AxisY.LineColor = Color.Silver; 
     a.AxisY.MajorGrid.Enabled = true; 
     a.AxisY.MinorGrid.Enabled = false; 
     a.AxisY.MajorGrid.LineColor = Color.FromArgb(50, Color.Black); 
     a.AxisY.LabelStyle.Font = new System.Drawing.Font("Arial", 8F); 

     // Chart title 
     chartReturn.Titles.Add(new Title(strChartTitle)); 

     // Add the data 
     // Create the data series 
     Series s = new Series("IN"); 
     s.ChartType = SeriesChartType.Line; 

     dblX.ToList<double>().ForEach(x => { s.Points.Add(x); }); 
     s.Color = Color.FromArgb(200, Color.Red); 
     s.BorderWidth = 3; 

     Series s2 = new Series("OUT"); 
     s2.ChartType = SeriesChartType.Line; 

     dblY.ToList<double>().ForEach(x => { s2.Points.Add(x); }); 
     s2.Color = Color.FromArgb(200, Color.Green); 
     s2.BorderWidth = 3; 

     chartReturn.Series.Add(s); 
     chartReturn.Series.Add(s2); 

     chartReturn.SaveImage("c:/test/" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".jpeg", ChartImageFormat.Jpeg); 

    } 

} 

的自定義圖表中的代碼在創建爲圖表對象時都進行了測試並且工作正常,並且自定義類將圖表保存爲圖像良好。

然而,當我嘗試這一種形式

Chart C = (Chart)new clsCustomChart("TEST",x,y); 

this.Controls.Add(C); 

我不明白圖表......任何人都可以告訴.....

TIA

+0

可以爲您的DataVisualization圖表控件添加到窗口的形式,並瞭解IDE是添加代碼?從那裏,你可以用你自己的派生類型替換圖表類型。 – dfdsfdsfsdf

+0

你想從構造函數中「返回」一個值嗎?您正在定義的圖表嵌套在您的自定義控件中,而不是自定義控件本身。 – nicholas

+0

對不起,我不明白?我已經在form_load上使用了表單中類的代碼,並且它很好地繪製了圖表。 –

回答

2
// Create the chart 
Chart chartReturn = new Chart(); 

這會創建一個圖表,然後您可以對其進行設計並丟棄。

刪除它並用this替換chartReturn

你也可能要提供的情況下,你想將它放在通過設計窗體上的無參數的構造函數 ..

+0

我試過了,首先,這是我的想法,但是我只是讀了一遍,因爲我正在創造這個,非常感謝,我知道我的記憶力並不差。 –