2011-12-16 141 views
0

我使用zedgraph在我的vb.net項目中繪製圖形。 圖的x軸是日期。我有兩個文本框顯示日期中x軸的最小值和最大值。以下代碼顯示如何在使用isEnableWheelZoom屬性縮放圖形時獲取x軸值,另請參閱此鏈接zedgraph EnableWheelZoom, how to get axis values after zooming?。但是我發現我只能得到軸值,例如4508.345,但是不是我想要的日期。其實在圖上,它正確顯示日期。我的問題是如何獲得數字上的最小和最大日期?zedgraph,如何將XAxis.Scale.Min轉換爲日期

Friend WithEvents gcMain As ZedGraph.ZedGraphControl 
Me.gcMain.IsZoomOnMouseCenter = True 
Me.gcMain.IsEnableWheelZoom = True 

    Private Sub gcMain_ZoomEvent(ByVal sender As ZedGraphControl, ByVal oldState As ZoomState, ByVal newState As ZoomState) Handles gcMain.ZoomEvent 
    tbxRangeStart.Text = CStr(New XDate(gcMain.GraphPane.XAxis.Scale.Min)) 
    tbxRangeEnd.Text = CStr(New XDate(gcMain.GraphPane.XAxis.Scale.Max)) 

End Sub 

回答

0

你幾乎明白了。您只需將其轉換爲DateTime:

Dim minDate As XDate = New XDate(gcMain.GraphPane.XAxis.Scale.Min) 
tbxRangeStart.Text = minDate.DateTime.ToString() 

有關XData格式的更多信息,請參閱API

+0

嗨,謝謝你的回答。我發現我可以在zedgraph中使用XLDateToCalendarDate函數。但你的答案似乎更好 - ^ – Summer 2011-12-19 17:54:40

0

其實,XAxis.Min和XAxis.Max都是XLDate。所以答案是使用XLDateToCalendarDate

Private Sub gcMain_ZoomEvent(ByVal sender As ZedGraphControl, ByVal oldState As ZoomState, ByVal newState As ZoomState) Handles gcMain.ZoomEvent 

    Dim xlMin, xlMax As Double 
    Dim year, month, day, hour, minute As Integer 
    Dim second As Double 
    Dim dateStart As Date 
    Dim dateEnd As Date 

    xlMin = gcMain.GraphPane.XAxis.Scale.Min 
    xlMax = gcMain.GraphPane.XAxis.Scale.Max 

    ZedGraph.XDate.XLDateToCalendarDate(xlMin, year, month, day, hour, minute, second) 
    dateStart = New Date(year, month, day, hour, minute, CInt(second)) 

    ZedGraph.XDate.XLDateToCalendarDate(xlMax, year, month, day, hour, minute, second) 
    dateEnd = New Date(year, month, day, hour, minute, CInt(second)) 

       SetDateText(textbox1.TextBox, dateStart) 
       SetDateText(text) 

End Sub 

    Private Sub SetDateText(ByRef tbx As TextBox, ByVal dte As Date) 
    Try 
     tbx.Text = dte.Hour.ToString("00") + " : " + dte.Minute.ToString("00") 
    Catch 
    End Try 
End Sub