0

I am a newbie at VB.NET coding. I am currently trying to make a real time line chart in WinForms, but I am struggling with zooming in the chart. My chart has the range of random number generated by a timer as the Y axis, and the current time (formatted in "HH:mm:ss") as the X axis. I tried to make the chart zoomable (preferably zoom in 10% every time I click the zoom in button) and horizontally scrollable, while also making the chart show only 15 points on screen and auto scroll to the last point (when the points exceed 15) but I fail to do so. I have read in the VB forum that apparently zooming in the chart with DateTime as the X axis causes problems? Can anyone help me and explain how I can solve this chart problem?

Public RandNum As New Random()  'List of random numbers generated

Private startTime As DateTime   'The starting time of x axis

Private chartDuration As Integer
Private elapsedDuration As Integer

Private Sub F000_MAIN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    'Chart
    ChartXY_RandNum.Series.Clear()
    Dim ySeries As New Series("Y Data")
    ySeries.ChartType = SeriesChartType.Line
    ySeries.XValueType = ChartValueType.DateTime
    ChartXY_RandNum.Series.Add(ySeries)

    Dim ca As ChartArea = ChartXY_RandNum.ChartAreas(0)
    ca.AxisX.Title = "Time"
    ca.AxisY.Title = "Value"
    ca.CursorX.IsUserEnabled = True
    ca.CursorX.IsUserSelectionEnabled = True

    ca.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash
    ca.AxisX.MajorGrid.LineColor = Color.FromArgb(130, Color.Gray)
    ca.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash
    ca.AxisY.MajorGrid.LineColor = Color.FromArgb(130, Color.Gray)

    'Chart Axis X
    With ca.AxisX
        .LabelStyle.Format = "HH:mm:ss"
        .IntervalType = DateTimeIntervalType.Seconds
        .Interval = 2
        'Scrollbar inside chart
        .ScrollBar.Enabled = True
        .ScrollBar.Size = 12
        .ScrollBar.ButtonStyle = ScrollBarButtonStyles.All
        .ScrollBar.IsPositionedInside = True
        .ScaleView.Zoomable = True
        .ScaleView.Size = 15
        .ScaleView.Position = Double.NaN
    End With

    ca.AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Seconds
    ca.AxisX.MajorGrid.Interval = 5

    'Default show chart
    Dim nowTime As DateTime = DateTime.Now
    'ca.AxisX.Minimum = nowTime.ToOADate()
    'ca.AxisX.Maximum = nowTime.AddSeconds(15).ToOADate()

    ca.AxisY.Minimum = minYRange
    ca.AxisY.Maximum = maxYRange

    'A dummy invinsible point
    ChartXY_RandNum.Series("Y Data").Points.AddXY(nowTime, Double.NaN)

    'Timer
    RandNumTimer.Interval = 1000     'miliseconds
    DateTimer.Start()

End Sub

And below is the Timer's code for generating the chart's data points

elapsedDuration += RandNumTimer.Interval

If elapsedDuration >= chartDuration Then
    RandNumTimer.Stop()
End If

Dim nowTime As DateTime = DateTime.Now
Dim yValue As Double = RandNum.Next(minYRange, maxYRange + 1)

ChartXY_RandNum.Series("Y Data").Points.AddXY(nowTime, yValue)

ChartXY_RandNum.ChartAreas(0).AxisX.ScaleView.Scroll(ChartXY_RandNum.ChartAreas(0).AxisX.Maximum)
4
  • ...are you using an existing chart control library? ...or using Excel charts via COM/ActiveX? ...or are you intentionally building your own chart widget from scratch? If so, is this for your own education/satisfaction, or some other reason? Commented Dec 4 at 5:20
  • I am using the chart widget in VB NET from scratch (System.Windows.Forms.DataVisualization.Charting.Chart), no external libraries, and this is a project in my current internship. Commented Dec 4 at 6:37
  • Please edit your post to show us your actual VB.NET Form class code. Also include (only) the lines from InitializeComponent concerning the Chart control. Commented Dec 4 at 6:39
  • I already edited it with the code for Form Load and Timer Commented Dec 4 at 6:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.