折线图VB.NET的滚动条

时间:2019-03-15 20:44:42

标签: vb.net graph

我在Visual Basic中创建了一个折线图,以显示用户每天吃多少卡路里。但是,我的用户要求我包括一个滚动条,以沿x轴前后滚动以查看更多的日子。

不幸的是,我以前从未做过这样的事情,并且在查看了Stack Overflow和Googling之后,我看不到任何这样做的例子。

这是到目前为止我的图表的屏幕截图:

这是代码:

        Cursor.Current = Cursors.WaitCursor
        CalorieChartView = True
        BurntChartView = False
        NetChartView = False
        Dim Series As Series = CalorieChart.Series(0)
        'keeps track of if the chart is empty, starting as true
        Dim empty As Boolean = True
        'Clears the chart
        Series.Points.Clear()
        'Draws the chart in dark red
        Series.Color = Color.DarkRed
        'The legend text is changed
        Series.LegendText = "Calories Consumed"
        'For each of the past 8 days, a point is plotted with how many calories were eaten in that day
        For i = -7 To 0
            Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
            Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
            'If any of the points are not 0
            If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
                'the chart is not empty
                empty = False
            End If
        Next

        HandleEmpty(empty)
        Cursor.Current = Cursors.Default

我将不胜感激。

2 个答案:

答案 0 :(得分:4)

如果我理解您的问题,则想在图形中添加水平滚动条。为了模拟数据的目的,我对您的代码进行了一些修改和新的代码。请参考下面的代码。您可以通过单独运行此代码来获得想法。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim blockSize As Integer = 10
    Cursor.Current = Cursors.WaitCursor
    CalorieChartView = True
    BurntChartView = False
    NetChartView = False

    CalorieChart.Series.Clear()
    Dim series = CalorieChart.Series.Add("My Series")
    series.ChartType = SeriesChartType.Line
    series.XValueType = ChartValueType.Int32
    'keeps track of if the chart is empty, starting as true
    Dim empty As Boolean = True
    'Clears the chart
    series.Points.Clear()
    'Draws the chart in dark red
    series.Color = Color.DarkRed
    'The legend text is changed
    series.LegendText = "Calories Consumed"
    'For each of the past 8 days, a point is plotted with how many calories were eaten in that day

    Dim sizeOfDayToDisplay As Int16 = 0

    For i = 0 To 100
        'Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
        'Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
        ''If any of the points are not 0
        'If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
        '    'the chart is not empty
        '    empty = False
        'End If

         ' just for testing purpose.
        series.Points.Add(getRandumNumber())
        series.Points(i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")

        ' series.Points.AddXY(i, Date.Now.AddDays(i).ToString("dd/MM/yyyy"))
         sizeOfDayToDisplay += 1
    Next

     'most new code added is below here
    Dim chartArea = CalorieChart.ChartAreas(Series.ChartArea)
    chartArea.AxisX.Minimum = 0
    chartArea.AxisX.Maximum = sizeOfDayToDisplay
    chartArea.CursorX.AutoScroll = True
    chartArea.AxisX.ScaleView.Zoomable = True
    chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number
    Dim position As Integer = 0
    Dim size As Integer = blockSize
    chartArea.AxisX.ScaleView.Zoom(position, size)
    chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll
    chartArea.AxisX.ScaleView.SmallScrollSize = blockSize
    'HandleEmpty(empty)
    'Cursor.Current = Cursors.Default
End Sub

Public Function getRandumNumber() As Int16
    Return CInt(Math.Floor((3500 - 1000 + 1) * Rnd())) + 1000
End Function

Here is the output graph

答案 1 :(得分:3)

基于此:How to scroll MS Chart along x-axis in vb.net,您可以使用:

Chart1.Series("LoadCell").Points.AddY(receivedData)
Chart1.ResetAutoValues()

If Chart1.Series("LoadCell").Points.Count >= 100 Then
     Chart1.Series("LoadCell").Points.RemoveAt(0)
End If
  

它会自动缩放y轴,并将x轴限制为100   条目超过100时删除第一个条目。