在c#winform里面如何画波形图???

milo_c 2013-11-28 08:36:04
我要在c#中画一张以时间为横轴,TextBox中的值为纵轴的波形图,这个该怎么画?
...全文
1343 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
学习了,多谢楼主!我最近也是为此苦恼呢!只不过我要画的是动态波形!有没有相关案例哦,,,我研究下,谢谢
skcry 2014-08-12
  • 打赏
  • 举报
回复
学习了
milo_c 2013-11-28
  • 打赏
  • 举报
回复
引用 11 楼 u012592437 的回复:
[quote=引用 10 楼 u010119353 的回复:] [quote=引用 9 楼 u012592437 的回复:] 完整代码:Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(442, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(105, 21); this.textBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(553, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(640, 326); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); } } }
嗯,真的很谢谢你。答的很详细。我还有几个问题请教一下,第一,这个波形显示能做到实时的吗?第二,能使画出的图小一点吗?第三,能使图像由左像右画吗? 谢谢~[/quote] 一、 只要你有数据源,更新数据的时候把button1_Click中的代码替换就可以了. 你可以先用随机数试试效果 二、 这段代码是在窗口中绘图.绘图大小使用窗口大小计算出的 points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; 你可以改进代码,用一个PictureBox代替窗口,将Form1_Paint事件过程邦定到PictureBox的Paint事件处理过程:选中PictureBox,在属性表上方选择事件,然后在Paint事件中创建处理过程。 然后,这段代码需要改造为: points[i].X=PictureBox.Width/n*i; points[i].Y=PictureBox.Height-dataPoint.V*PictureBox.Height/vMax; 三、 points[i].X=this.Width/n*i; 改为 points[i].X=this.Width-this.Width/n*i; 绘图的方向就变了。[/quote] 谢谢~~~,最后一个问题,怎么改就能在picturebox中画两条线,横坐标都是时间,纵坐标分别来来自两个TextBox? 为什么TextBox中输入double型数的时候,好像画的不对。谢谢!
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
引用 10 楼 u010119353 的回复:
[quote=引用 9 楼 u012592437 的回复:] 完整代码:Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(442, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(105, 21); this.textBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(553, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(640, 326); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); } } }
嗯,真的很谢谢你。答的很详细。我还有几个问题请教一下,第一,这个波形显示能做到实时的吗?第二,能使画出的图小一点吗?第三,能使图像由左像右画吗? 谢谢~[/quote] 一、 只要你有数据源,更新数据的时候把button1_Click中的代码替换就可以了. 你可以先用随机数试试效果 二、 这段代码是在窗口中绘图.绘图大小使用窗口大小计算出的 points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; 你可以改进代码,用一个PictureBox代替窗口,将Form1_Paint事件过程邦定到PictureBox的Paint事件处理过程:选中PictureBox,在属性表上方选择事件,然后在Paint事件中创建处理过程。 然后,这段代码需要改造为: points[i].X=PictureBox.Width/n*i; points[i].Y=PictureBox.Height-dataPoint.V*PictureBox.Height/vMax; 三、 points[i].X=this.Width/n*i; 改为 points[i].X=this.Width-this.Width/n*i; 绘图的方向就变了。
milo_c 2013-11-28
  • 打赏
  • 举报
回复
引用 9 楼 u012592437 的回复:
完整代码:Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(442, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(105, 21); this.textBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(553, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(640, 326); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); } } }
嗯,真的很谢谢你。答的很详细。我还有几个问题请教一下,第一,这个波形显示能做到实时的吗?第二,能使画出的图小一点吗?第三,能使图像由左像右画吗? 谢谢~
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
完整代码:Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(442, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(105, 21); this.textBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(553, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(640, 326); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); } } }
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
呵呵,上面的图没有Load的事件邦定 另外,操作是,输入一个值,点右侧的按钮添加更新数据。 你需要顺次灌入数据。
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
引用 6 楼 u010119353 的回复:
[quote=引用 5 楼 u012592437 的回复:] [quote=引用 3 楼 u010119353 的回复:] [quote=引用 1 楼 u012592437 的回复:] public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); }
不错~图像是画在什么上面的?什么控件?[/quote] 我这个直接划在窗口上了 你可以换成PictureBox。然后把绘图代码放入PictureBox的Paint事件中。 响应测量坐标的依据也由 this.Height,this.Width,变成pictureBox.Height pictureBox.Width[/quote] 我直接把你代码放进去,点button怎么什么反应都没有?[/quote] Form1_Paint和button1_Click是两个事件处理过程 你在属性表中为窗口创建这两个事件并指向这两个过程才可以.
milo_c 2013-11-28
  • 打赏
  • 举报
回复
引用 5 楼 u012592437 的回复:
[quote=引用 3 楼 u010119353 的回复:] [quote=引用 1 楼 u012592437 的回复:] public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); }
不错~图像是画在什么上面的?什么控件?[/quote] 我这个直接划在窗口上了 你可以换成PictureBox。然后把绘图代码放入PictureBox的Paint事件中。 响应测量坐标的依据也由 this.Height,this.Width,变成pictureBox.Height pictureBox.Width[/quote] 我直接把你代码放进去,点button怎么什么反应都没有?
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
引用 3 楼 u010119353 的回复:
[quote=引用 1 楼 u012592437 的回复:] public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); }
不错~图像是画在什么上面的?什么控件?[/quote] 我这个直接划在窗口上了 你可以换成PictureBox。然后把绘图代码放入PictureBox的Paint事件中。 响应测量坐标的依据也由 this.Height,this.Width,变成pictureBox.Height pictureBox.Width
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
不知道是不是这个意思.一些细节您自己完善一下. 绘制图表主要处理好坐标点. 你可能要根据数据动态更新最大值和最小值. 绘图的方式就是刷新要绘图的控件,在控件的Paint里进行重绘。 例子中有画圆、填充圆、画线,使用画布、画刷和画笔的代码。 注释不是很得体,见谅。
milo_c 2013-11-28
  • 打赏
  • 举报
回复
引用 1 楼 u012592437 的回复:
public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); }
不错~图像是画在什么上面的?什么控件?
飞小猪 2013-11-28
  • 打赏
  • 举报
回复
gdi+画移动波形图,类似任务管理器CPU使用率的效果 http://download.csdn.net/detail/xiaozhu39505/1820839
capricciosoft 2013-11-28
  • 打赏
  • 举报
回复
public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); }
milo_c 2013-11-28
  • 打赏
  • 举报
回复
引用 12 楼 u010119353 的回复:
[quote=引用 11 楼 u012592437 的回复:] [quote=引用 10 楼 u010119353 的回复:] [quote=引用 9 楼 u012592437 的回复:] 完整代码:Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(442, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(105, 21); this.textBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(553, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(640, 326); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 数据点结构 /// </summary> struct DataPoint { public int V; public DateTime T; } private const int n = 20;//点数 private int vMax = 1;//最大值 private const int r = 6;//点半径 private DataPoint[] data = new DataPoint[n];//数据点 private Point[] points = new Point[n];//坐标点 /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < n - 1; i++) { data[n - i - 1] = data[n - i - 2]; } data[0].T = DateTime.Now; try { data[0].V = int.Parse(textBox1.Text); } catch { data[0].V = 0; } if (data[0].V > vMax) { vMax = data[0].V;//更新最大值 } this.Refresh();//刷新,导致窗口重绘 } //绘图函数 private void Form1_Paint(object sender, PaintEventArgs e) { for (int i=0; i < n; i++) { DataPoint dataPoint=data[i]; points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; Rectangle rect = new Rectangle(points[i].X - r, points[i].Y - r, r + r, r + r); e.Graphics.FillEllipse(new SolidBrush(Color.Green), rect); e.Graphics.DrawEllipse(new Pen(Color.Red, 1), rect); e.Graphics.DrawString(dataPoint.T.ToShortTimeString(), this.Font, new SolidBrush(Color.Black), points[i].X, this.Height -100); } e.Graphics.DrawLines(new Pen(Color.Red, 1), points); } } }
嗯,真的很谢谢你。答的很详细。我还有几个问题请教一下,第一,这个波形显示能做到实时的吗?第二,能使画出的图小一点吗?第三,能使图像由左像右画吗? 谢谢~[/quote] 一、 只要你有数据源,更新数据的时候把button1_Click中的代码替换就可以了. 你可以先用随机数试试效果 二、 这段代码是在窗口中绘图.绘图大小使用窗口大小计算出的 points[i].X=this.Width/n*i; points[i].Y=this.Height-dataPoint.V*this.Height/vMax; 你可以改进代码,用一个PictureBox代替窗口,将Form1_Paint事件过程邦定到PictureBox的Paint事件处理过程:选中PictureBox,在属性表上方选择事件,然后在Paint事件中创建处理过程。 然后,这段代码需要改造为: points[i].X=PictureBox.Width/n*i; points[i].Y=PictureBox.Height-dataPoint.V*PictureBox.Height/vMax; 三、 points[i].X=this.Width/n*i; 改为 points[i].X=this.Width-this.Width/n*i; 绘图的方向就变了。[/quote] 谢谢~~~,最后一个问题,怎么改就能在picturebox中画两条线,横坐标都是时间,纵坐标分别来来自两个TextBox? 为什么TextBox中输入double型数的时候,好像画的不对。谢谢![/quote] 谢谢~,我自己搞定了~~~

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧