0

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.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Property assignments happen after the ctor runs, so trying to hard code "test" as in the first snippet will get overwritten. Same for the second. I am not sure what you are doing in the 3rd. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Aug 08 '17 at 16:09
  • In the third snippet, I was trying to make the inherited Chart class use an inherited Series collection instead. It was an attempt to override the default ChartType. I believe it didn't work because, as you mentioned, it was overwritten the same as the other two. – Sean Martin Aug 09 '17 at 13:19
  • The first two probably are working - the default - the initial starting value is supposed to be replaced by any value assigned in code or the designer. The last block doesnt show that block being called, so that will fail. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Aug 09 '17 at 13:24
  • Also, I did read How To Ask; is there something I should or shouldn't do when asking questions in the future? Should I have shortened up the third snippet? – Sean Martin Aug 09 '17 at 13:25
  • You might want to read up on inheritance - if you inherit Chart, your thing ***is*** a chart, so you dont need a local Chart variable. To hook into the events you would `override` the respective methods (e.g. `OnMouseDoubleClick`) rather than consume the events – Ňɏssa Pøngjǣrdenlarp Aug 09 '17 at 13:47

1 Answers1

2

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.

Sam Makin
  • 1,526
  • 8
  • 23