Winform中利用Graphics 画折线图

来源:互联网 发布:电子电路仿真软件下载 编辑:程序博客网 时间:2024/06/07 15:35
Form窗体控件
#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.pnlImage = new System.Windows.Forms.Panel();this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();// // pnlImage// this.pnlImage.AutoScroll = true;this.pnlImage.BackColor = System.Drawing.SystemColors.Control;this.pnlImage.ForeColor = System.Drawing.SystemColors.ControlLightLight;this.pnlImage.Location = new System.Drawing.Point(183, 39);this.pnlImage.Name = "pnlImage";this.pnlImage.Size = new System.Drawing.Size(640, 400);this.pnlImage.TabIndex = 0;// Form10// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1017, 522);this.Controls.Add(this.button1);this.Controls.Add(this.pnlImage);this.MaximizeBox = false;this.MinimizeBox = false;this.Name = "Form10";this.Text = "Form10";this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form10_FormClosed);this.Load += new System.EventHandler(this.Form10_Load);this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form2_Paint);this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form2_MouseMove);this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.Panel pnlImage;private System.Windows.Forms.Button button1;

后台代码:

private void Form2_Paint(object sender, PaintEventArgs e){//首先确定原点Point centerPoint = new Point(180, 340);//自定义一个带有箭头的画笔Pen pen = new Pen(Color.Black, 1);//带有箭头的画笔//pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;//得到当前窗体的Graphics对象Graphics g = e.Graphics;//画X轴//ng.DrawLine(pen, centerPoint, new Point(centerPoint.X + 650, centerPoint.Y));//画Y轴//g.DrawLine(pen, centerPoint, new Point(centerPoint.X, 40));#region// panel nai//绘制Y轴的点g.DrawLine(pen, centerPoint, new Point(centerPoint.X, 40));//画Y轴g.DrawString("(℃)", this.Font, Brushes.Black, new Point(155, 20));//底辺的线绘制g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(160, 350, 20, 5));g.DrawString("温度", this.Font, Brushes.Black, new Point(155, 360));for (int i = 0; i < 13; i++){//横线的绘制//g.DrawLine(pen, new Point(centerPoint.X - 3, centerPoint.Y - i * 25), new Point(830, centerPoint.Y - i * 25));g.DrawLine(Pens.Black, new Point(centerPoint.X - 3, centerPoint.Y - i * 25), new Point(centerPoint.X + 3, centerPoint.Y - i * 25));g.DrawString(string.Format("{0}", i * 10), this.Font, Brushes.Black, new PointF((centerPoint.X + 5) - 35, (centerPoint.Y - i * 25) - 5));}//绘制Y1轴的点//toPの度数的绘制g.DrawString("(℃)", this.Font, Brushes.Black, new Point(110, 20));//底辺的线绘制g.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(125, 350, 20, 5));g.DrawString("气压", this.Font, Brushes.Black, new Point(120, 360));g.DrawLine(pen, new Point(135, 341), new Point(135, 40));for (int i = 0; i < 13; i++){g.DrawLine(Pens.Black, new Point(132, centerPoint.Y - i * 25), new Point(138, centerPoint.Y - i * 25));g.DrawString(string.Format("{0}", i * 10), this.Font, Brushes.Black, new PointF(105, (centerPoint.Y - i * 25) - 5));}//绘制right Y2轴的点g.DrawString("(%)", this.Font, Brushes.Black, new Point(830, 20));//底辺的线绘制g.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(835, 350, 20, 5));g.DrawString("湿度", this.Font, Brushes.Black, new Point(830, 360));g.DrawLine(pen, new Point(826, 341), new Point(826, 40));for (int i = 0; i < 13; i++){g.DrawLine(Pens.Black, new Point(823, centerPoint.Y - i * 25), new Point(829, centerPoint.Y - i * 25));g.DrawString(string.Format("{0}", i * 10), this.Font, Brushes.Black, new PointF(834, (centerPoint.Y - i * 25) - 5));}//绘制right Y3轴的点g.DrawString("(m/s)", this.Font, Brushes.Black, new Point(870, 20));//底辺的线绘制g.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(875, 350, 20, 5));g.DrawString("风速", this.Font, Brushes.Black, new Point(870, 360));g.DrawLine(pen, new Point(875, 341), new Point(875, 40));for (int i = 0; i < 13; i++){g.DrawLine(Pens.Black, new Point(872, centerPoint.Y - i * 25), new Point(878, centerPoint.Y - i * 25));g.DrawString(string.Format("{0}", i * 10), this.Font, Brushes.Black, new PointF(883, (centerPoint.Y - i * 25) - 5));}pen.Dispose();g.Dispose();#endregion}


 

private void Form10_Load(object sender, EventArgs e){//GraphicsDrawPanel(6);#region //panel graphicsBitmap bmImage = new Bitmap(pnlImage.Width, pnlImage.Height);Graphics grpTemp = Graphics.FromImage(bmImage);//首先确定原点Point centerPoint1 = new Point(0, 301);grpTemp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;for (int i = 0; i < 13; i++){//横線を描くgrpTemp.DrawLine(new Pen(Color.Black, 1), new Point(centerPoint1.X, centerPoint1.Y - i * 25), new Point(830, centerPoint1.Y - i * 25));}//绘制X轴的点for (int i = 0; i < 12; i++){grpTemp.DrawLine(Pens.Black, new Point(centerPoint1.X + (i + 1) * 50 - 5, centerPoint1.Y), new Point(centerPoint1.X + (i + 1) * 50 - 5, centerPoint1.Y + 3));//テキストの「24時」を書くgrpTemp.DrawString((i + 1).ToString() + "月", this.Font, Brushes.Black, (centerPoint1.X + i * 50) + 12, centerPoint1.Y + 25);}//绘制年月日的点grpTemp.DrawString("2013/01/17", this.Font, Brushes.Black, 13, centerPoint1.Y + 10);grpTemp.DrawString("2013/01/18", this.Font, Brushes.Black, 464, centerPoint1.Y + 10);//计算十二个月销售额对应的坐标点double[] data = { 56.2, 66.3, 98.4, 34.5, 55.6, 87.3, 81.4, 33.3, 46.4, 34.6, 114.5, 80.4 };PointF[] dataPoint = new PointF[data.Length];for (int i = 0; i < data.Length; i++){float y = (float)(310 - data[i] * 2.5);float x = centerPoint1.X + (i + 1) * 50;PointF point = new PointF(x, y);dataPoint[i] = point;}//绘制十二个点的折线for (int i = 0; i < data.Length; i++){grpTemp.DrawRectangle(Pens.Black, dataPoint[i].X, dataPoint[i].Y, 2, 2);}//将十二个点串成线描绘出来。grpTemp.DrawLines(Pens.Black, dataPoint);grpTemp.Dispose();pnlImage.BackgroundImage = bmImage;pnlImage.Refresh();#endregion}


没什么技术含量。定好坐标开始画就行。细心点调试坐标即可。

效果图:


 

原创粉丝点击