SciChart WPF 2D Charts > Filters API > Creating a Custom Filter
Creating a Custom Filter

One of the most powerful features of the Filters API is the ability to create custom filters.

To create a Custom Filter we simply override FilterBase. We must then override a few methods to perform the filtering.

Example: A Custom Low Pass Filter

Steps to create a custom filter

  1.    
  2. Create a class which inherits FilterBase
  3. Accept a DataSeries via constructor and pass to the base.
  4. Create and store the derived FilteredDataSeries.
  5. Override FilterAll() to recalculate data. Ensure this is called in the constructor of your Custom Filter
  6. (OPTIONAL) Override FilterOnAppend, FilterOnRemove, FilterOnInsert, FilterOnUpdate for better performance on point-by-point operations
CustomFilter Eample
Copy Code
/// To create a custom filter, inherit FilterBase and override FilterALl. For best performance, you can also override FilterOnAppend
/// </summary>
public class CustomFilter : FilterBase
{
    private readonly XyDataSeries<TimeSpan, double> _originalDataSeries;
    private readonly XyDataSeries<TimeSpan, double> _filteredDataSeries = new XyDataSeries<TimeSpan, double>();
    public CustomFilter(XyDataSeries<TimeSpan,double> originalDataSeries) : base(originalDataSeries)
    {
        _originalDataSeries = originalDataSeries;
        // Store a reference in the base class to the FilteredDataSeries
        FilteredDataSeries = _filteredDataSeries;
        // Update the filter
        FilterAll();
    }
    protected override void FilterOnAppend(int index)
    {
        // Override FilterOnAppend to update just the latest data-point in _filteredDataSeries for a more efficient filter
        base.FilterOnAppend(index);
    }
    protected override void FilterOnInsert(int startIndex, int count)
    {
        // Override FilterOnInsert to update just the inserted data-point in _filteredDataSeries for a more efficient filter
        base.FilterOnInsert(startIndex, count);
    }
    protected override void FilterOnRemove(int startIndex, int count)
    {
        // Override FilterOnRemove to update just the removed data-point in _filteredDataSeries for a more efficient filter
        base.FilterOnRemove(startIndex, count);
    }
    protected override void FilterOnUpdate(int index)
    {
        // Override FilterOnUpdate to update just the updated data-point in _filteredDataSeries for a more efficient filter
        base.FilterOnUpdate(index);
    }
    public override void FilterAll()
    {
        // When FilterAll is called, recreate the FilteredDataSeries and apply the filtering.
        _filteredDataSeries.Clear();
        _filteredDataSeries.Append(_originalDataSeries.XValues[0], _originalDataSeries.YValues[0]);
        const double beta = 0.2;
        // Implementing a simple low pass filter https://kiritchatterjee.wordpress.com/2014/11/10/a-simple-digital-low-pass-filter-in-c/
        for (int i = 1; i < _originalDataSeries.Count; i++)
        {
            TimeSpan xValue = _originalDataSeries.XValues[i];
            double yValue = beta * _originalDataSeries.YValues[i] + (1 - beta) * _filteredDataSeries.YValues[i - 1];
            _filteredDataSeries.Append(xValue, yValue);
        }
    }
}

Applying the Custom Filter

Applying the Custom Filter is a little different to the extension methods previously described. Because the CustomFilter inherits FilterBase, it is not an IDataSeries, so you have to access the FilterBase.FilteredDataSeries property to assign to a RenderableSeries in SciChart. Other than that, custom filters work exactly the same as filters built into SciChart.

Offset Filter
Copy Code
using SciChart.Charting.Model.Filters; // Required for class FilterBase

var dataSeries = new XyDataSeries<double,double>(); // Original Data
dataSeries.Append(0,1);
dataSeries.Append(2,2);

int numInterpolationPoints = 5;
var customFilter = new CustomFilter(dataSeries); // Create the custom filter.
var customFilteredDataSeries = customFilter.FilteredDataSeries;

var lineRenderableSeries = new FastLineRenderableSeries()
{
    DataSeries = customFilteredDataSeries , // Apply the Custom Filtered Data to a Line Series
}

 

See Also