using LiveCharts.Wpf; using LiveCharts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Timers; namespace LiveChartPractice { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private Timer _timer; private Random _rand; private SampleData _data; public MainWindow() { InitializeComponent(); InitInstance(); } private void InitInstance() { _data = this.DataContext as SampleData; _timer = new Timer(); _timer.Elapsed -= _timer_Elapsed; _timer.Elapsed += _timer_Elapsed; _timer.Interval = 100; _timer.Stop(); _rand = new Random(); } private void btnSeriesAdd_Click(object sender, RoutedEventArgs e) { if (_data == null) return; _data.SeriesCollection.Add(new LineSeries { Title = $"Series {_data.SeriesCollection.Count + 1}", Values = new ChartValues { _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8) }, LineSmoothness = _rand.Next(0, 2), //0: straight lines, 1: really smooth lines //PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"), //PointGeometrySize = 50, //PointForeground = Brushes.Gray }); } private void btnRT_Click(object sender, RoutedEventArgs e) { if (_data == null) return; _timer.Start(); } private void _timer_Elapsed(object? sender, ElapsedEventArgs e) { _data.SingleSeries[0].Values.Add(_rand.NextDouble() * 10.0); } } }