Draw Graphs in C#
In this tutorial, you will learn how to draw and plot Cartesian graphs in C# using Windows Forms. We will build a simple graph application step by step, create a coordinate system, and use the System.Drawing library to display mathematical functions on a 2D graph.
This guide is designed for senior secondary and beginner programmers who want to understand C# graphics programming and learn how graph plotting works inside a Windows Forms application.
By the end of this tutorial, you will be able to:
- Create a coordinate plane in C#
- Draw X and Y axes using System.Drawing
- Plot a line graph from mathematical values
- Build a simple C# graph program from scratch
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.
Creating a Coordinate System in C#
Before plotting any mathematical function, we must first create a coordinate system.
In a typical Windows Forms application, drawing is done inside the Paint event using the Graphics object. The default coordinate system starts at the top-left corner of the form, so we need to adjust positioning to simulate a Cartesian plane.
To create the coordinate axes:
- Determine the center of the form
- Draw the horizontal X-axis
- Draw the vertical Y-axis
- Optionally add scale markings
This forms the foundation for 2D graph plotting in C#.
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.
Why Learn Graph Drawing in C#?
Learning how to draw graphs in C# strengthens your understanding of:
- Coordinate geometry
- Mathematical modeling
- Event-driven programming
- .NET graphical application development
It also builds a foundation for more advanced topics such as data visualization, simulations, and custom UI components.
For students studying mathematics and programming, combining both subjects through C# graph plotting provides practical and visual learning.
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.
Summary: Graphs in C#
In this tutorial, you learned how to:
- Draw Cartesian graphs in C#
- Create a coordinate plane in Windows Forms
- Use System.Drawing for graphics programming
- Plot a line graph from mathematical data
- Build a simple C# graph application
With these core principles, you can now experiment with different mathematical functions and expand your C# graphics programming skills.
C# Graph Code for Form Class
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!!