We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to generate dynamic number of series based on common items source?

Description:

This article describes how to generate dynamic number of series by binding a collection (Items source) directly to chart.

Solution:

The solution for achieving this scenario is quite easy. It can be achieved by extending the SfChart (inheriting SfChart), and providing it with the SeriesTemplate (for customizing series type and its properties) and Source properties.

The implementation process is explained in the following steps.

Step 1: Define chart’s view model setup

This step describes how to define the chart’s collection (items source) through MVVM.

Model

C#

 

C#

 

View Model

C#

 

Step 2: Implement SfChart extension

To achieve this requirement, use the custom SfChart, which inherits from SfChart and defines two properties, namely the Source and SeriesTemplate properties.

Source:

The Source property is used to bind the items source (collection of collections) to chart. The chart series will be generated per item in the collection.

C#

 

SeriesTemplate:

The SeriesTemplate property is used to define the type and visual appearance of chart series. This template is more flexible, and it allows you to define any type of series and all its properties since the content of the template is the series.

C#

 

The following code illustrates generating dynamic number of series based on data(collection).

C#

 

XAML

 

The following illustrates the result of above code examples.

 

To generate different types of chart series

You can generate multiple types of series by using the DataTemplateSelector property. A new class of SeriesDataTemplateSelector can be created by inheriting the DataTemplateSelector to achieve this requirement.

C#

 

SfChart extension with SeriesTemplateSelector:

The SeriesTemplateSelector property is used to select the different series template in single chart based on item.

C#

 

SeriesDataTemplateSelector definition:

The different types of templates are defined in the resources for the SeriesTemplateSelector property.

XAML

 

The following column and line series (multiple series) are created as the result of above code examples.

 

Please find the required sample from the link.

Article ID: Published Date: Last Revised Date: Platform: Control:
7578 03/21/2017 03/13/2018 WPF SfChart
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.