2011-01-26 73 views
2

我想使用的例子從下面 http://code.msdn.microsoft.com/mschart 實時數據圖表,但雖然應用程序提供C#代碼複製/粘貼到你的程序中,我發現了以下錯誤:找不到適合稱爲Dispose的函數的合適方法。 任何人都知道什麼是錯的?.NET 4.0圖表控件問題

下面是應用程序給出的確切代碼。有人知道如何使用它嗎?

using System.Windows.Forms.DataVisualization.Charting; 
... 
private Thread addDataRunner; 
private Random rand = new Random(); 
private System.Windows.Forms.DataVisualization.Charting.Chart chart1; 
public delegate void AddDataDelegate(); 
public AddDataDelegate addDataDel; 
... 

private void RealTimeSample_Load(object sender, System.EventArgs e) 
{ 

    // create the Adding Data Thread but do not start until start button clicked 
    ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop); 
    addDataRunner = new Thread(addDataThreadStart); 

    // create a delegate for adding data 
    addDataDel += new AddDataDelegate(AddData); 

} 

private void startTrending_Click(object sender, System.EventArgs e) 
{ 
    // Disable all controls on the form 
    startTrending.Enabled = false; 
    // and only Enable the Stop button 
    stopTrending.Enabled = true; 

    // Predefine the viewing area of the chart 
    minValue = DateTime.Now; 
    maxValue = minValue.AddSeconds(120); 

    chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate(); 
    chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate(); 

    // Reset number of series in the chart. 
    chart1.Series.Clear(); 

    // create a line chart series 
    Series newSeries = new Series("Series1"); 
    newSeries.ChartType = SeriesChartType.Line; 
    newSeries.BorderWidth = 2; 
    newSeries.Color = Color.OrangeRed; 
    newSeries.XValueType = ChartValueType.DateTime; 
    chart1.Series.Add(newSeries); 

    // start worker threads. 
    if (addDataRunner.IsAlive == true) 
    { 
     addDataRunner.Resume(); 
    } 
    else 
    { 
     addDataRunner.Start(); 
    } 

} 

private void stopTrending_Click(object sender, System.EventArgs e) 
{ 
    if (addDataRunner.IsAlive == true) 
    { 
     addDataRunner.Suspend(); 
    } 

    // Enable all controls on the form 
    startTrending.Enabled = true; 
    // and only Disable the Stop button 
    stopTrending.Enabled = false; 
} 

/// Main loop for the thread that adds data to the chart. 
/// The main purpose of this function is to Invoke AddData 
/// function every 1000ms (1 second). 
private void AddDataThreadLoop() 
{ 
    while (true) 
    { 
     chart1.Invoke(addDataDel); 

     Thread.Sleep(1000); 
    } 
} 

public void AddData() 
{ 
    DateTime timeStamp = DateTime.Now; 

    foreach (Series ptSeries in chart1.Series) 
    { 
     AddNewPoint(timeStamp, ptSeries); 
    } 
} 

/// The AddNewPoint function is called for each series in the chart when 
/// new points need to be added. The new point will be placed at specified 
/// X axis (Date/Time) position with a Y value in a range +/- 1 from the previous 
/// data point's Y value, and not smaller than zero. 
public void AddNewPoint(DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries) 
{ 
    double newVal = 0; 

    if (ptSeries.Points.Count > 0) 
    { 
     newVal = ptSeries.Points[ptSeries.Points.Count -1 ].YValues[0] + ((rand.NextDouble() * 2) - 1); 
    } 

    if (newVal < 0) 
     newVal = 0; 

    // Add new data point to its series. 
    ptSeries.Points.AddXY(timeStamp.ToOADate(), rand.Next(10, 20)); 

    // remove all points from the source series older than 1.5 minutes. 
    double removeBefore = timeStamp.AddSeconds((double)(90) * (-1)).ToOADate(); 
    //remove oldest values to maintain a constant number of data points 
    while (ptSeries.Points[0].XValue < removeBefore) 
    { 
     ptSeries.Points.RemoveAt(0); 
    } 

    chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue; 
    chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate(); 

    chart1.Invalidate(); 
} 

/// Clean up any resources being used. 
protected override void Dispose(bool disposing) 
{ 
    if ((addDataRunner.ThreadState & ThreadState.Suspended) == ThreadState.Suspended) 
    { 
     addDataRunner.Resume(); 
    } 
    addDataRunner.Abort(); 

    if(disposing) 
    { 
     if (components != null) 
     { 
      components.Dispose(); 
     } 
    } 
    base.Dispose(disposing); 
}  
... 
+1

MS Chart Controls令我頭疼。 – 2011-01-26 16:57:25

+0

沒有repro。用錯誤的文件名和行號記錄你的問題。 – 2011-01-26 17:16:13

回答

0

您必須將複製的代碼插入到正確的類中。因爲我只知道控件繼承的類(例如Forms,UserControls等)有一個Dispose方法。

只需將複製的代碼粘貼到新窗體中即可使用。

0

似乎你忘了粘貼所有的文件,也許文件名爲xxxx.designer.cs? 有時,類是跨多個文件定義的部分。

+0

沒有要複製的文件。我從下載頁面下載了示例並運行可執行文件。在該應用程序內部,您可以選擇所需的圖表,併爲您提供將代碼複製/粘貼到應用程序中的代碼。我剛剛創建了一個新類並粘貼了該代碼並嘗試編譯它,但它給了我那個錯誤 – user579674 2011-01-26 17:30:42