在vb6中绘制带有两个轴的折线图

时间:2013-04-23 20:22:42

标签: input vb6 charts

我想在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中生成图表的任何其他更好的方法。

1 个答案:

答案 0 :(得分:0)

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

将PictureBox“Picture1”添加到表单中,并将两个Line控件LineX和LineY 放入 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事件中。