我想对LineSeries
不幸的是我不能简单地这样做:
foreach (var point in myLineSeries.Points)
{
point.X = point.X * Math.Cos(angle_rad) + point.Y ...
point.Y = ...
}
... X
的属性Y
和DataPoint
没有公开set
访问者
有没有办法在LineSeries
上执行此类操作(简单轮播/翻译),而无需在其上调用Clear
,然后Add
新转换DataPoints
(其中对我来说似乎不太优雅)
答案 0 :(得分:1)
不是直接在LineSeries
中操纵点,而是使用MVVM:在 View Model 中进行计算,然后将结果绑定到 View 。< / p>
<强>视图模型:强>
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>