Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Draw a radar graph in Visual Basic .NET This example shows how to draw a radar graph in Visual Basic .NET. radar graph, graph, VB.NET Algorithms, Graphics, VB.NET

The program stores data in the m_RData array of Singles. The picCanvas PictureBox's Paint event handler draws the graph. First it converts the data into a series of PointF objects distance m_RData(i) away from the origin at different angles. It finds the largest value and uses it to give the Graphics object a scale transformation so the data will fit nicely on the PictureBox. The code then transforms the Graphics object to center the data (so the origin is in the middle of the PictureBox).

Next the code uses the DrawPolygon method to draw the transformed points. If the Draw Radii check box is checked, the code draws lines from (0, 0) to each transformed point. If the txtCircleIncrement text box contains a number, the program draws circles showing distances from the center.

Finally the code calls function PolygonArea to display the polygon's area. See Calculate a polygon's area in Visual Basic .NET for a desription of that function.

```' Draw the radar plot.
Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles _
picCanvas.Paint
' Generate coordinates for the points.
Dim pts(m_RData.Length - 1) As PointF
Dim theta As Single = 0
Dim dtheta As Single = CSng(2 * PI / (m_RData.Length - _
1))
For i As Integer = 0 To m_RData.Length - 1
pts(i) = New PointF( _
CSng(m_RData(i) * Cos(theta)), _
CSng(m_RData(i) * Sin(theta)))
theta += dtheta
Next i

' Find the largest value so we can scale nicely.
Dim max_r As Single = m_RData(0)
For i As Integer = 0 To m_RData.Length - 1
If max_r < m_RData(i) Then max_r = m_RData(i)
Next i

' Transform to scale the result.
Dim the_scale As Single = _
CSng(Min(picCanvas.ClientSize.Width, _
picCanvas.ClientSize.Height) / 2 / max_r)
e.Graphics.ScaleTransform( _
the_scale, the_scale, _
Drawing2D.MatrixOrder.Append)

' Transform to center the result.
e.Graphics.TranslateTransform( _
picCanvas.ClientSize.Width \ 2, _
picCanvas.ClientSize.Height \ 2, _
Drawing2D.MatrixOrder.Append)

' Draw.
e.Graphics.Clear(picCanvas.BackColor)

e.Graphics.FillPolygon(Brushes.LightBlue, pts)
Dim black_pen As New Pen(Color.Black, 0)
e.Graphics.DrawPolygon(black_pen, pts)
black_pen.Dispose()

Dim blue_pen As New Pen(Color.Blue, 0)
For i As Integer = 0 To pts.Length - 1
e.Graphics.DrawLine(blue_pen, _
New PointF(0, 0), pts(i))
Next i
blue_pen.Dispose()
End If

If IsNumeric(txtCircleIncrement.Text) Then
Dim circle_increment As Single = _
CSng(Val(txtCircleIncrement.Text))
Dim white_pen As New Pen(Color.White, 0)
For r As Single = circle_increment To max_r + 1 _
Step circle_increment
e.Graphics.DrawEllipse(white_pen, -r, -r, 2 * _
r, 2 * r)
Next r
white_pen.Dispose()
End If

' Calculate and display the area.
lblArea.Text = PolygonArea(pts).ToString("0.00")
End Sub```