【问题标题】:How to plot LineSeries using the OxyPlot library on the WPF UI framework如何在 WPF UI 框架上使用 OxyPlot 库绘制 LineSeries
【发布时间】:2020-08-24 06:06:55
【问题描述】:

我正在尝试使用 OxyPlot 绘制我的数据。我想在我的图表中绘制两条线,但我不确定如何为图表提供数据。 这是我的 XAML 文件的样子:

<oxy:Plot Name="Plot1">
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding Graph1}"/>
    <oxy:LineSeries ItemsSource="{Binding Graph2}"/>
  </oxy:Plot.Series>
</oxy:Plot>

我的问题是如何在使用 LineSeries 时在同一个图中绘制两条线?

【问题讨论】:

    标签: c# wpf plot oxyplot


    【解决方案1】:

    通常您不会将 LineSeries 直接添加到 PlotView,而是添加到 PlotModel,然后将其绑定到 Plot View。

    C# 代码可能如下所示:

            PlotModel pm = new PlotModel();
    
            var s1 = new LineSeries();
    
            for (int i = 0; i < 1000; i++)
            {
                double x = Math.PI * 10 * i / (1000 - 1);
                s1.Points.Add(new DataPoint(x, Math.Sin(x)));
            }
    
            pm.Series.Add(s1);
    
            var s2 = new LineSeries();
    
            for (int i = 0; i < 1000; i++)
            {
                double x = Math.PI * 10 * i / (1000 - 1);
                s2.Points.Add(new DataPoint(x, Math.Cos(x)));
            }
    
            pm.Series.Add(s2);
    
            Plot1.Model = pm; 
    

    与 Plot1 的绑定当然也可以在 XAML 中完成。如果您的 DataContext 通过属性“MyModel”提供 PlotModel,它将如下所示:

    <oxyplot:PlotView Model="{Binding MyModel}"></oxyplot:PlotView>
    

    【讨论】:

    • 感谢您解决这个问题。在使用 log Axis 绘制与 Y 轴平行的线时,我遇到了另一个问题:var logAxisX = new LogarithmicAxis() { Position = AxisPosition.Bottom, Title = "I (A) - log", UseSuperExponentialFormat = false, Base = 10 }; var logAxisY = new LogarithmicAxis() { Position = AxisPosition.Left, Title = "Time (s) - log", UseSuperExponentialFormat = false, Base = 10 }; pm.Axes.Add(logAxisX); pm.Axes.Add(logAxisY); 到目前为止,一切似乎都很好,但这条线从未显示出来。积分:s2.Points.Add(new DataPoint(n, 0)); s2.Points.Add(new DataPoint(n, n));
    • 我猜这是因为如果绘制在对数轴上,第一个点将在 y 中处于负无穷大。
    • 解决了,第一点就是问题。我使用了一个非常接近 zero 的新 point 来替换它。
    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多