更改vb.net图表xAxis最大和最小日期时间类型

时间:2017-07-19 17:56:42

标签: vb.net datetime charts visual-studio-2017

我在vb.net windows窗体应用程序上使用图表控件。

我正在将数据加载到chart1并且看起来都很好;我正在使用这种数据类型:

Chart1.Series("test").XValueType = DataVisualization.Charting.ChartValueType.DateTime
Chart1.Series("test").YValueType = DataVisualization.Charting.ChartValueType.Int32

然后我使用Trackbar1控制来更改/缩放/重新缩放 xAxis 并使用此代码:

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        ChartRanger(TrackBar1.Value)
End Sub

在属性窗口中TrackBar1 min = 0和max = 366。

虽然ChartRanger函数如下所示:

Private Sub ChartRanger(theDays As Integer)
        Chart1.ChartAreas(0).AxisX.IntervalType = 0
        Chart1.ChartAreas(0).AxisX.Interval = 0
        Chart1.ChartAreas(0).AxisX.Minimum = 0
        Chart1.ChartAreas(0).AxisX.Maximum = theDays
End Sub

没有调试错误,它看起来像工作;但输出中的逻辑不正确。

我想要实现的目标是让应用用户为minimum xAxis 定义maximum日期时间和chart1日期时间。拥有数据集包含每日记录。

更新:我已经看过其他相关帖子,他们建议ASP,VBA的答案但是找不到VB.Net win表格的答案;它更符合这里最好的逻辑。

1 个答案:

答案 0 :(得分:1)

我认为您的问题是Chart1.ChartAreas(0).AxisX.Maximum = theDays。 AxisX类型是表示DateTime的Double,而theDays是Integer。您需要根据AxisX的值确定theDays的最长日期,并使用ToOADate()将该日期转换为双倍。

这是一个有效的例子:

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

        Dim s = Chart1.Series.Add("s")

        s.ChartType = DataVisualization.Charting.SeriesChartType.Point

        s.XValueType = DataVisualization.Charting.ChartValueType.DateTime
        s.XValueType = DataVisualization.Charting.ChartValueType.Int32


        For i As Integer = 0 To 100
            s.Points.AddXY(Date.Today.AddDays(i), i)
        Next

        TrackBar1.Maximum = 100
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Dim val As Integer = TrackBar1.Value
        Chart1.ChartAreas(0).AxisX.Maximum = Date.Today.AddDays(val).ToOADate()
    End Sub
End Class

或者,如果您还不知道最小和最大日期,则可以设置相对于TrackBar位置的轴最大值:

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll

    ' Get min and max dates
    Dim maxDate As Date = Date.FromOADate(Chart1.Series("s").Points.FindMaxByValue("X").XValue)
    Dim minDate As Date = Date.FromOADate(Chart1.Series("s").Points.FindMinByValue("X").XValue)

    ' Get total days between dates
    Dim totalDays = maxDate.Subtract(minDate).Days

    ' Get the bar position as a percent of 100
    Dim barPct As Double = TrackBar1.Value / TrackBar1.Maximum

    ' Find the maximum day that should be displayed on the plot
    Dim maxAxisDay As Integer = barPct * totalDays

    ' Get the date of the maximum day
    Dim maxAxisDate = minDate.AddDays(maxAxisDay)

    ' Set maximum. Convert the date to a double using .ToOADate()
    Chart1.ChartAreas(0).AxisX.Maximum = maxAxisDate.ToOADate()
End Sub

编辑: 假设轨迹栏最大值设置为数据集所代表的总天数,则更容易:

  Private Sub TrackBar1_Scroll_1(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        ' Get the first date
        Dim minDate As Date = Date.FromOADate(Chart1.Series("s").Points.FindMinByValue("X").XValue)

        ' Assuming that the trackbar has the total number of days represented, the maximum date to display is the first date plus the 
        ' days represented by the trackbar
        Dim maxDisplayDate As Date = minDate.AddDays(TrackBar1.Value)

        ' Set the max axis
        Chart1.ChartAreas(0).AxisX.Maximum = maxDisplayDate.ToOADate()

    End Sub