Dynamic Line chart in C# WPF Application

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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?

回答1:

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:

 

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 } 

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();     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(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(); } 

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!