现在正在学习C#和WPF约5或6周,我在一个项目上作为实习生工作。没有做坏事,但现在我被卡住了,我无法弄清楚为什么。尝试将WinForms应用程序迁移到WPF。旧的应用程序使用Z-graphs,编写了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;
就像我说的那样,只有一个初学者,因此可能存在一些明显的错误。随意指出并教育我。
由于
答案 0 :(得分:1)
您在方法中声明了PlotModel ...当您离开该函数时,它将超出范围。所以你什么都没有约束力。您的PlotModel需要在ViewModel / Main中公开访问并声明。
答案 1 :(得分:0)
我自己是OxyPlot的新手。据我所知,您可能需要包含一个使用
using OxyPlot.WPF;
声明。
同时致电
openPlot InvalidatePlot(true);
可能需要触发重绘。
您可能需要添加
openPlot.Series.Clear();
在添加系列之前,如果有任何方法可能会再次添加这些系列。