最近做一个上位机的项目,要求实时显示温度曲线,开始用.net自带的 chart控做的,在动态显示那块,在删除一个数据点、加入一个新的数据点的时候,新的数据点显示不出来;纠结好久,解决不了这个问题。后来只好选择开源.net控件zedgraph。
ZedGraph 是用于创建任意数据的二维线型、棒型、饼型图表的一个类库,也可以作为 Windows 窗体用户控件和 ASP 网页访问控件。这个类库具有高度的灵活性,几乎所有式样的图表都能够被创建。关于zedgraph控件的使用具体。参考官网介绍:http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET
在项目中遇到的问题总结如下:
一、曲线图基本参数设置:
//标题和x轴、y轴标签
根据上面的设置就可以很容易定制自己的曲线图了。
三、程序参考:
//标题、标签
this.zedGraphControl1.GraphPane.Title.Text = "实时曲线图";
this.zedGraphControl1.GraphPane.Title.FontSpec.FontColor = Color.Blue;
this.zedGraphControl1.GraphPane.Title.FontSpec.Size = 30f;
this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";
this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.FontColor = Color.Blue;
this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.Size = 20f;
this.zedGraphControl1.GraphPane.YAxis.Title.Text = "温度(℃)";
this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.FontColor = Color.Blue;
this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.Size = 20f;
//this.zedGraphControl1.GraphPane.Chart.Border.Color = Color .White ;
this.zedGraphControl1.GraphPane.Chart.IsRectAuto =true ;
//刻度值字体大小
this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 20f;
this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 20f;
this.zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
this.zedGraphControl1.GraphPane.YAxis.Scale.Max = 50;
//x轴类型
this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Date;
this.zedGraphControl1.GraphPane.XAxis.Scale.Format = "HH:mm:ss";
//显示网格线
this.zedGraphControl1.GraphPane.YAxis.MajorGrid.IsVisible = true;
this.zedGraphControl1.GraphPane.XAxis.MajorGrid.IsVisible = true;
//legend
this.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 10f;
this.zedGraphControl1.GraphPane.Legend.Position = LegendPos.InsideTopRight;
//面板填充颜色
this.zedGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
//设置窗口大小
SetSize();
//更新画面
this.zedGraphControl1.AxisChange();
this.zedGraphControl1.Refresh();
private void timer2_Tick(object sender, EventArgs e)
{
this.zedGraphControl1.GraphPane.CurveList.Clear();
//温度曲线1
try
{
y = Convert.ToDouble(Convert.ToInt16(strTempureArry[4]) / 100.00); ;
}
catch
{
return;
}
//存储数据
StoreDataDelegate myStoreDataDelegate = new StoreDataDelegate(StoreData);//实例化委托
myStoreDataDelegate(DateTimeNow, y);//调用委托
list.Add(x, y);
LineItem mycurve = zedGraphControl1.GraphPane.AddCurve
("温度传感器1", list, Color.Red, SymbolType.Circle);
mycurve.Line.Width = 2.0F;//线宽
TextObj textT1 = new TextObj(y.ToString("") + "℃",
x, y*1.02, CoordType.AxisXYScale, AlignH.Center , AlignV.Bottom);
textT1.FontSpec.Size = 10f;
textT1.FontSpec.FontColor = Color.Red;
textT1.FontSpec.Fill.IsVisible = false;
textT1.FontSpec.Border.IsVisible = false;
//textT1.ZOrder = ZOrder.A_InFront;
//textT1.FontSpec.Fill = new Fill(Color.FromArgb(240, Color.Snow));
this.zedGraphControl1.GraphPane.GraphObjList.Add(textT1);
this.zedGraphControl1.AxisChange();
this.zedGraphControl1.Refresh();
//this.zedGraphControl1.Invalidate();
if (list.Count >= 10)
{
list.RemoveAt(0);
//同时清除该点的值
this.zedGraphControl1.GraphPane.GraphObjList.RemoveAt(0);
}
}
控件大小设置:
private void SetSize()
{
zedGraphControl1.Location = new Point(10, 10);
zedGraphControl1.Size = new Size(ClientRectangle.Width - 100, ClientRectangle.Height - 200);
}