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)
Formclass code. Also include (only) the lines fromInitializeComponentconcerning theChartcontrol.