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 »

Vector Charts using C# and GDI+


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



Use this method to create a simple 2D vector plot. This example simulates the electric field distribution from two opposite charges. The formula is not exact, I purposely change the denominator power to 0.8 (should be 1.5) in order to create a better arrow lines. You can create your own vector plot by replacing it with your vector function

Code Sample
--------------
usingSystem;

using System.Collections;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;



namespace VectorChart

{

public partial class Form1 : Form

{

private DataCollection dc;

private ChartStyle cs;



public Form1()

{

InitializeComponent();

SetStyle(ControlStyles.ResizeRedraw, true);

this.BackColor = Color.White;



//set Form1 size:

this.Width = 350;

this.Height = 350;

dc = new DataCollection();

cs = new ChartStyle(this);

cs.XLimMin = -8f;

cs.XLimMax = 8f;

cs.YLimMin = -8f;

cs.YLimMax = 8f;

cs.IsXGrid = false;

cs.IsYGrid = false;

cs.XTick = 4f;

cs.YTick = 4f;

cs.Title = "Vector Chart";

}



private void AddData(Graphics g)

{

dc.DataSeriesList.Clear();



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

{

for (int j = 1; j < 16; j++)

{

double x = i - 8;

double y = j - 8;

double a = 4;

float ex = (float)((x - a) / Math.Pow((x - a) * (x - a) + y * y, 0.8) -

(x + a) / Math.Pow((x + a) * (x + a) + y * y, 0.8));

float ey = (float)(y / Math.Pow((x - a) * (x - a) + y * y, 0.8) -

y / Math.Pow((x + a) * (x + a) + y * y, 0.8));

float em = (float)Math.Sqrt(ex * ex + ey * ey);



dc.AddVectorChart(g, cs, 0.7f*em, new PointF((float)x, (float)y), ex, ey);

}

}

}



protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.AntiAlias;

cs.ChartArea = this.ClientRectangle;

SetPlotArea(g);

cs.AddChartStyle(g);

AddData(g);

g.Dispose();

}

}

}

Here is the code of the AddVectorChart method:


public void AddVectorChart(Graphics g, ChartStyle cs, float length,

PointF center, float xValue, float yValue)

{

float sn = (float)(yValue / Math.Sqrt(xValue * xValue + yValue * yValue));

float cn = (float)(xValue / Math.Sqrt(xValue * xValue + yValue * yValue));

PointF pt1 = new PointF(center.X, center.Y);

PointF pt2 = new PointF(center.X + length * cn, center.Y + length * sn);



Pen aPen = new Pen(Color.DarkBlue, 3f);

aPen.EndCap = LineCap.ArrowAnchor;

g.DrawLine(aPen, cs.Point2D(pt1), cs.Point2D(pt2));

aPen.Dispose();

}



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.
Vector Charts using C# & GDI+  .  

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: Create two dimensional Chart using c#
Previous Resource: The Basic of the .NET Framework
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