【问题标题】:Scroll bar for line graph VB.NETVB.NET 折线图的滚动条
【发布时间】:2019-03-15 20:44:42
【问题描述】:

我在 Visual Basic 中创建了一个折线图来显示用户每天摄入的卡路里量。但是,我的用户要求我包含一个滚动条,以便沿 x 轴前后滚动以查看更多天数。

不幸的是,我以前从未做过这样的事情,在查看 Stack Overflow 和谷歌搜索后,我看不到任何人这样做的例子。

这是我目前的图表截图:

这里是代码:

        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

我将不胜感激。

【问题讨论】:

  • @MaciejLos 如果您想要 50 个代表,您可以将其写成答案
  • 如果对您有帮助,我可以将其发布为答案。所以,让我知道,我会尽快发布。

标签: vb.net winforms graph


【解决方案1】:

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

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

【讨论】:

  • @mr Popo 我的回答对您有帮助吗?如果有,请您接受我的回答。
  • 如果有任何问题,请告诉我。我会尽力提供帮助
  • 我唯一的问题是滚动条比数据走得更远:see herehere
  • 您可以尝试使用不同的设置进行调整。您可以尝试更改块大小。在上面的示例中,我将其设置为 10。您可以增加它的大小。
【解决方案2】:

基于此: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 时删除第一个条目。

【讨论】:

  • 我不太确定滚动图表是否有帮助,而是自动缩放它。
  • 但它使用户能够以一种奇怪的方式滚动数据;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多