vb.net 如何在vb.net中绘制连续线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32047375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:24:08  来源:igfitidea点击:

How to draw continuous line graph in vb.net

vb.net

提问by shivcena

I'm very new to VB.NET. I'm trying to draw continuous line graph based on certain time intervals using the graph UI control. I'm able to draw the graph the first time and getting unhandled exception on second time. Please refer to my code below,

我对 VB.NET 很陌生。我正在尝试使用图形 UI 控件根据特定时间间隔绘制连续折线图。我能够第一次绘制图形并在第二次得到未处理的异常。请参考我下面的代码,

Dim s As New Series

    s.Name = "aline"

    'Change to a line graph.
    s.ChartType = SeriesChartType.Line

    For index As Integer = 1 To 10
        s.Points.AddXY("1990", 27)
        s.Points.AddXY("1991", 15)
        s.Points.AddXY("1992", index)
    Next
    Chart1.Series.Add(s)
    Threading.Thread.Sleep(1000)
    s.Points.AddXY("1993", 27)
    s.Points.AddXY("1994", 15)
    s.Points.AddXY("1995", 10)
    Chart1.Series.Add(s)'Here im getting exception

But on second time if I try to update the values using Chart1.Series.Add(s), I'm getting "A chart element with the name 'aline' already exists in the 'SeriesCollection'" error. Please guide how to update the values continuously.

但是第二次,如果我尝试使用 更新值Chart1.Series.Add(s),我会收到“'SeriesCollection' 中已经存在名为'aline' 的图表元素”错误。请指导如何不断更新值。

采纳答案by Andrew Morton

You do not need to do anything to get the chart to update - just remove the second Chart1.Series.Add(s).

您无需执行任何操作即可更新图表 - 只需删除第二个即可Chart1.Series.Add(s)

In some cases, you may need to force an update, in which case you would use the Chart.DataBind method.

在某些情况下,您可能需要强制更新,在这种情况下,您将使用Chart.DataBind 方法

From the Remarks section of the DataBind method documentation:

来自 DataBind 方法文档的备注部分:

In cases where a data source is set to a chart and no other data operations are required, the DataBind method does not have to be explicitly called. In these cases, the Chart itself will data bind to the data source prior to being rendered.

如果数据源设置为图表并且不需要其他数据操作,则不必显式调用 DataBind 方法。在这些情况下,图表本身会在呈现之前将数据绑定到数据源。

The action of this is easier to see if you add a button to the form to add some values: when you click the button, you see the chart adjust its x-axis and add the new points.

如果您向表单添加一个按钮来添加一些值,则更容易看到此操作:当您单击该按钮时,您会看到图表调整其 x 轴并添加新点。

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    Dim s As New Series

    Sub DoChart()

        s.Name = "aline"

        'Change to a line graph.
        s.ChartType = SeriesChartType.Line

        For index As Integer = 1 To 10
            s.Points.AddXY("1990", 27)
            s.Points.AddXY("1991", 15)
            s.Points.AddXY("1992", index)
        Next
        Chart1.Series.Add(s)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        s.Points.AddXY("1993", 27)
        s.Points.AddXY("1994", 15)
        s.Points.AddXY("1995", 10)
        'Chart1.DataBind()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DoChart()

    End Sub

End Class