Winform中利用Graphics 画折线图

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);

		}

		#endregion

		private 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 graphics
			Bitmap 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			
		}


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

效果图:


 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值