我正在使用Zedgraph的RollingPointPairList绘制实时图。我每秒接收100点并试图每秒绘制100点。但是zedgraph显示滞后并且能够每秒仅绘制最多20点。我正在使用以下代码进行绘图
RollingPointPairList samp = new RollingPointPairList(105);
private void Form1_Load(object sender, EventArgs e)
{
CreateGraph(zg1);
SetSize();
}
int x1 = 0;
private void TimerEventProcessor(int[] diff)
{
zg1.GraphPane.XAxis.Scale.MaxAuto = true;
x1 += 1;
samp.Add(diff[0], x1);
if (samp.Count >= 100)
{
zg1.AxisChange();
zg1.Invalidate();
zg1.Refresh();
Thread.Sleep(50);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
SetSize();
}
private void SetSize()
{
zg1.Location = new Point(10, 10);
zg1.Size = new Size(this.ClientRectangle.Width - 10, this.ClientRectangle.Height - 10);
}
private void CreateGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = "Test";
myPane.XAxis.Title.Text = "X Value";
myPane.YAxis.Title.Text = "Y Axis";
LineItem myCurve;
myCurve= myPane.AddCurve("Curve 1", samp, Color.Blue, SymbolType.Star);
myCurve.Symbol.Fill = new Fill(Color.White);
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
zgc.AxisChange();
zgc.Refresh();
}
因此,他们可以通过任何方式改进我的代码,以便每秒可以绘制100点或更多。
答案 0 :(得分:3)
每秒绘制100个点远非ZedGraph限制。我怀疑瓶颈不是ZedGraph。为了能够尽我所能地重现你的用例,我很快就完成了这段代码,它通过ZedGraph控件每秒抛出最大点数,我和你一样初始化:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;
namespace WindowsFormsApplication1 {
static class Program {
/// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void Main() {
Application.Run(new Form1());
}
class Form1 : Form {
ZedGraphControl zgc;
System.Windows.Forms.Label lbl;
public Form1() {
this.WindowState = FormWindowState.Maximized;
lbl = new System.Windows.Forms.Label { Parent = this, Dock = DockStyle.Bottom, AutoSize = false, TextAlign = ContentAlignment.MiddleCenter };
zgc = new ZedGraphControl {
Parent = this,
Dock = DockStyle.Fill,
Margin = new Padding(10)
};
var myPane = zgc.GraphPane;
myPane.Title.Text = "Test";
myPane.XAxis.Title.Text = "X Value";
myPane.YAxis.Title.Text = "Y Axis";
myPane.XAxis.Scale.MaxAuto = true;
var myCurve = myPane.AddCurve("Curve 1", samp, Color.Blue, SymbolType.Star);
myCurve.Symbol.Fill = new Fill(Color.White);
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
}
RollingPointPairList samp = new RollingPointPairList(105);
int c;
CancellationTokenSource cts = new CancellationTokenSource();
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
var t = new System.Timers.Timer { Interval = 1000 };
t.Elapsed += (sender, eventargs) => { this.BeginInvoke(new Action(() => { lbl.Text = "Dot per seconds: " + c.ToString(); c = 0; })); };
t.Start();
Task.Run(() => {
var r = new Random();
while(!cts.IsCancellationRequested) {
TimerEventProcessor(r.Next(-10, 10));
};
});
}
protected override void OnClosing(CancelEventArgs e) {
cts.Cancel();
cts.Token.WaitHandle.WaitOne();
base.OnClosing(e);
}
int x1 = 0;
void TimerEventProcessor(int d) {
x1++; c++;
samp.Add(d, x1);
zgc.AxisChange();
zgc.Invalidate();
}
}
}
}
如果我错误地假设你的代码应该如何运行,请告诉我。但是,它在我的机器上运行良好。
恕我直言,我在团队中使用ZedGraph时给出的建议是:
我怀疑最后一点是你的瓶颈的原因:在一个单独的线程上使用windows Forms SynchronizationContext在ZedGraph控件上调用Invalidate可以更好地实现接收数据包。