0

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

0

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.

rmojab63
  • 3,513
  • 1
  • 15
  • 28
  • Thank you for your great effort! Unfortunately, I still have an error message: _The Resources element was not recognized, or the item could not be accessed._ The error is displayed on `` What is faulty? Thank you in advance. –  Mar 07 '17 at 06:04
  • I really times so free and have the faulty code you uploaded here. Would be nice if you could tell me what the solutions are. [link](https://gist.github.com/anonymous/70d7c04eeb853f3b3b3c3003418e71ac) [link](https://gist.github.com/anonymous/1af6f11785c888b7a3777d064fd7edad) –  Mar 07 '17 at 06:22
  • I copy paste the code in the links in a new project and it works. Would you please create a new project, copy paste it exactly and test it. Let me know if anything is wrong there. (Also note that the Button will not be displayed, because you override the content of the Window in code behind) – rmojab63 Mar 07 '17 at 06:37
  • Also check this too: Move the ``...`` to ``App.xaml`` and in ```` block. – rmojab63 Mar 07 '17 at 06:40
  • I do not know what is wrong, I've done exactly the same thing and have re-written the `` property in App.XAML. _error message:_ Nested properties are not supported: Window.Resources [link](https://gist.github.com/anonymous/0b26b10a0d84ef99278efa07d2128b94) –  Mar 07 '17 at 06:46
  • Delete```` part. It should be like this: `` – rmojab63 Mar 07 '17 at 06:49
  • _errrorMessage:_ The name "DataPointToStringConverter" does not exist in the namespace "clr-namespace: WpfApplication1". And _error message:_ The type or namespacename "ViewModel" was not found (missing a using directive or an assembly reference?)| Sorry for the inconveniences! I hope you can help me. –  Mar 07 '17 at 06:53
  • Delete this line: ``DataContext = new ViewModel();`` It is from some other code. Sorry. About ``DataPointToStringConverter``. You should put it in the namespace the ``xmlns:local=....`` is pointing at. put it after ``MainWindow`` class. – rmojab63 Mar 07 '17 at 07:59
  • Thank you for your help. Could not you tell me how to add LinearAxis a margin by Code? `plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 10, Minimum = 0 });` Does it have to be re-written or where else? Thank you in advance. –  Mar 08 '17 at 07:41