Belgin Fish
Belgin Fish

Reputation: 19837

Only Show Full X-Axis Intervals VB.NET

I'm using the .NET charting library to show a line graph (voltage versus time). The full data length always varies (can be between 10ms of data up to ~250ms of data).

The issue I'm running into is that the final interval on the chart is usually cut short. For example if there is 230ms of data, and the chart has 50ms intervals, the final interval will be cut off after 30ms and won't show the complete interval on the x-axis. Ideally I want it to go all the way to 250ms and simply stop showing the data after 230ms (but extend the x-axis range to fully complete the interval).

Is there an option somewhere to not cut intervals short if the data ends? I haven't been able to find anything.

Upvotes: 0

Views: 653

Answers (1)

djv
djv

Reputation: 15774

Just set the min and max explicitly

' make a test series, add points
Dim s As New Series()
For i = 0 To 230
    s.Points.Add(New DataPoint(i, i))
Next
' add series to chart
Chart1.Series.Add(s)
' set axis interval, min, and max
Chart1.ChartAreas(0).AxisX.Interval = 50
Chart1.ChartAreas(0).AxisX.Minimum = 0
Chart1.ChartAreas(0).AxisX.Maximum = 250

If you don't want to hard-code it, it can be done programatically

Chart1.ChartAreas(0).AxisX.Maximum = Math.Ceiling(maxValue / interval) * interval

If you don't want to set the interval, you can calculate it based on the max value. This is an example but you may want to tweak the ranges and values

Private Function getInterval(maxValue As Double) As Double
    Select Case maxValue
        Case 0 To 10
            Return 1
        Case 10 To 50
            Return 5
        Case 50 To 100
            Return 10
        Case 100 To 1000
            Return 50
        Case Else
            Return 100
    End Select
End Function

''' 

Dim maxValue = 33
' make a test series, add points
Dim s As New Series()
For i = 1 To maxValue
    s.Points.Add(New DataPoint(i, i))
Next
' add series to chart
Chart1.Series.Add(s)
Dim interval = getInterval(maxValue)
Chart1.ChartAreas(0).AxisX.Minimum = 0
Chart1.ChartAreas(0).AxisX.Maximum = Math.Ceiling(maxValue / interval) * interval

Upvotes: 2

Related Questions