在我的XAML文件中,我创建了一个ChartPlotter,然后我用c#创建了我的LineGraphs,并将它们附加到我的ChartPlotter上。我试图找到一种方法来更新这些线形图创建后,但它总是失败。
我发现的唯一解决方案是,我删除所有LineGraphs,用新值重新创建它们,最后将它们链接到我的ChartPlotter。
如何更新LineGraph ?
for (int i = 0; i < lgs.Length; i++)
if (lgs[i] != null)
lgs[i].RemoveFromPlotter();
PS: lgs是我的LineGraph数组
要更新LineGraphs,你必须使用ObservableDataSource对象而不是CompositeDataSource。对于这个对象,你可以使用AppendAsync()方法。
public partial class MainWindow : Window
{
public ObservableDataSource<Point> source1 = null;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Create source
source1 = new ObservableDataSource<Point>();
// Set identity mapping of point in collection to point on plot
source1.SetXYMapping(p => p);
// Add the graph. Colors are not specified and chosen random
plotter.AddLineGraph(source1, 2, "Data row");
// Force everyting to fit in view
plotter.Viewport.FitToView();
// Start computation process in second thread
Thread simThread = new Thread(new ThreadStart(Simulation));
simThread.IsBackground = true;
simThread.Start();
}
private void Simulation()
{
int i = 0;
while (true)
{
Point p1 = new Point(i * i, i);
source1.AppendAsync(Dispatcher, p1);
i++;
Thread.Sleep(1000);
}
}
}
所有你想要的是在方法Simulation的while中。
source1.AppendAsync(Dispatcher, p1);