1

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 ...

MarkJ
  • 30,070
  • 5
  • 68
  • 111
Mark A.
  • 243
  • 6
  • 21
  • 1
    The word "cartesian" won't help you in your searching. just use the term "x-y plot". Cartesian is implicity and rarely mentioned. There are plenty of examples if you google it. –  May 07 '12 at 15:28
  • @jaime seemed to me your edit turned this into a question about simple x-y scatter plots. I think the OP wants to be able to create custom 2D drawings – MarkJ May 07 '12 at 17:28
  • @user do you need to create custom 2D drawings, or just a standard x-y scatter plot ? – MarkJ May 07 '12 at 17:30

3 Answers3

3

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Thank you for providing me this sample, but i'm really new to this Paint event. Didn't knew that a simple line crossing each other would take this much codes. I'll study on this Paint event and Graphics object and use this as a reference ... Thanks again. – Mark A. May 07 '12 at 16:46
  • Drawing the line is easy. It's the calculating of where the line goes that takes all the thought and effort. As you can see in my example, I provided properties to set the outer extents of the graph, so my code is having to calculate the scale of the graph. If you just had a simple one pixel on the screen per one point on the graph, the code would be much simpler. – Steven Doggart May 07 '12 at 16:59
  • A custom user control seems excessive. Why not just draw onto a PictureBox like Heinzi says in his answer? – MarkJ May 07 '12 at 17:23
  • A user control isn't significantly more overhead than a PictureBox anyway, so why not make a user control and encapsulate the logic in a nice reusable package. Either way is perfectly acceptable, but I don't think that's really the issue. The harder problem to solve is, how do you draw the graph and plot the points, not what kind of surface should you draw it on. – Steven Doggart May 07 '12 at 18:43
3

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:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

.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

David Rinck
  • 6,637
  • 4
  • 45
  • 60