在vb.net中,如何在图表图表上的x轴上显示4小时的增量

时间:2016-09-08 21:05:03

标签: vb.net charts

我希望x轴显示4小时的增量。所以5小时24小时。

我用180个日期条目加载我的数组--3小时 - 每小时60个。

8/29/2016 5:00:30 PM threw 8/29/2016 5:59:30 PM

8/29/2016 6:00:30 PM threw 8/29/2016 6:59:30 PM

8/29/2016 7:00:30 PM threw 8/29/2016 7:59:30 PM

这就是我现在得到的: enter image description here

注意:对于上面,数组类型是String。如果我将其设为日期时间,则点数不会绘制,日期也是奇数。如下所示。enter image description here

这是我的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim iServerId As Integer = 47
    Dim strTimeframe As String = "day"

    Chart1.Series.Clear()
    Chart1.Titles.Add("Server id " & iServerId.ToString() & ", Port index 8. The In/Out Mbps - by " & strTimeframe)

    ' Rotates the X axis text.
    Chart1.ChartAreas(0).AxisX.LabelStyle.Angle = -90

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' I would the increment to show every 4 hours.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Chart1.ChartAreas(0).AxisX.Minimum = 1
    ' When running 'day', this shows every 5 minutes: 5:00:30 PM, 5:05:30 PM, 5:10:30 PM, 5:15:30 PM, 5:20:30 PM increment lines.
    Chart1.ChartAreas(0).AxisX.Maximum = 24

    ' Arrays - 1 X array for the "start time", 2 Y arrays for the "IN Mbps" and "OUT Mbps".
    Dim xAxisArray() As String
    Dim yAxisInMbpsArray() As Integer
    Dim yAxisOutMbpsArray() As Integer        

    Dim DBFunc As New DatabaseFunctions
    Dim drReturn As SqlDataReader
    Dim strMessage As String = ""
    Dim iArrayCount As Integer = 0

    DBFunc.OpenDB()

    Try
        With DBFunc.objCmd
            .CommandType = CommandType.StoredProcedure
            .CommandText = "SelectBandwidthLogCalculated"
            .Parameters.Clear()

            .Parameters.AddWithValue("@ServerId", iServerId)
            .Parameters.AddWithValue("@TimeFrame", strTimeframe)

            ' Execute.
            drReturn = .ExecuteReader

            If drReturn.HasRows = True Then
                ' Build the array.
                Do While drReturn.Read
                    ReDim Preserve xAxisArray(iArrayCount)
                    ReDim Preserve yAxisInMbpsArray(iArrayCount)
                    ReDim Preserve yAxisOutMbpsArray(iArrayCount)

                    xAxisArray(iArrayCount) = drReturn("StartDate")
                    yAxisInMbpsArray(iArrayCount) = drReturn("BandwidthInMbps")
                    yAxisOutMbpsArray(iArrayCount) = drReturn("BandwidthOutMbps")

                    ' increment.
                    iArrayCount += 1
                Loop

                ' Close reader.
                drReturn.Close()
            Else
                ' No rows error.
                strMessage = "Critical Error - no rows. Contact IT, do not continue."
            End If
        End With
    Catch sqlex As SqlException
        strMessage = "Critical Error - when retrieving rows. Contact IT, do not continue. SQL Error: " & sqlex.Message
    Catch ex As Exception
        strMessage = "Critical Error - when retrieving rows. Contact IT, do not continue. Error: " & ex.Message
    Finally
        ' Close database.
        DBFunc.CloseDB()
    End Try

    If strMessage = "" Then
        'Create a new series and add data points to it.
        Dim sIn As New Series
        Dim sOut As New Series

        sIn.Name = "In Mbps"
        sOut.Name = "Out Mbps"

        ' Change to a line graph.
        sIn.ChartType = SeriesChartType.Line
        sOut.ChartType = SeriesChartType.Line

        ' Since both arrays should have the same qty, either's count should be fine.
        For idx As Integer = 0 To xAxisArray.Count - 1
            sIn.Points.AddXY(xAxisArray(idx), yAxisInMbpsArray(idx))
            sOut.Points.AddXY(xAxisArray(idx), yAxisOutMbpsArray(idx))
        Next

        'Add the series to the Chart1 control.
        Chart1.Series.Add(sIn)
        Chart1.Series.Add(sOut)
    Else
        lblMessage.Text = strMessage
    End If
End Sub

进行建议更改后: enter image description here

1 个答案:

答案 0 :(得分:0)

将其设为DateTime,并确保执行

sIn.XValueType = ChartValueType.Time
sOut.XValueType = ChartValueType.Time

然后进行4小时的间隔

Chart1.ChartAreas(0).AxisX.IntervalType = DataVisualization.Charting.DateTimeIntervalType.Hours
Chart1.ChartAreas(0).AxisX.Interval = 4

检查以前的作业,确保他们按照间隔输入

Chart1.ChartAreas(0).AxisX.Minimum = 1
Chart1.ChartAreas(0).AxisX.Maximum = 24