我正在尝试用WPF和C#(动态数据显示)绘制实时信号、信号与时间的关系图;一旦信号产生,它应该立即显示在绘图图上;信号可以非常快地产生(在毫秒(0.001秒)的数量级上);最好的方法是什么?欢迎提出任何建议。谢谢
编辑:一个示例代码将不胜感激。
以下是我尝试过的:
绘图启动:
#region Constructor
public SignalStatsDisplay()
{
InitializeComponent();
plotter.Legend.Remove();
_initialChildrenCount = plotter.Children.Count;
int count = plotter.Children.Count;
//do not remove the initial children
if (count > _initialChildrenCount)
{
for (int i = count - 1; i >= _initialChildrenCount; i--)
{
plotter.Children.RemoveAt(i);
}
}
_nColorChannels = 4;
_lineprofileColor = new Color[4];
_lineprofileColor[0] = Colors.Transparent;
_lineprofileColor[1] = Colors.Red;
_lineprofileColor[2] = Colors.Black;
_lineprofileColor[3] = Colors.Blue;
//LineGraph lg = new LineGraph();
LineAndMarker<MarkerPointsGraph> lg = new LineAndMarker<MarkerPointsGraph>();
CirclePointMarker pm = new CirclePointMarker { Size = 10, Fill = Brushes.Green };
// MarkerPointsGraph mpg = new MarkerPointsGraph();
if (_nColorChannels > 0) // plotter init
{
_dataX = new List<int[]>();
_dataY = new List<int[]>();
int[] dataXOneCh = new int[1];
int[] dataYOneCh = new int[1];
dataXOneCh[0] = 0;
dataYOneCh[0] = 0;
/*CirclePointMarker marker = new CirclePointMarker();
marker.Fill = new SolidColorBrush(_lineprofileColor[0]);
marker.Pen = new Pen(marker.Fill, 0);
marker.Size = 2;
int lineWidth = 1;*/
for (int i = 0; i < 4; i++)
{
_dataX.Add(dataXOneCh); // data x-y mapping init
_dataY.Add(dataYOneCh);
EnumerableDataSource<int> xOneCh = new EnumerableDataSource<int>(dataXOneCh);
EnumerableDataSource<int> yOneCh = new EnumerableDataSource<int>(dataYOneCh);
xOneCh.SetXMapping(xVal => xVal);
yOneCh.SetXMapping(yVal => yVal);
CompositeDataSource dsOneCh = new CompositeDataSource(xOneCh, yOneCh);
// LineProfileColorSetup();
lg = plotter.AddLineGraph(dsOneCh,
(new Pen(Brushes.Green, 2)),
pm,
(new PenDescription("Data")));
// lg = plotter.AddLineGraph(dsOneCh,
// Colors.Transparent,
// 2,
// "Data");
// pm.FilteringEnabled = false;
}
plotter.FitToView();
}
else
{
return;
}
}
数据更新:
for (int i = 0; i < 1; i++)
{
if (_dataX[i].Length == _dataY[i].Length)
{
EnumerableDataSource<int> xOneCh = new EnumerableDataSource<int>(_dataX[i]);
xOneCh.SetXMapping(xVal => xVal);
EnumerableDataSource<int> yOneCh = new EnumerableDataSource<int>(_dataY[i]);
yOneCh.SetYMapping(yVal => yVal);
CompositeDataSource ds = new CompositeDataSource(xOneCh, yOneCh);
Action UpdateData = delegate()
{
// ((LineGraph)plotter.Children.ElementAt(startIndex + i)).DataSource = ds;
// ((LineGraph)plotter.Children.ElementAt(startIndex + i)).LinePen = new Pen(new SolidColorBrush(Colors.Green), 1);
// ((PointsGraphBase)plotter.Children.ElementAt(startIndex + i)).DataSource = ds;
((PointsGraphBase)plotter.Children.ElementAt(startIndex + i + 1)).DataSource = ds;
// }
// (plotter.Children.ElementAt(startIndex + i)).DataSource = ds;
};
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateData);
}
}
我遇到的问题是,当信号非常丰富时,比如说每毫秒就会产生一个新的信号,图形似乎每1秒只会生成一个新标记,我不明白。
其基本思想是生成一个绘图仪。AddLineGraph,然后只更新DataSource。但它似乎不起作用。所以我想也许我走错了方向。
我不是C#或WPF专家。当LineAndMarker没有按预期工作时,我开始怀疑。因此,我想知道人们使用WPF和C#显示实时信号(快速变化,样本之间的时间跨度可能在毫秒)的一般方式是什么。
我发现哪里做错了。以下是出现问题的部分:
_pxlValAllChan[0, signalIndex] = pxlValue;
DateTime currentTime = DateTime.Now; //has to come from real file: Tiff
TimeSpan timeSpan = currentTime - _startTime;
_timeStampAllChan[0, signalIndex] = Convert.ToInt16(timeSpan.TotalSeconds);
_pxlValOneChan = new int[_signalLength]; // no need to new it every time
_timeStampOneChan = new int[_signalLength];
for (int i = 0; i <= signalIndex; i++)
{
_pxlValOneChan[i] = _pxlValAllChan[0, i];
//_timeStampOneChan[i] = _timeStampAllChan[0, i];
_timeStampOneChan[i] = i;
}
_signalDisplay.SetData(_timeStampOneChan, _pxlValOneChan, 0, true);
我的x数据是基于时间的。我基本上把程序的启动视为起点,然后通过用当前时间减去开始时间来跟踪时间的流逝。然后,所经过的时间将保存为x轴数据(请参阅注释掉的行)。这实际上造成了问题。当我简单地将x轴数据更改为索引时,问题就消失了。图形更新非常快。虽然我还没有弄清楚应该如何更改逻辑以仍然使用时间流逝作为x轴数据,但这是图形更新缓慢的罪魁祸首。