我在WPFToolkit中使用了LineSeries
。以下是我的XAML代码:
<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>
这是用于生成数据的类:
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));
}
}
}
生成所有值后图表会更新。因此,图表在整个时间内都是空白的。我想要在添加值后立即更新图表。我该怎么做?
答案 0 :(得分:1)
我想我得到了答案。我修改了我的代码如下:
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));
}
}
}