从 OxyPlot 读取坐标
Read coordinates from OxyPlot
我有一个 OxyPlot 坐标系。在此坐标系中生成不同的点。现在我很乐意通过按钮询问积分,是否可以再次读取积分?这些点显示在坐标系中。
例如获取点列表(一个点总是包含一个 X 和一个 Y 值)
之后我可以查询列表(或任何解决方案)并且所有点都收到此坐标系中指示的内容。
图片应该更清楚我想要什么。
我有一个坐标系,在这个坐标系中显示了之前生成的点。现在我想把坐标系中的这些点放到列表或类似的东西中。
这是一个工作示例,它监听 plotModel.MouseDown
(您需要更改它)并显示包含所需信息的 window。请注意,您可以在不同的控件(例如 Popup)中显示结果列表框。
public partial class MainWindow : Window
{
PlotModel plotModel;
public MainWindow()
{
InitializeComponent();
plotModel = new PlotModel { Title = "OxyPlot" };
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom });
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 10, Minimum = 0 });
var series1 = new OxyPlot.Series.LineSeries
{
MarkerType = MarkerType.Circle,
MarkerSize = 5,
MarkerStroke = OxyColors.White
};
series1.Points.Add(new DataPoint(0, 6));
series1.Points.Add(new DataPoint(1, 2));
series1.Points.Add(new DataPoint(2, 4));
series1.Points.Add(new DataPoint(3, 2));
series1.Points.Add(new DataPoint(4, 7));
series1.Points.Add(new DataPoint(6, 6));
series1.Points.Add(new DataPoint(8, 8));
series1.Smooth = true;
plotModel.Series.Add(series1);
this.Content = new OxyPlot.Wpf.PlotView() { Model = plotModel };
plotModel.MouseDown += PlotModel_MouseDown;
}
private void PlotModel_MouseDown(object sender, OxyMouseDownEventArgs e)
{
var s = plotModel.Series[0] as LineSeries; // asuming that there is just one line series
ListBox list = new ListBox();
list.Style = (Style)TryFindResource("listOfPoint");
list.ItemsSource = s.Points;
Window win = new Window() { Content = list, Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; // You can display the results in a Popup too
win.ShowDialog(); // You might call Show() instead.
}
}
您需要以下样式
<Window.Resources>
<local:DataPointToStringConverter x:Key="DataPointToStringConverter"/>
<Style x:Key="listOfPoint" TargetType="ListBox">
<Setter Property="AlternationCount" Value="1000"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource DataPointToStringConverter}">
<Binding />
<Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ListBox, Mode=FindAncestor}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
使用以下转换器:
public class DataPointToStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
DataPoint point = (DataPoint)values[0];
List<DataPoint> points = ((IEnumerable<DataPoint>)values[1]).ToList();
return (points.IndexOf(point) + 1).ToString() +"- (" + point.X.ToString() + " , " + point.Y.ToString() + ")";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您可以在此转换器中更改 DataPoint 转换为字符串的方式。
我有一个 OxyPlot 坐标系。在此坐标系中生成不同的点。现在我很乐意通过按钮询问积分,是否可以再次读取积分?这些点显示在坐标系中。
例如获取点列表(一个点总是包含一个 X 和一个 Y 值) 之后我可以查询列表(或任何解决方案)并且所有点都收到此坐标系中指示的内容。
图片应该更清楚我想要什么。
我有一个坐标系,在这个坐标系中显示了之前生成的点。现在我想把坐标系中的这些点放到列表或类似的东西中。
这是一个工作示例,它监听 plotModel.MouseDown
(您需要更改它)并显示包含所需信息的 window。请注意,您可以在不同的控件(例如 Popup)中显示结果列表框。
public partial class MainWindow : Window
{
PlotModel plotModel;
public MainWindow()
{
InitializeComponent();
plotModel = new PlotModel { Title = "OxyPlot" };
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom });
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 10, Minimum = 0 });
var series1 = new OxyPlot.Series.LineSeries
{
MarkerType = MarkerType.Circle,
MarkerSize = 5,
MarkerStroke = OxyColors.White
};
series1.Points.Add(new DataPoint(0, 6));
series1.Points.Add(new DataPoint(1, 2));
series1.Points.Add(new DataPoint(2, 4));
series1.Points.Add(new DataPoint(3, 2));
series1.Points.Add(new DataPoint(4, 7));
series1.Points.Add(new DataPoint(6, 6));
series1.Points.Add(new DataPoint(8, 8));
series1.Smooth = true;
plotModel.Series.Add(series1);
this.Content = new OxyPlot.Wpf.PlotView() { Model = plotModel };
plotModel.MouseDown += PlotModel_MouseDown;
}
private void PlotModel_MouseDown(object sender, OxyMouseDownEventArgs e)
{
var s = plotModel.Series[0] as LineSeries; // asuming that there is just one line series
ListBox list = new ListBox();
list.Style = (Style)TryFindResource("listOfPoint");
list.ItemsSource = s.Points;
Window win = new Window() { Content = list, Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; // You can display the results in a Popup too
win.ShowDialog(); // You might call Show() instead.
}
}
您需要以下样式
<Window.Resources>
<local:DataPointToStringConverter x:Key="DataPointToStringConverter"/>
<Style x:Key="listOfPoint" TargetType="ListBox">
<Setter Property="AlternationCount" Value="1000"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource DataPointToStringConverter}">
<Binding />
<Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ListBox, Mode=FindAncestor}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
使用以下转换器:
public class DataPointToStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
DataPoint point = (DataPoint)values[0];
List<DataPoint> points = ((IEnumerable<DataPoint>)values[1]).ToList();
return (points.IndexOf(point) + 1).ToString() +"- (" + point.X.ToString() + " , " + point.Y.ToString() + ")";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您可以在此转换器中更改 DataPoint 转换为字符串的方式。