如何将ObservableCollection与LiveChart2一起使用



如何将ObservableCollection与LiveCharts2 一起使用

我正在尝试使用ObservableCollection实现LiveChart2,以在每次添加新值时更新图表。如果我一次添加所有数据,则图表工作良好;ObservableCollection";。链接到文档:https://github.com/beto-rodriguez/LiveCharts2/blob/master/docs/overview/1.4.automatic%20updates.md我需要帮助。我有这个代码:

using System.Collections.ObjectModel;
using System.Windows;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;

namespace WpfSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public ISeries[] Series { get; set; }
public ObservableCollection<ObservableValue> Series2 { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
// This one is not working ///////////////////////////////////
Series2 = new ObservableCollection<ObservableValue>();
Series2.Add(new ObservableValue { Value = 1 });
Series2.Add(new ObservableValue { Value = 2 });
Series2.Add(new ObservableValue { Value = 3 });
// every time you update the Value property, you will also see that change in the user interface
Series2[0].Value = 5;

// This one is working fine
Series = new ISeries[]
{
new LineSeries<double>
{
Values = new double[] { 6, 4, 3, 9, 4, 2, 1 },
}
};

DataContext = this;
}
} 
}
<Window x:Class="WpfSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfSample"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<lvc:CartesianChart 
Series="{Binding Series}" Margin="4,4,433,174">
</lvc:CartesianChart>
<lvc:CartesianChart 
Series="{Binding Series2}" Margin="403,0,0,177">
</lvc:CartesianChart>
<Button Content="Button" HorizontalAlignment="Left" Margin="728,318,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
</Window>

也许可以试试这样的东西:

public ObservableCollection<ISeries> Series2 { get; set; }

private void Button_Click(object sender, RoutedEventArgs e)
{  
Series2 = new ObservableCollection<ISeries>();
Series2.Add(new LineSeries<double> { Values = new List<double> {1,2,3 } });
}

相关内容

  • 没有找到相关文章

最新更新