实例介绍
【实例简介】
ZedGraph显示多条实时曲线并可控制显示哪一条
【实例截图】
【核心代码】
namespace 显示实时曲线
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int tickStart = 0;
private PointPairList list1 = new PointPairList();
private PointPairList list2 = new PointPairList();
double y = 0;
LineItem curve1;
LineItem curve2;
private void Form1_Load(object sender, EventArgs e)
{
//获取引用
GraphPane myPane = zedGraphControl1.GraphPane;
curve1 = myPane.AddCurve("曲线1", list1, Color.Blue, SymbolType.None);
curve2 = myPane.AddCurve("曲线2", list2, Color.Green, SymbolType.None);
//设置标题
myPane.Title.Text = "实时曲线";
//设置X轴说明文字
myPane.XAxis.Title.Text = "时间";
//设置Y轴说明文字
myPane.YAxis.Title.Text = "温度";
myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
//myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
//设置1200个点,假设每50毫秒更新一次,刚好检测1分钟,一旦构造后将不能更改这个值
//RollingPointPairList list1 = new RollingPointPairList(2400);
//RollingPointPairList list2 = new RollingPointPairList(2400);
//开始,增加的线是没有数据点的(也就是list为空)
//增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条
timer1.Interval = 1000; //设置timer控件的间隔为50毫秒
timer1.Enabled = true; //timer可用
timer1.Start(); //开始
myPane.Y2Axis.IsVisible = true;
myPane.Y2Axis.Scale.Align = AlignP.Inside;
myPane.Y2Axis.MajorTic.IsOpposite = false;
myPane.Y2Axis.MinorTic.IsOpposite = false;
myPane.XAxis.Scale.Format = "dd HH:mm:ss"; //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
myPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;
myPane.XAxis.Scale.Min = 0; //X轴最小值0
myPane.XAxis.Scale.Max = 5; //X轴最大30
//myPane.XAxis.Scale.MinorStep = 0.02;//X轴小步长1,也就是小间隔
//myPane.XAxis.Scale.MajorStep = 0.1;//X轴大步长为5,也就是显示文字的大间隔
myPane.XAxis.MajorGrid.IsVisible = true;//设置X虚线
myPane.YAxis.MajorGrid.IsVisible = true;//设置Y虚线
//改变轴的刻度
//zedGraphControl1.AxisChange();
// Show the x axis grid
// myPane.XAxis.MajorGrid.IsVisible = true;
// myPane.YAxis.MajorTic.IsOpposite = true;
// myPane.YAxis.MinorTic.IsOpposite = true;
// Don't display the Y zero line
//保存开始时间
tickStart = Environment.TickCount;
zedGraphControl1.IsShowPointValues = true;
//zedGraphControl1.PointValueEvent = new ZedGraphControl.PointValueHandler(MyPointValueHandler);
// OPTIONAL: Add a custom context menu item
// zedGraphControl1.ContextMenuBuilder = new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder);
// OPTIONAL: Handle the Zoom Event
//zedGraphControl1.ZoomEvent = new ZedGraphControl.ZoomEventHandler(MyZoomEvent);
//zedGraphControl1.AxisChange();
// Make sure the Graph gets redrawn
zedGraphControl1.Invalidate();
}
private int timerDrawI = 0;
private void timer1_Tick(object sender, EventArgs e)
{
//确保CurveList不为空
if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
{
return;
}
//取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
if (curve == null)
{
return;
}
//第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
IPointListEdit list = curve.Points as IPointListEdit;
// If this is null, it means the reference at curve.Points does not
// support IPointListEdit, so we won't be able to modify it
if (list == null)
{
return;
}
// Time is measured in seconds
/*for (int i = 0; i <= 100; i )
{
double x = (double)new XDate(DateTime.Now.AddSeconds(-(100 - i)));
list1.Add(x, (double)Math.Sin(timerDrawI / 10) * 12);
list2.Add(x, (double)Math.Sin(timerDrawI / 10f));
}*/
//double x = (double)new XDate(DateTime.Now.AddSeconds(100.00));
double x = (double)new XDate(DateTime.Now);
// 3 seconds per cycle
list1.Add(x, (double)Math.Sin(timerDrawI / 10) * 12);
list2.Add(x, (double)Math.Sin(timerDrawI / 10f));
y = (double)Math.Sin(timerDrawI / 10f);
//textBox1.Text = y.ToString();
textBox1.Text = list2.ToString();
// list.Add(time, Math.Sin(2.0 * Math.PI * time / 5.0));
timerDrawI ;
// Keep the X scale at a rolling 30 second interval, with one
// major step between the max X value and the end of the axis
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
/*if ( time > xScale.Max - xScale.MajorStep )
{
xScale.Max = time xScale.MajorStep;
xScale.Min = xScale.Max - 30.0;
}*/
//第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
zedGraphControl1.AxisChange();
//第四步:调用Form.Invalidate()方法更新图表
zedGraphControl1.Invalidate();
if (list.Count >= 100)
{
list1.RemoveAt(0);
list2.RemoveAt(0);
}
}
/// <summary>
/// Display customized tooltips when the mouse hovers over a point
/// </summary>
/*private string MyPointValueHandler(ZedGraphControl control, GraphPane pane,
CurveItem curve, int iPt)
{
// Get the PointPair that is under the mouse
PointPair pt = curve[iPt];
return curve.Label.Text " is " pt.Y.ToString("f2") " units at " pt.X.ToString("f1") " days";
}*/
/// <summary>
/// Customize the context menu by adding a new item to the end of the menu
/// </summary>
//private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip,
// Point mousePt)
//{
// ToolStripMenuItem item = new ToolStripMenuItem();
// item.Name = "add-beta";
// item.Tag = "add-beta";
// item.Text = "Add a new Beta Point";
// item.Click = new System.EventHandler(AddBetaPoint);
// menuStrip.Items.Add(item);
//}
/// <summary>
/// Handle the "Add New Beta Point" context menu item. This finds the curve with
/// the CurveItem.Label = "Beta", and adds a new point to it.
/// </summary>
/* private void AddBetaPoint(object sender, EventArgs args)
{
// Get a reference to the "Beta" curve IPointListEdit
IPointListEdit ip = zedGraphControl1.GraphPane.CurveList["Beta"].Points as IPointListEdit;
if (ip != null)
{
double x = ip.Count * 5.0;
double y = Math.Sin(ip.Count * Math.PI / 15.0) * 16.0 * 13.5;
ip.Add(x, y);
zedGraphControl1.AxisChange();
zedGraphControl1.Refresh();
}
}*/
// Respond to a Zoom Event
/*private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,
ZoomState newState)
{
// Here we get notification everytime the user zooms
}*/
private void Form1_Resize(object sender, EventArgs e)
{
//SetSize();
}
// Set the size and location of the ZedGraphControl
/*private void SetSize()
{
// Control is always 10 pixels inset from the client rectangle of the form
Rectangle formRect = this.ClientRectangle;
formRect.Inflate(-10, -10);
if (zedGraphControl1.Size != formRect.Size)
{
zedGraphControl1.Location = formRect.Location;
zedGraphControl1.Size = formRect.Size;
}
}*/
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
curve2.IsVisible = true;
}
else
{
//否则文本框不可以用
curve2.IsVisible = false;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
curve1.IsVisible = true;
}
else
{
//否则文本框不可以用
curve1.IsVisible = false;
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论