public partial class FormReport : Form
{
Point centerPoint = new Point(50, 360);//中心点位置
int iXLength = 400;//X轴的长度
int iYLength = 300;//Y轴的长度
int iXStep = 30;//每个月份X轴的步长
int iKeduLength = 10;//刻度长度
int iYStep = 20;//Y轴步长
int[] reportValue = { 10, 40, 52, 68, 94, 26, 54, 30, 74, 50, 58, 99 };
public FormReport()
{
InitializeComponent();
}
private void FormReport_MouseMove(object sender, MouseEventArgs e)
{
this.Text = String.Format("x = {0},y = {1}", e.X, e.Y);
}
private void FormReport_Paint(object sender, PaintEventArgs e)
{
//得到窗体的绘制对象
Graphics g = e.Graphics;
Pen zuobiaoPen = new Pen(Color.Black, 1);
zuobiaoPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
//绘制X轴
g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X + iXLength, centerPoint.Y));
//绘制Y轴
g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X, centerPoint.Y - iYLength));
zuobiaoPen.Dispose();
//绘制X坐标刻度
for (int i = 1; i <= 12;i++ )
{
Point p1 = new Point(centerPoint.X + i * iXStep, centerPoint.Y - iKeduLength);
Point p2 = new Point(centerPoint.X + i * iXStep, centerPoint.Y);
g.DrawLine(Pens.Black, p1, p2);
Point ptText = new Point(p2.X - 10, p2.Y);
g.DrawString(String.Format("{0}月", i), this.Font, Brushes.Black, ptText);
}
//绘制Y坐标刻度
for (int i = 1; i <= 10; i++)
{
Point p1 = new Point(centerPoint.X + iKeduLength, centerPoint.Y - i * iYStep);
Point p2 = new Point(centerPoint.X, centerPoint.Y - i * iYStep);
g.DrawLine(Pens.Black, p1, p2);
Point ptText = new Point(p2.X - 30, p2.Y-10);
g.DrawString(String.Format("{0}万", i*10), this.Font, Brushes.Black, ptText);
}
DrawReport(g);
}
void DrawReport(Graphics g)
{
Point [] pts = new Point [reportValue.Length];
const int width = 2;
for (int i = 0; i < reportValue.Length;i++ )
{
pts[i].X = centerPoint.X+(i + 1) * iXStep;
pts[i].Y = centerPoint.Y - (int)(reportValue[i] / 10.0 * iYStep);
Point ellipseCenter = new Point(pts[i].X - width, pts[i].Y - width);
g.DrawEllipse(Pens.Black, new Rectangle(ellipseCenter, new Size(width * 2, width*2)));
}
g.DrawLines(Pens.Black, pts);
}
}
{
Point centerPoint = new Point(50, 360);//中心点位置
int iXLength = 400;//X轴的长度
int iYLength = 300;//Y轴的长度
int iXStep = 30;//每个月份X轴的步长
int iKeduLength = 10;//刻度长度
int iYStep = 20;//Y轴步长
int[] reportValue = { 10, 40, 52, 68, 94, 26, 54, 30, 74, 50, 58, 99 };
public FormReport()
{
InitializeComponent();
}
private void FormReport_MouseMove(object sender, MouseEventArgs e)
{
this.Text = String.Format("x = {0},y = {1}", e.X, e.Y);
}
private void FormReport_Paint(object sender, PaintEventArgs e)
{
//得到窗体的绘制对象
Graphics g = e.Graphics;
Pen zuobiaoPen = new Pen(Color.Black, 1);
zuobiaoPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
//绘制X轴
g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X + iXLength, centerPoint.Y));
//绘制Y轴
g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X, centerPoint.Y - iYLength));
zuobiaoPen.Dispose();
//绘制X坐标刻度
for (int i = 1; i <= 12;i++ )
{
Point p1 = new Point(centerPoint.X + i * iXStep, centerPoint.Y - iKeduLength);
Point p2 = new Point(centerPoint.X + i * iXStep, centerPoint.Y);
g.DrawLine(Pens.Black, p1, p2);
Point ptText = new Point(p2.X - 10, p2.Y);
g.DrawString(String.Format("{0}月", i), this.Font, Brushes.Black, ptText);
}
//绘制Y坐标刻度
for (int i = 1; i <= 10; i++)
{
Point p1 = new Point(centerPoint.X + iKeduLength, centerPoint.Y - i * iYStep);
Point p2 = new Point(centerPoint.X, centerPoint.Y - i * iYStep);
g.DrawLine(Pens.Black, p1, p2);
Point ptText = new Point(p2.X - 30, p2.Y-10);
g.DrawString(String.Format("{0}万", i*10), this.Font, Brushes.Black, ptText);
}
DrawReport(g);
}
void DrawReport(Graphics g)
{
Point [] pts = new Point [reportValue.Length];
const int width = 2;
for (int i = 0; i < reportValue.Length;i++ )
{
pts[i].X = centerPoint.X+(i + 1) * iXStep;
pts[i].Y = centerPoint.Y - (int)(reportValue[i] / 10.0 * iYStep);
Point ellipseCenter = new Point(pts[i].X - width, pts[i].Y - width);
g.DrawEllipse(Pens.Black, new Rectangle(ellipseCenter, new Size(width * 2, width*2)));
}
g.DrawLines(Pens.Black, pts);
}
}