0

Been learning C#, and WPF for about 5 or 6 weeks now, and I'm working as an Intern on a project . Not doing to bad, but right now I'm stuck and I cant figure out why. Trying to migrate a WinForms application over to WPF. The old application used Z-graphs, written the code for OxyPlot, but my graphs arent showing and I have no idea why.

XAML

<Border BorderBrush="{StaticResource chromeBrush}" BorderThickness="5 5 5 5" Margin="2 3 0 2" Grid.Column="3" Grid.ColumnSpan="36" Grid.Row="3" Grid.RowSpan="33" Background="Black"> <DockPanel> <DockPanel.DataContext> <local:Main/> </DockPanel.DataContext> <oxy:Plot Model="{Binding openPlot}" /> </DockPanel> </Border>

And the C#

public partial class Main : Window
{

    public Dictionary<string, DoorData> doorList;

    public Main()
    {
        //this.InitializeComponent();
        doorList = new Dictionary<string, DoorData>();

    }

    public DoorData doorGraphs = new DoorData();

    private void unitList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedUnit = unitList.SelectedItem.ToString();
        doorGraphs = doorList[selectedUnit];

        this.Title = selectedUnit + " Open";
        PlotModel openPlot = new PlotModel();

        List<SmartDDUOperation> values = doorGraphs.Data;
        doorGraphs.FilterToType(SmartOperationType.Open);
        IEnumerable<SmartDDUOperation> drawOrder = values.OrderByDescending(x => x.TimeStamp);

        List<LineSeries> linePointsArray = new List<LineSeries>();
        foreach (SmartDDUOperation doorOp in drawOrder)
        {
            List<Tuple<double, double>> points = new List<Tuple<double, double>>();
            points = doorOp.GetTimePoints2();
            LineSeries linePoints = new LineSeries();

            foreach (Tuple<double, double> p in points)
            {
                DataPoint XYPoint = new DataPoint(p.Item1, p.Item2);
                linePoints.Points.Add(XYPoint);
            }

            linePointsArray.Add(linePoints);
        }

        LinearAxis Xaxis = new LinearAxis();
        Xaxis.Maximum = 6;
        Xaxis.Minimum = 0;
        Xaxis.Position = AxisPosition.Bottom;
        Xaxis.Title = "Time (s)";
        openPlot.Axes.Add(Xaxis);

        LinearAxis Yaxis = new LinearAxis();
        Yaxis.Maximum = 12;
        Yaxis.Minimum = 1;
        Yaxis.Position = AxisPosition.Left;
        Yaxis.Title = "Door Position";
        openPlot.Axes.Add(Yaxis);

        // Adds each series to the graph

        foreach (var series in linePointsArray)
        {
            openPlot.Series.Add(series);
        }



    }

Using the following namespaces...

enter using OxyPlot; using OxyPlot.Annotations; using OxyPlot.Series; using OxyPlot.Axes;

Like I said, only a beginner, so there is probably some obvious mistakes in there. Feel free to point them out and educate me.

Thanks

Reaver
  • 1
  • 2

2 Answers2

1

You are declaring your PlotModel inside of a method... It is going to be out of scope when you leave the function. So you are binding to nothing. Your PlotModel needs to be publicly accessible and declared in your ViewModel/Main.

CodyF
  • 4,977
  • 3
  • 26
  • 38
0

I am new to OxyPlot myself. From what I know, you may need to include a using

using OxyPlot.WPF;

declaration.

Also a call to

openPlot InvalidatePlot(true);

may be needed to trigger a redraw.

You might need to add a

openPlot.Series.Clear();

Before you add your series, if there is any way that you might be adding those series again.

user2069105
  • 561
  • 4
  • 7