Search code examples
c#wpfcoordinatescoordinate-systemsoxyplot

Read coordinates from OxyPlot


I have a coordinate system with OxyPlot. Different points are generated in this coordinate system. Now I would gladly ask the points by button, is it possible to read the points again? The points are displayed in the coordinate system.

This is e.g. Get a list of the points (a point always contains an X and a Y value) Which I afterwards the list (or whatever the solution is) can query and all points receive the indicated in this coordinate system.

enter image description here

The picture should make more clear what I would like.

I have a coordinate system and in this coordinate system points are displayed which were generated before. Now I want to get these points from the coordinate system into a list or something similar.


Solution

  • This is a working example which listens to plotModel.MouseDown (You need to change it) and displays a window with the required information. Note that you can display the resulted listbox in different Controls such as 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.
        }  
    }
    

    You need the following style

    <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>
    

    Which uses the following converter:

    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();
        } 
    }
    

    You can change how DataPoint is converted into a string in this converter.