C#WPF应用程序中的动态折线图 [英] Dynamic Line chart in C# WPF Application
问题描述
Im在WPF中开发一个C#.Net GUI,实际上它是一个与嵌入式设备串行通信的应用程序,我想显示一个带有经常接收的数据的折线图。我还应该提供一个选项来保存这些图表并提供打印选项。我如何在自由图书馆或软件的支持下动态绘制?
Im developing one GUI in C#.Net under WPF.Actually its an application for serial communication with the embedded device,I want to show a line chart with the frequently recieved data.And also I should provide an option to save those charts and to give an option to print it.How can I draw this dynamically with the support of free libraries or softwares?
推荐答案
我对我的所有WPF图表需求使用动态数据显示。它支持保存图表,非常快速,提供无缝缩放和平移。
命名空间:xmlns:d3 =http://research.microsoft.com/DynamicDataDisplay/1.0
I use Dynamic Data Display for all my WPF charting needs. It supports saving the charts, is very quick, provides seamless zooming and panning. Namespace: xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
XAML:
<d3:ChartPlotter Name="plotter" Background="White">
<d3:ChartPlotter.Resources>
<conv:Date2AxisConverter x:Key="Date2AxisConverter"/>
</d3:ChartPlotter.Resources>
<d3:ChartPlotter.HorizontalAxis>
<d3:HorizontalDateTimeAxis Name="dateAxis"/>
</d3:ChartPlotter.HorizontalAxis>
<d3:Header Content="{Binding PlotHeader}"/>
<d3:VerticalAxisTitle Content="Value"/>
<d3:HorizontalAxisTitle Content="Date"/>
</d3:ChartPlotter>
C#代码:使用转换器
C# Code: Converter used
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
}
<代码:清除图表和创建折线图,这里我的StockasticProcessPoint是一个结构,具有字段DateTime t和字段双值。
C# Code: Clearing Graph and Creating line graph, Here my StockasticProcessPoint is a structure with a field "DateTime t" and a field "Double value".
using Microsoft.Research.DynamicDataDisplay;
using System.Collections.ObjectModel;
using Microsoft.Research.DynamicDataDisplay.DataSources;
public void ClearLines()
{
var lgc = new Collection<IPlotterElement>();
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<StochasticProcessPoint> _edsSPP;
_edsSPP = new EnumerableDataSource<StochasticProcessPoint>(myListOfStochasticProcessPoints);
_edsSPP.SetXMapping(p => dateAxis.ConvertToDouble(p.t));
_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的绑定示例。
With this, you should be able to plot a chart in WPF. Adding data live when it comes back from the serial port should be no problem. You can also look at the binding examples from DynamicDataDisplay.
这篇关于C#WPF应用程序中的动态折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!