Oxyplot中的仿射变换

时间:2017-12-18 13:06:19

标签: c# wpf xaml mvvm oxyplot

我想对LineSeries

执行简单的仿射转换

不幸的是我不能简单地这样做:

foreach (var point in myLineSeries.Points)
{
    point.X = point.X * Math.Cos(angle_rad) + point.Y ...
    point.Y = ...
}

... X的属性YDataPoint没有公开set访问者

有没有办法在LineSeries上执行此类操作(简单轮播/翻译),而无需在其上调用Clear,然后Add新转换DataPoints(其中对我来说似乎不太优雅)

1 个答案:

答案 0 :(得分:1)

不是直接在LineSeries中操纵点,而是使用MVVM:在 View Model 中进行计算,然后将结果绑定到 View 。< / p>

enter image description here

<强>视图模型:

public class MyViewModel
{
    public ObservableCollection<DataPoint> Data1 { get; set; }
    public ObservableCollection<DataPoint> Data2 { get; set; }

    public MyViewModel()
    {
        Data1 = new ObservableCollection<DataPoint>();
        Data2 = new ObservableCollection<DataPoint>();

        double pi = Math.PI;
        double a = pi / 4; // rotation angle
        double fw = 5; // wave frequency
        double fs = 100 * fw; // sampling rate
        double te = 1; // end time
        int size = (int)(fs * te); 

        // do your calculations
        var x1 = Enumerable.Range(0, size).Select(p => p / fs).ToArray();
        var y1 = x1.Select(p => Math.Sin(2*pi*fw*p)).ToArray();

        //populate your Model (original data)
        for (int i = 0; i < y1.Length; i++)
            Data1.Add(new DataPoint(x1[i], y1[i]));

        // transform original data
        var x2 = Data1.Select(p => p.X * Math.Cos(a) - p.Y * Math.Sin(a)).ToArray();
        var y2 = Data1.Select(p => p.X * Math.Sin(a) + p.Y * Math.Cos(a)).ToArray();

        // populate your Model (transformed data)
        for (int i = 0; i < y2.Length; i++)
            Data2.Add(new DataPoint(x2[i], y2[i]));
    }
}

<强> XAML:

<Window x:Class="WpfApp93.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:oxy="http://oxyplot.org/wpf"
        xmlns:local="clr-namespace:WpfApp93"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="350">

    <Window.DataContext>
        <local:MyViewModel/>
    </Window.DataContext>

    <Grid>
        <oxy:Plot>
            <oxy:LineSeries ItemsSource="{Binding Data1}"
                            DataFieldX="X"
                            DataFieldY="Y"/>
            <oxy:LineSeries ItemsSource="{Binding Data2}"
                            DataFieldX="X"
                            DataFieldY="Y"/>
        </oxy:Plot>
    </Grid>
</Window>