zedGraph绘制实时数据的散点图
看了几个有关zedGraph画动态曲线的例子,都采用Timer定时,来控制曲线的动态时间。但我的数据是下位机实时采集得到的数据,通过串口Oncomm事件传递给Pc,我现在想画其实时的曲线,不用Timer能实现吗?坐标自动收缩能实现吗?
我做过相关的应用,数据是在串口响应事件中添加到PointPairList中的,实时刷新界面是新起的一个线程,在线程中更新。坐标自动收缩我想就在刷新界面中改变坐标轴的值就行了吧。
/// <summary>
/// zedgragh控件刷新线程
/// </summary>
public void DrawPicture()
{
while (true)
{
/// zedgragh控件刷新线程
/// </summary>
public void DrawPicture()
{
while (true)
{
if (g_cDrawEnable == 1)
{
zedGraphControl2.Refresh();
}
System.Threading.Thread.Sleep(200);
}
}
private void Form1_Load(object sender, EventArgs e)
{
drawPictureThread=new Thread (new ThreadStart (DrawPicture));
drawPictureThread.IsBackground = true;
drawPictureThread.Start();
}
{
zedGraphControl2.Refresh();
}
System.Threading.Thread.Sleep(200);
}
}
private void Form1_Load(object sender, EventArgs e)
{
drawPictureThread=new Thread (new ThreadStart (DrawPicture));
drawPictureThread.IsBackground = true;
drawPictureThread.Start();
}
现在有个问题啊。用ZedGraph画图的时候,我的X坐标为HH:mm:ss格式,但目前X轴全部显示的00:00:00X轴这样定义的 myPane.XAxis.Scale.Format = "HH:mm:ss";//横轴格式
myPane.XAxis.Type = AxisType.Date;
string[] labels = new string[dt.Rows.Count];
for(int i=0;i<dt.Rows.Count;i++)
{
lables[i] = Convert.ToDateTime(dt.Rows[i]["RecTime"]).ToString("HH:mm:ss");
}
myPane.XAxis.Scale.TextLabels = labels;
string[] labels = new string[dt.Rows.Count];
for(int i=0;i<dt.Rows.Count;i++)
{
lables[i] = Convert.ToDateTime(dt.Rows[i]["RecTime"]).ToString("HH:mm:ss");
}
myPane.XAxis.Scale.TextLabels = labels;
用zedgraph绘制实时曲线,时间间隔为100ms,但有问题,请指教
∙ 下面是代码,由串口采集的数据存到showline数组里,然后由zedgraph控件进行绘点,但是,因为,调用Invalidate()方法,更新控件太慢,像单独开个线程,只让控件更新。
using System;
using System;
using System.Collections.Generic;
using System.Text;
using ZedGraph;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Text;
using ZedGraph;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
namespace ShowGraphLibrary
{
public class ShowGraph
{
/// <summary>
/// 声明显示坐标的ZedGraph
/// </summary>
private ZedGraphControl ShowZedGraph;
{
public class ShowGraph
{
/// <summary>
/// 声明显示坐标的ZedGraph
/// </summary>
private ZedGraphControl ShowZedGraph;
//private Thread CTrlThread;
// 声明一个委托:
//public delegate void DelegateCTrlThr();
//public delegate void DelegateCTrlThr();
// 声明了一个方法:
/// <summary>
/// 线名称
/// </summary>
private string lineName;
/// <summary>
/// 线名称
/// </summary>
private string lineName;
/// <summary>
/// 声明构造函数
/// </summary>
/// <param name="ShowZedGraph">坐标轴</param>
/// <param name="showColor">颜</param>
/// 声明构造函数
/// </summary>
/// <param name="ShowZedGraph">坐标轴</param>
/// <param name="showColor">颜</param>
/// <param name="lineName">曲线名称</param>
public ShowGraph(ZedGraphControl ShowZedGraph, Color showColor, string lineName)
{
public ShowGraph(ZedGraphControl ShowZedGraph, Color showColor, string lineName)
{
this.ShowZedGraph = ShowZedGraph;
//
GraphPane ShowGraphPane = this.ShowZedGraph.GraphPane;
//
//设置1200个点,假设每50毫秒更新一次,刚好检测1分钟
//一旦构造后将不能更改这个值
RollingPointPairList list = new RollingPointPairList(100);
//
GraphPane ShowGraphPane = this.ShowZedGraph.GraphPane;
//
//设置1200个点,假设每50毫秒更新一次,刚好检测1分钟
//一旦构造后将不能更改这个值
RollingPointPairList list = new RollingPointPairList(100);
//开始,增加的线是没有数据点的(也就是list为空)
//增加一条名称:Voltage,颜Color.Bule,无符号,无数据的空线条
LineItem tempCurve = ShowGraphPane.AddCurve(lineName, list, showColor, SymbolType.None);
//增加一条名称:Voltage,颜Color.Bule,无符号,无数据的空线条
LineItem tempCurve = ShowGraphPane.AddCurve(lineName, list, showColor, SymbolType.None);
//传递名称
this.lineName = lineName;
this.lineName = lineName;
//X轴最小值0
ShowGraphPane.XAxis.Scale.Min = 0;
//////X轴最大30
//////ShowGraphPane.XAxis.Scale.Max = 1000;
ShowGraphPane.XAxis.Scale.Max = 200;
//////X轴小步长1,也就是小间隔
ShowGraphPane.XAxis.Scale.MinorStep = 1;
//////X轴大步长为5,也就是显示文字的大间隔
ShowGraphPane.XAxis.Scale.MajorStep = 10;
ShowGraphPane.XAxis.Scale.Min = 0;
//////X轴最大30
//////ShowGraphPane.XAxis.Scale.Max = 1000;
ShowGraphPane.XAxis.Scale.Max = 200;
//////X轴小步长1,也就是小间隔
ShowGraphPane.XAxis.Scale.MinorStep = 1;
//////X轴大步长为5,也就是显示文字的大间隔
ShowGraphPane.XAxis.Scale.MajorStep = 10;
//////Y轴最大30
ShowGraphPane.YAxis.Scale.Min = -20;
ShowGraphPane.YAxis.Scale.Max = 40;
ShowGraphPane.YAxis.Scale.MinorStep = 1;
ShowGraphPane.YAxis.Scale.MajorStep = 10;
// Scale the axes
//改变轴的刻度
//this.ShowZedGraph.AxisChange();
ShowGraphPane.YAxis.Scale.Min = -20;
ShowGraphPane.YAxis.Scale.Max = 40;
ShowGraphPane.YAxis.Scale.MinorStep = 1;
ShowGraphPane.YAxis.Scale.MajorStep = 10;
// Scale the axes
//改变轴的刻度
//this.ShowZedGraph.AxisChange();
}
/// <summary>
/// 添加节点
/// </summary>
/// <param name="tempValue">显示的值</param>
/// <param name="tempIndex">横坐标索引</param>
/// <param name="tempIndex">横坐标索引</param>
public void ShowLine(double tempValue, double tempIndex)
{
// Make sure that the curvelist has at least one curve
//确保CurveList不为空
pane{
// Make sure that the curvelist has at least one curve
//确保CurveList不为空
if (this.ShowZedGraph.GraphPane.CurveList.Count <= 0)
return;
return;
// Get the first CurveItem in the graph
//取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查CurveItem
LineItem tempCurve = this.ShowZedGraph.GraphPane.CurveList[this.lineName] as LineItem;
if (tempCurve == null)
return;
//取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查CurveItem
LineItem tempCurve = this.ShowZedGraph.GraphPane.CurveList[this.lineName] as LineItem;
if (tempCurve == null)
return;
// Get the PointPairList
//第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
IPointListEdit tempList = tempCurve.Points as IPointListEdit;
//第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
IPointListEdit tempList = tempCurve.Points as IPointListEdit;
//
if (tempList == null)
return;
if (tempList == null)
return;
//添加显示内容
tempList.Add(tempIndex, tempValue);
Scale xScale = this.ShowZedGraph.GraphPane.XAxis.Scale;
if (tempIndex > xScale.Max - xScale.MajorStep)
{
xScale.Max = tempIndex + xScale.MajorStep;
xScale.Min = xScale.Max - 250;
}// Make sure the Y axis is rescaled to accommodate actual data
//第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
this.ShowZedGraph.AxisChange();
// Force a redraw
//第四步:调用Form.Invalidate()方法更新图表
//第四步:调用Form.Invalidate()方法更新图表
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论