usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Draw Graphs in C# - Windows Forms Tutorial



Understanding Cartesian Coordinates vs. Window Forms in C#

In this tutorial, we'll explore how to draw graphs in C# using the System.Drawing library. By creating a Cartesian coordinate plane in C# Windows Forms, you can plot mathematical functions or custom data points. Unlike charts, which focus on statistics, a C# graph drawing shows the relationship between values across the coordinate system.

The Cartesian Plane is the usual graph surface we are taught in school.
It consists of the x and y axes.

In C#, the Window's Form would represent something like a graph book; and this graph book is plotted or drawn on using code.

By default, Windows Forms treats the top-left corner as '(0,0)', which means the Y-axis increases downward. To make graphs behave like traditional math diagrams, you can invert the Y-axis.

Cartesian graph compared to C# Windows Form coordinate plane.
Figure: Cartesian graph compared to C# Windows Form coordinate plane.

Drawing a Cartesian Plane in C# Windows Forms

To represent data clearly, we'll build a Cartesian coordinate system in C#. The X-axis and Y-axis intersect at the origin, and we'll also add grid lines for better readability.

In C#, drawing is done using the System.Drawing library.
With C#, drawing is usually done in the paint method of a window form or picture box.
Remember, there is a subtle difference between the way we use graphs and the C# windows form:
The y-axis of the C# windows form is measured from the top and increases as you move downwards.

Create a new C# Windows Forms Application project ; call it Dymetric_CS.
Type out the adjoining C# algorithm to see what the windows form looks like.


What You've Learnt on Graphs and C# Windows Forms

In this tutorial, we've explored how to draw graphs in C# using the System.Drawing library. By creating a Cartesian coordinate plane in C# Windows Forms, you can plot mathematical functions or custom data points. Unlike charts, which focus on statistics, a C# graph drawing shows the relationship between values across the coordinate system.

Key Takeaways on Graphs and C# Windows Forms

  • Graphs in C# can be created using System.Drawing and the Paint event.
  • You can draw Cartesian axes in Windows Forms and invert the Y-axis for correct orientation.
  • By looping through values, you can plot functions in C# and visualize data clearly.

With these basics, you can expand to more advanced graph drawing in C#, including grid lines, labels, and multiple functions.







C# Graph Code for Form Class

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Dymetric_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Set window position, width and height
            Rectangle screen_rect = Screen.PrimaryScreen.Bounds;
            this.SetDesktopBounds(0, 0, screen_rect.Width, screen_rect.Height);

            // Set a display text
            this.Text = "usingMaths.com";

            // Set a background colour
            this.BackColor = System.Drawing.Color.Orange;

            // Set an icon image
            String path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
            path = new Uri(path).LocalPath;
            try
            {
                this.Icon = new Icon(path + "\\usingMaths.ico");
            }
            catch(System.IO.FileNotFoundException ex)
            {
                // Well, just go on and use default pic
            }
            
        }
    }
}

Important: Get the logo image(.ico) used in the code here and save it to the same folder(directory) as your code files.
For Visual Studio, this folder should be C:\Users\user_name\Documents\Visual Studio 20**\Projects\Dymetric_VB\Dymetric_VB\bin\Debug.




C# Drawing Tutorial Reference

For a thorough explanation of drawing graphics in C#, please see the following links:

Drawing Graphics in C Sharp      - Step by step guide on using Visual Studio for C# graphics.

Using Bitmaps for Persistent Graphics in C Sharp      - Shows how to draw from Bitmaps to Windows Forms in C#.

Graphics for C# 6.0 Users      - largely apt for C# beginners; Knock your head out!!




<< PreviousNext >>