一个波形图(如正弦)怎样在界面展示呢

雨师88 2010-10-27 11:02:16
一个波形图(如正弦)怎样在界面展示呢
...全文
167 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LorenLiu 2010-10-27
  • 打赏
  • 举报
回复
以下代码作为参考

写一个自定义的Panel类,用于绘制坐标图


public class CoordinatePanel : Panel
{
public CoordinatePanel()
{
m_CoordinateData = new List<Coordinate>();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}

public List<Coordinate> CoordinateData
{
get { return m_CoordinateData; }
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw axis
using (Pen pen = new Pen(Brushes.Black))
{
pen.EndCap = LineCap.ArrowAnchor;
e.Graphics.DrawLine(
pen,
new Point(m_Margin, this.Height / 2),
new Point(this.Width - m_Margin, this.Height / 2)
);
e.Graphics.DrawLine(
pen,
new Point(this.Width / 2, this.Height - m_Margin),
new Point(this.Width / 2, m_Margin)
);
}


// Draw data
if (m_CoordinateData.Count == 0)
return;

Point[] pointArray = new Point[m_CoordinateData.Count];

for (Int32 i = 0; i < m_CoordinateData.Count; i++)
{
Coordinate coordinate = m_CoordinateData[i];
Point p = new Point(
Convert.ToInt32(coordinate.X * m_XScale + this.Width / 2),
Convert.ToInt32(this.Height / 2 - coordinate.Y * m_YScale)
);

pointArray[i] = p;
}

e.Graphics.DrawLines(Pens.Black, pointArray);
}

private List<Coordinate> m_CoordinateData;
private Int32 m_Margin = 20;
// set the y scale, the result will be extend on Y axis
private Int32 m_YScale = 80;
// set the x scale, the result will be extend on Y axis
private Int32 m_XScale = 20;
}

public class Coordinate
{
public Coordinate(Double x, Double y)
{
m_X = x;
m_Y = y;
}

public Double X
{
get { return m_X; }
set { m_X = value; }
}

public Double Y
{
get { return m_Y; }
set { m_Y = value; }
}

private Double m_X;
private Double m_Y;
}


测试代码如下

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
for (Double i = -Math.PI; i <= Math.PI; i += 0.01)
{
Coordinate coordinate = new Coordinate(i, Math.Sin(i));
coordinatePanel1.CoordinateData.Add(coordinate);
}
}
}


效果图
flyerwing 2010-10-27
  • 打赏
  • 举报
回复
学习下,应该只能用图片展示了吧.
SecretGarden 2010-10-27
  • 打赏
  • 举报
回复

110,535

社区成员

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

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

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