VB.NET-图表无法绘制小于1的任何图形,例如:0,-1,-2

时间:2018-11-06 19:12:05

标签: vb.net charts

当我绘制小于1的图表时,我的图表将崩溃(退出尝试)。有人知道为什么吗?带有正数,可以绘制出漂亮的图!我正在为其输入Double值,并且X和Y数据类型在属性中设置为“自动”。

如果值是正数,就可以了:

Dim dataStr As String =“ 1,2,3,4,5,6,7,8,9,10,*”

Dim dataStr As String = "-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,*"
            Try
                Chart1.Series(0).Points.Clear()
                dataStr = dataStr.Replace(",*", "") ' Remove last comma and *
                Dim myArray = Array.ConvertAll(dataStr.Split(","c), AddressOf Convert.ToDouble)
                For Each point As Integer In myArray
                    Chart1.Series(0).Points.Add(myArray(point - 1))
                Next
            Catch
            End Try

2 个答案:

答案 0 :(得分:1)

更新:为避免在使用其他dataStr值(根据LarsTech注释)时出现异常,可以使用For循环,如下所示:

    Dim dataStr As String = "-1,-2,-3,-4,-5,-6,-7,-8,-9,-20,*"
    Try
        Chart1.Series(0).Points.Clear()
        dataStr = dataStr.Replace(",*", "") ' Remove last comma and *

        Dim myArray = Array.ConvertAll(dataStr.Split(","c), AddressOf Convert.ToDouble)
        Dim max As Integer = myArray.Length
        Dim i As Integer

        For i = 0 To max - 1
            Chart1.Series(0).Points.Add(myArray(i))
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

答案 1 :(得分:0)

由于索引到负值,因此出现错误。您可以使用点变量的绝对值,如下所示。

    Dim dataStr As String = "-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,*"

    Try
        Chart1.Series(0).Points.Clear()
        dataStr = dataStr.Replace(",*", "") ' Remove last comma and *
        Dim myArray = Array.ConvertAll(dataStr.Split(","c), AddressOf Convert.ToDouble)
        For Each point As Integer In myArray
           Chart1.Series(0).Points.Add(myArray(Math.Abs(point) - 1))
        Next
    Catch ex as exception
        MsgBox(ex.Message)
    End Try