I use Dynamic Data Display for all my WPF graphics tasks. It supports saving diagrams, very fast, provides smooth 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 # Code: Used Converter
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;
C # code: clear the graph and create a line graph. Here, my StockasticProcessPoint is a structure with a "DateTime t" field and a "Double value" field.
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(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(); }
With this, you can build a graph in WPF. Adding real-time data when it returns from the serial port should not be a problem. You can also see binding examples from DynamicDataDisplay.
source share