2016-09-08 76 views
0

我希望x軸顯示4小時增量。所以5 24小時。在vb.net中,如何在圖表上以x軸顯示4小時增量

我用180個日期條目加載我的數組--3小時 - 每小時60次。

2016年8月29日下午5點零零分30秒投2016年8月29日下午五時59分30秒

2016年8月29日下午六點00分30秒投2016年8月29日6: 59:30 PM

2016年8月29日下午7時00分30秒投2016年8月29日下午7時59分三十○秒

這是我現在得到: 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

回答

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 

檢查你以前的任務,以確保他們JIVE與間隔和類型

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

一個補充那些之前對於條款。但我得到AxisX不是系列的成員。 – user3020047

+0

固定,我承認我沒有測試它之前:) – djv

+0

沒有好 - 圖表不繪製點,日期不是4小時。現在它們很多(我將在我的原始文章中包含它現在的外觀的JPEG格式)。而且我其實已經有了最小和最大值,所以我沒有按照這個建議添加它們。 – user3020047

相關問題