The example you linked to tries to demonstrate data binding with the least amount of code possible. Using analogWaveform.Append
uses the waveform's built-in change notification to notify the graph of new data. However, as you found, the just creates a larger and larger waveform, leading to memory issues.
If you want to maintain history with a fixed capacity, you can use the ChartCollectionAnalogWaveform
. If you want to display individual waveforms, then you could use a more common form of WPF data binding where we use INotifyPropertyChanged
to notify when a new waveform is available:
public partial class MainWindow : Window, INotifyPropertyChanged {
private AnalogWaveform<double> analogWaveform;
public MainWindow( ) {
InitializeComponent( );
graph1.DataContext = this;
}
public AnalogWaveform<double> Data {
get { return analogWaveform; }
set {
analogWaveform = value;
PropertyChanged( this, new PropertyChangedEventArgs( "Data" ) );
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void button1_Click( object sender, RoutedEventArgs e ) {
Random random = new Random( );
int pointCount = 100;
double[] data = new double[pointCount];
for( int i = 0; i < pointCount; ++i )
data[i] = random.NextDouble( );
Data = AnalogWaveform<double>.FromArray1D( data );
}
}
You could also expose Data
as a dependency property in this example, which would handle the change notification automatically.
~ Paul H