.netvb.netinheritancemschartmicrosoft-chart-controls

Inherited Chart control: How to change default values and behaviour


I'm trying to implement a scrolling line chart (similar to the old chart recorders) by inheriting from the chart control. To do this, I need to do two things:

  1. Change default properties of various objects the Chart control uses
    • Series should default to Line, instead of Column
    • ChartArea's X axis needs the interval and label format changed
    • ChartArea's Y axis needs the interval, IsStartedFromZero, and label format changed
  2. Change the behavior of the DataPointCollection's AddXY method
    • After a certain time, it should delete the oldest data point for every new datapoint
    • It should refresh the axis after every datapoint is added

While I can do all this manually, I'd like to encapsulate everything into one custom control. I want to just add the custom control with the designer, and have all these properties set already and the behavior already included.

As a test, I have tried to just change the Text property of the inherited chart control, without success:

Public Class ScrollChart
    Inherits Chart

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

And by overriding the Text property:

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

I tried to change the Series chartType in two different ways:

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

In every case, the properties in the control I added using the designer did not change. I also tried to inherit the DataPointCollection, but can't because it has no exposed New(), and thus can't be inherited.

I suspect I need to replace the Series and DataPointCollection classes that my inherited Chart uses in order to change their properties and methods, but so far I'm at a loss on how to do so.


Solution

  • Create a user control (without inheriting) and add the chart control to it, then you can create properties on the user control that wrap the chart control. For example:

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

    In the ctor for your control you can assign any properties you want!

    I don't see the benefit of inheriting in this case.