2017-08-08 62 views
0

我試圖通過從圖表控件繼承來實現滾動折線圖(類似於舊的圖表記錄器)。要做到這一點,我需要做兩件事情:繼承圖表控件:如何更改默認值和行爲

  1. 各種對象的更改默認屬性圖表控件使用
    • 系列應該默認爲線,而不是列
    • ChartArea的X軸所需要的時間間隔和標籤格式改變
    • ChartArea的Y軸所需要的間隔,IsStartedFromZero,和標籤格式改變
  2. 變化T的行爲他DataPointCollection的AddXY方法
    • 在一定時間之後,應該刪除最早的數據點,每一個新的數據點
    • 應該刷新軸每個數據點被添加

雖然我能後手動完成所有這些,我想將所有內容封裝到一個自定義控件中。我只想添加自定義控件和設計器,並且已經設置了所有這些屬性並且已經包含了該行爲。

作爲一個測試,我都試過,只是更改繼承圖表控件的Text屬性,但沒有成功:

Public Class ScrollChart 
    Inherits Chart 

    Public Sub New() 
     Me.Text = "Test" 
    End Sub 
End Class 

而且通過重寫Text屬性:

Public Class ScrollChart 
    Inherits Chart 

    Private _myText As String = "Test" 
    Public Overrides Property Text() As String 
     Get 
      Return _myText 
     End Get 
     Set(value As String) 
      _myText = value 
     End Set 
    End Property 
End Class 

我試圖用兩種不同的方式更改Series chartType:

Public Class ScrollChart 
    Inherits Chart 

    Friend WithEvents Chart1 As Chart 

    Private Sub InitializeComponent() 
     Dim ChartArea1 As System.Windows.Forms.DataVisualization.Charting.ChartArea = New System.Windows.Forms.DataVisualization.Charting.ChartArea() 
     Dim Legend1 As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend() 
     Dim Series1 As ScrollSeries = New ScrollSeries 
     Me.Chart1 = New System.Windows.Forms.DataVisualization.Charting.Chart() 
     CType(Me.Chart1, System.ComponentModel.ISupportInitialize).BeginInit() 
     CType(Me, System.ComponentModel.ISupportInitialize).BeginInit() 
     Me.SuspendLayout() 
     ' 
     'Chart1 
     ' 
     ChartArea1.Name = "ChartArea1" 
     Me.Chart1.ChartAreas.Add(ChartArea1) 
     Legend1.Name = "Legend1" 
     Me.Chart1.Legends.Add(Legend1) 
     Me.Chart1.Location = New System.Drawing.Point(0, 0) 
     Me.Chart1.Name = "Chart1" 
     Series1.ChartArea = "ChartArea1" 
     Series1.Legend = "Legend1" 
     Series1.Name = "Series1" 
     Series1.ChartType = SeriesChartType.Line 
     Me.Chart1.Series.Add(Series1) 
     Me.Chart1.Size = New System.Drawing.Size(300, 300) 
     Me.Chart1.TabIndex = 0 
     Me.Chart1.Text = "Test" 
     CType(Me.Chart1, System.ComponentModel.ISupportInitialize).EndInit() 
     CType(Me, System.ComponentModel.ISupportInitialize).EndInit() 
     Me.ResumeLayout(False) 

    End Sub 
End Class 

Public Class ScrollSeries 
    Inherits Series 

    Public Sub New() 
     MyBase.New() 

     Me.ChartType = SeriesChartType.Line 
    End Sub 

End Class 

在任何情況下,使用設計器添加的控件中的屬性都沒有改變。我也嘗試繼承DataPointCollection,但不能因爲它沒有公開的New(),因此不能被繼承。

我懷疑我需要替換繼承Chart使用的Series和DataPointCollection類,以便更改它們的屬性和方法,但到目前爲止,我對如何實現這一點感到茫然。

+0

屬性分配在ctor運行後發生,所以試圖在第一個代碼段中對代碼「test」進行硬編碼將被覆蓋。第二個是一樣的。我不確定你在第三場比賽中的表現。請閱讀[問]並參加[遊覽] – Plutonix

+0

在第三個片段中,我試圖讓繼承的Chart類使用繼承的Series集合。這是試圖覆蓋默認的ChartType。我相信這是行不通的,因爲正如你所提到的那樣,它被覆蓋,就像其他兩個一樣。 –

+0

前兩個可能正在工作 - 默認情況下 - 初始起始值應該被替換爲代碼或設計器中分配的任何值。最後一個塊沒有顯示被調用的塊,因此會失敗。請閱讀[問]並參加[旅遊] – Plutonix

回答

2

創建一個用戶控件(不需要繼承)並向其添加圖表控件,然後您可以在包含圖表控件的用戶控件上創建屬性。例如:

Public Property ChartText() As String 
    Get 
     Return Chart1.Text 
    End Get 
    Set(value As String) 
     Chart1.Text = value 
    End Set 
End Property 

在控制器中,您可以指定任何您想要的屬性!

我沒有看到在這種情況下繼承的好處。