0

我想从 VB6 中的两个数组中绘制折线图,​​例如

x(1 to 3) =1,2,3
y(1 to 3) =1,2,3

... 轴包含值 x=1,2,3 和 y=1,2,3。

我只知道这个命令:

picture1.line(x1,y1)-(x2,y2) 

...但是这些没有任何轴选项来标记等。我得到了一个图表,但没有轴 - 只是一条带有相应所选点斜率的线。

请给我代码来命名轴,或任何其他更好的方式在 VB6 中生成图表。

4

1 回答 1

0

使用 VB6,您可以利用 Form 和 PictureBox 控件的自动缩放功能。

将 PictureBox“Picture1”添加到您的窗体中,并将​​ LineX 和 LineY 两个 Line 控件放入PictureBox。这些将形成轴。添加以下代码:

Option Explicit

Private Sub DrawLines(x() As Single, y() As Single)

    Dim nIndex As Long

    ' Ensure x() and y() are the same size.
    If UBound(x) <> UBound(y) Then
        Err.Raise 1000, , "x() and y() are different sizes"
    End If

    ' First line:
    If UBound(x) < 2 Then
        Exit Sub
    End If

    ' Draw line between first two coordinates.
    Picture1.Line (x(1), y(1))-(x(2), y(2))

    ' Subsequent lines:
    For nIndex = 3 To UBound(x)
        ' Draw line to next coordinate.
        Picture1.Line -(x(nIndex), y(nIndex))
    Next nIndex

End Sub

Private Sub Form_Load()

    SetupPictureBox Picture1

End Sub

Private Sub Picture1_Paint()

    Dim x(1 To 4) As Single
    Dim y(1 To 4) As Single

    x(1) = 15!
    y(1) = 15!

    x(2) = 45!
    y(2) = 45!

    x(3) = 15!
    y(3) = 45!

    x(4) = 15!
    y(4) = 15!

    DrawLines x(), y()

End Sub

Private Sub SetupPictureBox(ByRef pct As PictureBox)

    ' Set up coordinates for picture box.
    pct.ScaleLeft = -100
    pct.ScaleTop = -100
    pct.ScaleWidth = 200
    pct.ScaleHeight = 200

    ' Set up coordinates for the X axis.
    LineX.X1 = -100
    LineX.X2 = 100
    LineX.Y1 = 0
    LineX.Y2 = 0

    ' Set up coordinates for the Y axis.
    LineY.X1 = 0
    LineY.X2 = 0
    LineY.Y1 = -100
    LineY.Y2 = 100

End Sub

请注意,两个轴会自动绘制自己。三角形的绘制代码包含在 PictureBox 的 Paint 事件中。

于 2013-04-23T22:27:18.643 回答