C#WPF应用程序中的动态折线图

我在WPF下用C#.Net开发了一个GUI。实际上它是一个与嵌入式设备进行串行通信的应用程序,我想用频繁收到的数据显示折线图。我还应该提供一个选项来保存这些图表并给出打印它的选项。如何在免费库或软件的支持下动态绘制它?

我使用动态数据显示来满足我的所有WPF图表需求。 它支持保存图表,非常快速,提供无缝缩放和平移。 命名空间:xmlns:d3 =“http://research.microsoft.com/DynamicDataDisplay/1.0”

XAML:

           

C#代码:转换器使用

 public class Date2AxisConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is DateTime && targetType == typeof(double)) { return ((DateTime)value).Ticks / 10000000000.0; // See constructor of class Microsoft.Research.DynamicDataDisplay.Charts.DateTimeAxis // File: DynamicDataDisplay.Charts.Axes.DateTime.DateTimeAxis.cs // alternatively, see the internal class Microsoft.Research.DynamicDataDisplay.Charts.DateTimeToDoubleConversion } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // try Microsoft.Research.DynamicDataDisplay.Charts.DateTimeAxis.DoubleToDate throw new NotSupportedException(); } #endregion } 

C#代码:清除图表和创建折线图,这里我的StockasticProcessPoint是一个结构,其字段为“DateTime t”,字段为“Double value”。

 using Microsoft.Research.DynamicDataDisplay; using System.Collections.ObjectModel; using Microsoft.Research.DynamicDataDisplay.DataSources; public void ClearLines() { var lgc = new Collection(); foreach (var x in plotter.Children) { if (x is LineGraph || x is ElementMarkerPointsGraph) lgc.Add(x); } foreach (var x in lgc) { plotter.Children.Remove(x); } } internal void SendToGraph() { IPointDataSource _eds = null; LineGraph line; ClearLines(); EnumerableDataSource _edsSPP; _edsSPP = new EnumerableDataSource(myListOfStochasticProcessPoints); _edsSPP.SetXMapping(p => dateAxis.ConvertToDouble(pt)); _edsSPP.SetYMapping(p => p.value); _eds = _edsSPP; line = new LineGraph(_eds); line.LinePen = new Pen(Brushes.Black, 2); line.Description = new PenDescription(Description); plotter.Children.Add(line); plotter.FitToView(); } 

有了这个,您应该能够在WPF中绘制图表。 从串口回来时实时添加数据应该没问题。 您还可以查看DynamicDataDisplay中的绑定示例。