【问题标题】:Dynamic Line chart in C# WPF ApplicationC# WPF 应用程序中的动态折线图
【发布时间】:2011-08-17 09:11:36
【问题描述】:

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

【问题讨论】:

    标签: c# wpf xaml charts


    【解决方案1】:

    我使用Dynamic Data Display 来满足我所有的 WPF 图表需求。它支持保存图表,速度非常快,提供无缝缩放和平移。 命名空间: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# 代码:使用的转换器

    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”和字段“双值”的结构。

    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 中的绑定示例。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2020-11-09
    相关资源
    最近更新 更多