问题
I use zedgraph to draw figures in my vb.net project. The x axis of figure is date. I have two text boxes showing the minimum and maximum values of x axis in date. The following code shows how to get x axis values when a figure is zoomed by using isEnableWheelZoom property, also see this link zedgraph EnableWheelZoom, how to get axis values after zooming?. But I found that I only get axis value, e.g. 4508.345, but NOT the date that I want. Actually on figure, it shows date correctly. My question is how to get the minimum and maximum date on figure?
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
回答1:
You almost got it. You only need to convert it to DateTime:
Dim minDate As XDate = New XDate(gcMain.GraphPane.XAxis.Scale.Min)
tbxRangeStart.Text = minDate.DateTime.ToString()
See API for more information about the XData-Format.
回答2:
Actually, XAxis.Min and XAxis.Max are XLDate. So the answer is to use 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
来源:https://stackoverflow.com/questions/8538353/zedgraph-how-to-convert-xaxis-scale-min-to-a-date