Mark A.
Mark A.

Reputation: 243

VB.NET Cartesian Coordinate System

I'd like to make a Cartesian Coordinate System in a Windows form and be able to plot (x,y) coordinates in it.

How do i do this? I already did my research but unfortunately i only land on "charts" and not the Cartesian plane.

Any links regarding my problem will help ... thanks ...

Upvotes: 1

Views: 7633

Answers (3)

David Rinck
David Rinck

Reputation: 6966

.NET has a charting library, but there are a few open source projects that do this sort of thing quite well. If you want to plot coordinates Zedgraph makes this relatively easy and is quite flexible.

ZedGraph Example

Dynamic Data Display is also worth looking at, but it is WPF, not Windows Forms

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

You should create a custom UserControl and use the Paint even to draw on the surface of the control. The Paint event provides you with a Graphics object which you can use to draw the graph. The big thing to know, however, is that you will need to swap your Y axis. In windows, the top-left of the screen is 0,0 rather than the bottom-left.

So, for instance, the following code will draw the x and y axis of a graph on a contorl:

Public Class CartesianGraph
    Public Property BottomLeftExtent() As Point
        Get
            Return _bottomLeftExtent
        End Get
        Set(ByVal value As Point)
            _bottomLeftExtent = value
        End Set
    End Property
    Private _bottomLeftExtent As Point = New Point(-100, -100)


    Public Property TopRightExtent() As Point
        Get
            Return _topRightExtent
        End Get
        Set(ByVal value As Point)
            _topRightExtent = value
        End Set
    End Property
    Private _topRightExtent As Point = New Point(100, 100)


    Private Sub CartesianGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim extentHeight As Integer = _topRightExtent.Y - _bottomLeftExtent.Y
        Dim extentWidth As Integer = _topRightExtent.X - _bottomLeftExtent.X
        If (extentHeight <> 0) And (extentWidth <> 0) Then
            If (_bottomLeftExtent.Y <= 0) And (_topRightExtent.Y >= 0) Then
                Dim xAxis As Integer = e.ClipRectangle.Height - (_bottomLeftExtent.Y * -1 * e.ClipRectangle.Height \ extentHeight)
                e.Graphics.DrawLine(New Pen(ForeColor), 0, xAxis, e.ClipRectangle.Width, xAxis)
            End If
            If (_bottomLeftExtent.X <= 0) And (_topRightExtent.X >= 0) Then
                Dim yAxis As Integer = e.ClipRectangle.Width * _bottomLeftExtent.X * -1 \ extentWidth
                e.Graphics.DrawLine(New Pen(ForeColor), yAxis, 0, yAxis, e.ClipRectangle.Height)
            End If
        End If
    End Sub
End Class

Upvotes: 3

Heinzi
Heinzi

Reputation: 172200

In WinForms, you can use a PictureBox control and then draw on it using primitives such as DrawLine, DrawEllipse, etc. The following SO question contains an example:

In WPF, you can use a Canvas control similarly:

If you want automatic axes and labeling, Charts are indeed the way to go. For your use case, a point chart seems like the right solution:

Upvotes: 3

Related Questions