Click here to Skip to main content
15,886,873 members
Articles / Multimedia / GDI+

A flexible charting library for .NET

Rate me:
Please Sign up or sign in to vote.
4.70/5 (1,112 votes)
6 Jun 200730 min read 8.9M   180.2K   2.1K  
Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
//============================================================================
//ZedGraph demo code
//The code contained in this file (only) is released into the public domain, so you
//can copy it into your project without any license encumbrance.  Note that
//the actual ZedGraph library code is licensed under the LGPL, which is not
//public domain.
//
//This file is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//=============================================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ZedGraph;

namespace WindowsApplication2
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private ZedGraph.ZedGraphControl zg1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.zg1 = new ZedGraph.ZedGraphControl();
			this.SuspendLayout();
			// 
			// zg1
			// 
			this.zg1.IsAutoScrollRange = false;
			this.zg1.IsEnableHPan = true;
			this.zg1.IsEnableHZoom = true;
			this.zg1.IsEnableVPan = true;
			this.zg1.IsEnableVZoom = true;
			this.zg1.IsPrintFillPage = true;
			this.zg1.IsPrintKeepAspectRatio = true;
			this.zg1.IsScrollY2 = false;
			this.zg1.IsShowContextMenu = true;
			this.zg1.IsShowCopyMessage = true;
			this.zg1.IsShowCursorValues = false;
			this.zg1.IsShowHScrollBar = false;
			this.zg1.IsShowPointValues = false;
			this.zg1.IsShowVScrollBar = false;
			this.zg1.IsZoomOnMouseCenter = false;
			this.zg1.Location = new System.Drawing.Point(8, 8);
			this.zg1.Name = "zg1";
			this.zg1.PanButtons = System.Windows.Forms.MouseButtons.Left;
			this.zg1.PanButtons2 = System.Windows.Forms.MouseButtons.Middle;
			this.zg1.PanModifierKeys2 = System.Windows.Forms.Keys.None;
			this.zg1.PointDateFormat = "g";
			this.zg1.PointValueFormat = "G";
			this.zg1.ScrollMaxX = 0;
			this.zg1.ScrollMaxY = 0;
			this.zg1.ScrollMaxY2 = 0;
			this.zg1.ScrollMinX = 0;
			this.zg1.ScrollMinY = 0;
			this.zg1.ScrollMinY2 = 0;
			this.zg1.Size = new System.Drawing.Size(664, 400);
			this.zg1.TabIndex = 1;
			this.zg1.ZoomButtons = System.Windows.Forms.MouseButtons.Left;
			this.zg1.ZoomButtons2 = System.Windows.Forms.MouseButtons.None;
			this.zg1.ZoomModifierKeys = System.Windows.Forms.Keys.None;
			this.zg1.ZoomModifierKeys2 = System.Windows.Forms.Keys.None;
			this.zg1.ZoomStepFraction = 0.1;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(680, 414);
			this.Controls.Add(this.zg1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Resize += new System.EventHandler(this.Form1_Resize);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run( new Form1() );
		}

		private void Form1_Load( object sender, System.EventArgs e )
		{
			// Get a reference to the GraphPane instance in the ZedGraphControl
			GraphPane myPane = zg1.GraphPane;

			// Set the titles and axis labels
			myPane.Title = "Demonstration of Dual Y Graph";
			myPane.XAxis.Title = "Time, Days";
			myPane.YAxis.Title = "Parameter A";
			myPane.Y2Axis.Title = "Parameter B";
			
			// Make up some data points based on the Sine function
			PointPairList list = new PointPairList();
			PointPairList list2 = new PointPairList();
			for ( int i=0; i<36; i++ )
			{
				double x = (double) i * 5.0;
				double y = Math.Sin( (double) i * Math.PI / 15.0 ) * 16.0;
				double y2 = y * 13.5;
				list.Add( x, y );
				list2.Add( x, y2 );
			}

			// Generate a red curve with diamond symbols, and "Alpha" in the legend
			LineItem myCurve = myPane.AddCurve( "Alpha",
				list, Color.Red, SymbolType.Diamond );
			// Fill the symbols with white
			myCurve.Symbol.Fill = new Fill( Color.White );

			// Generate a blue curve with circle symbols, and "Beta" in the legend
			myCurve = myPane.AddCurve( "Beta",
				list2, Color.Blue, SymbolType.Circle );
			// Fill the symbols with white
			myCurve.Symbol.Fill = new Fill( Color.White );
			// Associate this curve with the Y2 axis
			myCurve.IsY2Axis = true;

			// Show the x axis grid
			myPane.XAxis.IsShowGrid = true;

			// Make the Y axis scale red
			myPane.YAxis.ScaleFontSpec.FontColor = Color.Red;
			myPane.YAxis.TitleFontSpec.FontColor = Color.Red;
			// turn off the opposite tics so the Y tics don't show up on the Y2 axis
			myPane.YAxis.IsOppositeTic = false;
			myPane.YAxis.IsMinorOppositeTic = false;
			// Don't display the Y zero line
			myPane.YAxis.IsZeroLine = false;
			// Align the Y axis labels so they are flush to the axis
			myPane.YAxis.ScaleAlign = AlignP.Inside;
			// Manually set the axis range
			myPane.YAxis.Min = -30;
			myPane.YAxis.Max = 30;

			// Enable the Y2 axis display
			myPane.Y2Axis.IsVisible = true;
			// Make the Y2 axis scale blue
			myPane.Y2Axis.ScaleFontSpec.FontColor = Color.Blue;
			myPane.Y2Axis.TitleFontSpec.FontColor = Color.Blue;
			// turn off the opposite tics so the Y2 tics don't show up on the Y axis
			myPane.Y2Axis.IsOppositeTic = false;
			myPane.Y2Axis.IsMinorOppositeTic = false;
			// Display the Y2 axis grid lines
			myPane.Y2Axis.IsShowGrid = true;
			// Align the Y2 axis labels so they are flush to the axis
			myPane.Y2Axis.ScaleAlign = AlignP.Inside;

			// Fill the axis background with a gradient
			myPane.AxisFill = new Fill( Color.White, Color.LightGray, 45.0f );

			// Add a text box with instructions
			TextItem text = new TextItem(
				"Zoom: left mouse & drag\nPan: middle mouse & drag\nContext Menu: right mouse",
				0.05f, 0.95f, CoordType.AxisFraction, AlignH.Left, AlignV.Bottom );
			text.FontSpec.StringAlignment = StringAlignment.Near;
			myPane.GraphItemList.Add( text );

			// Enable scrollbars if needed
			zg1.IsShowHScrollBar = true;
			zg1.IsShowVScrollBar = true;
			zg1.IsAutoScrollRange = true;
			zg1.IsScrollY2 = true;

			// OPTIONAL: Show tooltips when the mouse hovers over a point
			zg1.IsShowPointValues = true;
			zg1.PointValueEvent += new ZedGraphControl.PointValueHandler( MyPointValueHandler );

			// OPTIONAL: Add a custom context menu item
			zg1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler( MyContextMenuBuilder );

			// OPTIONAL: Handle the Zoom Event
			zg1.ZoomEvent += new ZedGraphControl.ZoomEventHandler( MyZoomEvent );

			// Size the control to fit the window
			SetSize();

			// Tell ZedGraph to calculate the axis ranges
			// Note that you MUST call this after enabling IsAutoScrollRange, since AxisChange() sets
			// up the proper scrolling parameters
			zg1.AxisChange();
			// Make sure the Graph gets redrawn
			zg1.Invalidate();

		}

		/// <summary>
		/// On resize action, resize the ZedGraphControl to fill most of the Form, with a small
		/// margin around the outside
		/// </summary>
		private void Form1_Resize(object sender, EventArgs e)
		{
			SetSize();
		}

		private void SetSize()
		{
			// Leave a small margin around the outside of the control
			Size size = new Size( this.Size.Width - 25,
				this.Size.Height - 50 );
			zg1.Size = size;
		}

		/// <summary>
		/// Display customized tooltips when the mouse hovers over a point
		/// </summary>
		private string MyPointValueHandler( ZedGraphControl control, GraphPane pane,
						CurveItem curve, int iPt )
		{
			// Get the PointPair that is under the mouse
			PointPair pt = curve[iPt];

			return curve.Label + " is " + pt.Y.ToString("f2") + " units at " + pt.X.ToString("f1") + " days";
		}

		/// <summary>
		/// Customize the context menu by adding a new item to the end of the menu
		/// </summary>
		private void MyContextMenuBuilder( ZedGraphControl control, ContextMenu menu, Point mousePt )
		{
			MenuItem menuItem = new MenuItem();
			menuItem.Index = menu.MenuItems.Count + 1;
			menuItem.Text = "Add a new Beta Point";
			menuItem.Click += new System.EventHandler( AddBetaPoint );

			menu.MenuItems.Add( menuItem );
		}

		/// <summary>
		/// Handle the "Add New Beta Point" context menu item.  This finds the curve with
		/// the CurveItem.Label = "Beta", and adds a new point to it.
		/// </summary>
		private void AddBetaPoint( object sender, EventArgs args )
		{
			// Get a reference to the "Beta" curve IPointListEdit
			IPointListEdit ip = zg1.GraphPane.CurveList["Beta"].Points as IPointListEdit;
			if ( ip != null )
			{
				double x = ip.Count * 5.0;
				double y = Math.Sin( ip.Count * Math.PI / 15.0 ) * 16.0 * 13.5;
				ip.Add( x, y );
				zg1.AxisChange();
				zg1.Refresh();
			}
		}

		// Respond to a Zoom Event
		private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,
					ZoomState newState)
		{
			// Here we get notification everytime the user zooms
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions