C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Create two dimensional Chart using c#


Posted Date: 13 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: LaljiMember Level: Diamond    
Rating: 1 out of 5Points: 2



Create a 2D X-Y chart using C#. Start off with a new C# Windows Application project and call it Example3_1. The following is the code listing of Form1 class that generates a simple X-Y chart with two lines, representing Sine and Cosine functions respectively
using System;

using System.Drawing;

using System.Windows.Forms;



namespace Example3_1

{

public partial class Form1 : Form

{

// Define the drawing area

private Rectangle PlotArea;

// Unit defined in world coordinate system :

private float xMin = 0f;

private float xMax = 6;

private float yMin = -1.1f;

private float yMax = 1.1f;

private int nPoints = 61;

// Unit in pixel:

private int offset = 30;



public Form1()

{

InitializeComponent();

This.BackColor = Color.White;

this.SetStyle(ControlStyles.ResizeRedraw, true);

}



protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;



// Calculate the location and size of the plot area

// within which we want to draw the graphics:

Rectangle ChartArea = ClientRectangle;

PlotArea = new Rectangle(ChartArea.Location,

ChartArea.Size);

PlotArea.Inflate(-offset, -offset);



//Draw PlotArea:

g.DrawRectangle(Pens.Black, PlotArea);



// Generate Sine and Cosine data points to plot:

PointF[] pt1 = new PointF[nPoints];

PointF[] pt2 = new PointF[nPoints];

for (int i = 0; i < nPoints; i++)

{

pt1[i] = new PointF(i / 5.0f, (float)Math.Sin(i/5.0f));

pt2[i] = new PointF(i / 5.0f, (float)Math.Cos(i/5.0f));

}



// Draw Sine and Cosine lines:

for (int i = 1; i < nPoints; i++)

{

g.DrawLine(Pens.Blue, Point2D(pt1[i - 1]),

Point2D(pt1[i]));

g.DrawLine(Pens.Red, Point2D(pt2[i - 1]),

Point2D(pt2[i]));

}

g.Dispose();

}



private PointF Point2D(PointF ptf)

{

PointF aPoint = new PointF();

if (ptf.X < xMin || ptf.X > xMax ||

ptf.Y < yMin || ptf.Y > yMax)

{

ptf.X = Single.NaN;

ptf.Y = Single.NaN;

}

aPoint.X = PlotArea.X + (ptf.X - xMin) *

PlotArea.Width / (xMax - xMin);

aPoint.Y = PlotArea.Bottom - (ptf.Y - yMin) *

PlotArea.Height / (yMax - yMin);

return aPoint;

}

}

}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Two dimensional Chart  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: System Net Mail
Previous Resource: Vector Charts using C# and GDI+
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use