0

已经学习 C# 和 WPF 大约 5 或 6 周了,我正在作为一个项目的实习生工作。没有做坏事,但现在我被卡住了,我不知道为什么。尝试将 WinForms 应用程序迁移到 WPF。旧应用程序使用 Z 图,为 OxyPlot 编写了代码,但我的图没有显示,我不知道为什么。

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>

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



    }

使用以下命名空间...

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

就像我说的,只是一个初学者,所以那里可能有一些明显的错误。随时指出他们并教育我。

谢谢

4

2 回答 2

1

您在方法中声明您的 PlotModel ......当您离开该函数时,它将超出范围。所以你对任何东西都没有约束力。您的 PlotModel 需要可公开访问并在您的 ViewModel/Main 中声明。

于 2015-02-20T14:27:14.597 回答
0

我自己是 OxyPlot 的新手。据我所知,您可能需要包含一个 using

using OxyPlot.WPF;

宣言。

也打电话给

openPlot InvalidatePlot(true);

可能需要触发重绘。

您可能需要添加一个

openPlot.Series.Clear();

在添加系列之前,如果有任何方法可以再次添加这些系列。

于 2014-09-30T20:25:18.650 回答