|
|
Title | Draw a radar graph in Visual Basic 6 |
Description | This example shows how to draw a radar graph in Visual Basic 6. |
Keywords | radar graph, graph |
Categories | Algorithms, Graphics |
|
|
The program stores data in the m_RData array of Singles. The DrawRadarGraph subroutine draws the graph.
The code starts by setting the PictureBox's ScaleWidth and ScaleHeight properties so it scales the data by the same amount vertically and horizontally and so the data fills the smaller of the two dimensions. It then sets ScaleLeft and ScaleTop to move the origin to the center of the form.
Next the code checks the txtCircleIncrement text box and, if it contains a number greater than zero, the program draws circles centered at the origin that distance apart. Then if the chkDrawRadii check box is checked, the program draws lines from (0, 0) to each data point converted into polar coordinates. Finally the program connects the data points, again converted into polar coordinates.
|
|
' Draw the graph on the PictureBox.
Private Sub DrawRadarGraph(ByVal pic As PictureBox, _
points() As Single)
Const PI As Single = 3.14159265
Dim aspect As Single
Dim i As Integer
Dim circle_increment As Single
Dim theta As Single
Dim dtheta As Single
Dim x As Single
Dim y As Single
' Find the center and calculate the scale.
If pic.ScaleWidth < pic.ScaleHeight Then
aspect = pic.ScaleHeight / pic.ScaleWidth
pic.ScaleWidth = 2 * MAX_R
pic.ScaleHeight = pic.ScaleWidth * aspect
Else
aspect = pic.ScaleWidth / pic.ScaleHeight
pic.ScaleHeight = 2 * MAX_R
pic.ScaleWidth = pic.ScaleHeight * aspect
End If
pic.ScaleLeft = -pic.ScaleWidth / 2
pic.ScaleTop = -pic.ScaleHeight / 2
' Draw.
pic.Cls
dtheta = 2 * PI / (UBound(points) - LBound(points))
theta = 0
' Draw the circles.
circle_increment = Val(txtCircleIncrement.Text)
If circle_increment > 0 Then
For i = circle_increment To MAX_R Step _
circle_increment
pic.Circle (0, 0), i, vbWhite
Next i
End If
' Draw the radii.
If chkDrawRadii.Value = vbChecked Then
theta = 0
For i = LBound(points) To UBound(points)
theta = theta + dtheta
x = points(i) * Cos(theta)
y = points(i) * Sin(theta)
pic.Line (0, 0)-(x, y), vbBlue
Next i
End If
' Draw the data.
pic.CurrentX = points(UBound(points))
pic.CurrentY = 0
theta = 0
For i = LBound(points) To UBound(points)
theta = theta + dtheta
x = points(i) * Cos(theta)
y = points(i) * Sin(theta)
pic.Line -(x, y)
Next i
End Sub
|
|
|
|
|
|