Simple 2D (X,Y) Plots in C#

If you are using C# for a scientific computing project you might have run into trouble trying to find a simple 2D plot library.  Below is an explanation of how to quickly make a simple library that you can use from another application to plot data from C#.

I'm assuming you already have a solution open in Visual Studio, and that this solution contains some C# code with data you'd like to plot.  Now follow these steps to get the data into a plot.


  1. Make a new project in Visual Studio
    1. File -> New -> Project
    2. Select Windows Form Application (we'll switch from an application to a library soon)

    3. In the "Solution:" box at the bottom select "Add to solution" and click OK
    4. A window with Form1.cs should open
  2. Add a Chart object to this project.
    1. Click View -> Toolbox
    2. Within the toolbox Windows select Data->Chart (if the chart option is not available in the toolbox select "choose items" and then, from .net framework components, select System.Web.UI.DataVisualization.Charting and System.Windows.Forms.DataVisualization.Charting)
    3. After selecting chart, use your mouse to click/drag a chart area onto the Form1 window.  It may look like a bar chart but you'll be able to use it in other ways.
  3. Make a method you can call to plot your data.
    1. Double-click the "Form1" item within the Visual Studio solution explorer.  Form1.cs should appear in the text editor window. 
    2. Add the Plot method below to the class Form1.
         public partial class Form1 : Form  
         {  
           public Form1()  
           {  
             InitializeComponent();  
           }  
           public void Plot(List<double> xs, List<double> ys, string name)  
           {  
             chart1.Series.Clear();  
             chart1.Series.Add(name);  
             chart1.Series.Last().ChartType = SeriesChartType.Point; //SeriesChartType.Line  
             for (int i = 0; i < xs.Count; i++) chart1.Series.Last().Points.AddXY(xs[i], ys[i]);  
             chart1.Series.Last().Points.DataBindXY(xs, "xs", ys, "ys");  
             //chart1.Series.Last().YAxisType = AxisType.Secondary;  
             //chart1.ChartAreas[0].AxisX.Minimum = 0.0; // xmin on plot  
             //chart1.ChartAreas[0].AxisY.IsReversed = true; //flip y order  
             //chart1.Size = new Size(900, 900); //resize plot  
           }  
         }  
      
  4. Change the project from an application to a library.
    1. Within the solution explorer, right click on the project and select "Properties"
    2. In the Application menu change the "Output type" to "Class Library"
    3. Build the solution and ensure there are no errors
  5. Use your new library.
    1. In the solution explorer, locate the project that you'd like to use this plot library from and right-click the "references" sub-item and left click "Add reference"
    2. In this reference manager add System.Windows.Forms under "Assemblies" and add your new library project under "Solution" and click OK
    3. Open a source file in that you'd like to use the plot library from and add the line "using System.Windows.Forms;" to the top and add a using statement for the namespace you just created in the plot library.
    4. Now just add the following three lines and run this application
             Form1 f = new Form1();  
             f.Plot(new List<double>() {0,1,2}, new List<double>() {0,1,4}, "square");  
             Application.Run(f);  
      
    5. If that code is reached at runtime you should see a plot containing 3 data points with x=0,1,2 and y=x^2.


Comments

Popular Posts