How to Aggregate WPF Chart Data Using FlexChart
Charts can be used to visualize and analyze data. Sometimes, we need to show summarized data in the chart for which the aggregation is used. In ComponentOne FlexChart for WPF, UI is separated from data manipulation to improve the product's performance, unlike the legacy C1Chart. Hence, FlexChart does not have built-in functionality for data manipulation.
In this topic, we will demonstrate the steps necessary to achieve aggregation of the WPF chart control data. A complete sample can be downloaded at the end.
Ready to Try FlexChart? Download ComponentOne Today!
WPF Chart Aggregation
Here is our WPF chart control unaggregated.
A common user requirement is to summarize the data at runtime by quarter. The Sum aggregate type adds all the values together, and the chart displays a single bar for each quarter.
The nice thing about making this a run-time feature is that the end user can choose how to view the data. For example, they may want to summarize the data by year instead.
Typically aggregation is done by simply summing all values together. But there are more aggregate types that users can analyze, including Average, Count, Min, and Max. The example below uses the “Max” aggregate type, which simply displays the maximum value found for each aggregate group.
To achieve the aggregation functionality like Sum, Avg, Max, Min, and Count in FlexChart for WPF, let’s go through the next section with code examples.
Creating the Model and Enum
To best create this runtime feature, we will create model and enumeration classes. This will enable it to be reusable in more forms.
Create the Model Class for the FlexChart Data. This would be unique to your dataset.
Create Enum for the Aggregate Type.
Creating Aggregate Extension
Next, we will create an Extension method for Aggregate to perform aggregation basis of AggregateType on data. Extension methods enable you to "add" methods to existing types without creating a new derived type. Here, we have created an Aggregate method in the Extension.
Creating ViewModel for the FlexChart
The ViewModel acts as the data source for our View, and we can control the View through ViewModel. We created some properties and methods in ViewModel which can be bound to the view.
For the data collection, we expose a DataSource. Typically, your Views and ViewModels will implement INotifyPropertyChanged so that you can pass updates to any property to the View.
- Update Bindings
Update Bindings whenever Aggregate Property updates.
- Update Aggregation
Update Aggregate DataSource based on Binding, AggregateProperty, and AggregateType. The updated DataSource (bounded to FlexChart’s ItemsSource) will reflect the FlexChart view.
Creating FlexChart and Binding to ViewModel in XAML
At last, we have to create a FlexChart in XAML so it can be visible in View. But the most important part is to bind the View with ViewModel. It can be done by setting the View's DataContext property to an instance of your ViewModel.
This is helpful for anyone migrating from C1Chart or any FlexChart user.
Download the sample for complete implementation.
Ready to Try FlexChart? Download ComponentOne Today!