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;
}
}
}
|
No responses found. Be the first to respond and make money from revenue sharing program.
|