简体   繁体   中英

Dynamic Data in WPF Toolkit Chart

I have a LineSeries which I am using from WPFToolkit. Following is my XAML code:

<Window x:Class="WpfChartExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chrt="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <chrt:Chart x:Name="simChart" Title="Simulation">
        <chrt:LineSeries IndependentValueBinding="{Binding Name}" 
                         DependentValueBinding="{Binding Value}" 
                         ItemsSource="{Binding}">
        </chrt:LineSeries>
    </chrt:Chart>
    <Button Name="btnStart" Width="Auto" Height="30" Grid.Row="1" HorizontalAlignment="Right" Margin="10" Click="btnStart_Click">Start Simulation</Button>
</Grid>

This is the Class for generating the data:

public partial class MainWindow : Window
{
    ObservableCollection<ChartData> chartData;
    ChartData objChartData;
    Thread thSim;
    bool thLock = true;

    public MainWindow()
    {
        InitializeComponent();
        chartData = new ObservableCollection<ChartData>();
        simChart.DataContext = chartData;
    }

    public void StartChartDataSimulation()
    {
        int i = 0;
        while (i < 10)
        {
            chartData.Add(new ChartData() { Name = DateTime.Now.ToString("HH:MM:ss"), Value = new Random().NextDouble() });
            Thread.Sleep(1000);
            i++;
        }
    }

    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        StartChartDataSimulation();            
    }


}

public class ChartData : INotifyPropertyChanged
{
    string _Name;
    double _Value;

    #region properties

    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }

    public double Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }
    #endregion

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The chart updates after all the values are generated. So, the chart is blank for the entire time. I want the chart to update as soon as the value is added. how do I do this?

I guess I got the answer. I modified my code as follows:

public partial class MainWindow : Window
{
    ObservableCollection<ChartData> chartData;
    ChartData objChartData;
    Thread MyThread;

    public MainWindow()
    {
        InitializeComponent();
        chartData = new ObservableCollection<ChartData>();
        objChartData = new ChartData() { Name = DateTime.Now.Second.ToString(), Value = 0.0 };
        chartData.Add(objChartData);
        simChart.DataContext = chartData;
        MyThread = new Thread(new ThreadStart(StartChartDataSimulation));
    }

    void MainWindow_DataChanged()
    {
        throw new NotImplementedException();
    }

    public void StartChartDataSimulation()
    {
        while (true)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                chartData.Add(new ChartData() { Name = DateTime.Now.Second.ToString(), Value = new Random().NextDouble() });
            }));
            //objChartData.Name = DateTime.Now.Second.ToString();
            //objChartData.Value = new Random().NextDouble();
            Thread.Sleep(500);
        }
    }

    private void btnStartStop_Click(object sender, RoutedEventArgs e)
    {
        if ((string)btnStartStop.Content == "Start Simulation")
        {
            if (MyThread.ThreadState == ThreadState.Unstarted)
            {
                MyThread.Start();
            }
            else if (MyThread.ThreadState == ThreadState.Suspended)
            {
                MyThread.Resume();
            }
            btnStartStop.Content = "Stop Simulation";
        }
        else
        {
            MyThread.Suspend();
            btnStartStop.Content = "Start Simulation";
        }
    }

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        MyThread.Suspend();
        Application.Current.Shutdown();
    }


}

public class ChartData : INotifyPropertyChanged
{
    string _Name;
    double _Value;

    #region properties

    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }

    public double Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }
    #endregion

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM