Generate Series from a Data Source

  • 5 minutes to read

The Chart Control can automatically generate series from the chart's data source using the Series Template. This approach is an alternative of manual series creation.

series-template__title-image

This document consists of the following sections explaining:

How to Configure the Series Template at Design-Time

The Chart Control allows you to configure the series template at design time in the following ways.

NOTE

Before beginning series template configuration, you must assign a data source to the Chart Control. This allows Visual Studio to populate data member editors with data source field names.

  • Using the Chart Designer

    Select the Chart Control, click its smart tag and then click Run Designer... in the invoked menu.

    series-template_design-time_smart-tag

    Select the chart in the designer's structure tree. Switch to the Data tab and drag the data members to the data member fields to assign data source members to the series template's series, argument and values data members. The series template uses the series data member values to generate series, and uses the argument and values data members to fill generated series with data points.

    series-template_design-time_chart-designer

  • Using the Properties window

    Locate the ChartControl.SeriesTemplate property in the Properties window and expand it. Find the template's SeriesTemplate.SeriesDataMember property and specify which data member values should be used as series identifiers.

    ProvideData_Template_0

    Then, assign the data fields whose values should specify point arguments and values to the SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties.

    IMPORTANT

    The number of value data members that should be assigned to a series depends on the series template's view. Several view types require more than one data member that provides values. For example, the Bubble series view requires two data members - one provides series point values and the other - series point weights.

    ProvideData_Series_2

A series template stores settings and elements that are used to initialize all automatically-created chart series. The Chart Control provides the ChartControl.BoundDataChanged to customize each generated series individually. Refer to the Obtain and Customize an Auto-Created Series topic section for more information.

How to Configure the Series Template at Runtime

The following is an example of how to configure the chart's series template at runtime.


class Sale {
    public String DepartmentName { get; set; }
    public int Year { get; set; }
    public double Volume { get; set; }
}
// ...
List<Sale> sales = LoadSales();
chartControl.DataSource = sales;
chartControl.SeriesTemplate.SeriesDataMember = "DepartmentName";
chartControl.SeriesTemplate.ArgumentDataMember = "Year";
chartControl.SeriesTemplate.ValueDataMembers.AddRange("Volume");
// Other series template customizations here.

The above-mentioned code uses the following classes and properties.

Symbol Description
ChartControl.SeriesTemplate Returns the series template the chart uses to generate its series.
SeriesTemplate The series template that the chart uses to generate its series.
SeriesTemplate.SeriesDataMember Gets or sets the name of the data member whose values identify series.
SeriesBase.ArgumentDataMember Gets or sets the name of the data field that contains series point arguments.
SeriesBase.ValueDataMembers Gets a collection of the names of data fields that contain series point values.

A series template stores settings and elements that are used to initialize all automatically-created chart series. The Chart Control provides the ChartControl.BoundDataChanged to customize each generated series individually. Refer to the Obtain and Customize an Auto-Created Series topic section for more information.

How to Obtain and Customize an Auto-Created Series

The series template configures the automatically generated series' appearance and behavior using the same settings as an individual Series. All the template customizations affect the automatically generated series. Use the ChartControl.BoundDataChanged event to configure each generated series individually.


chartControl.BoundDataChanged += OnBoundDataChanged;
// ...
void OnBoundDataChanged(object sender, EventArgs args) {
    XYDiagram diagram = chartControl.Diagram as XYDiagram;
    if(diagram == null) return;
    foreach(Series series in chartControl.Series) {
        if(series.Name == "Temperature") {
            LineSeriesView seriesView = (series.View as LineSeriesView);
            if(seriesView == null) break;
            seriesView.AxisX = diagram.SecondaryAxesX[0];
        }
    }
}

In addition to the default series template settings, the Chart Control provides the SeriesNameTemplate property. This property customizes automatically generated series names using a prefix and postfix.

SeriesNameTemplate

The Chart Control can sort automatically generated series by name using the ChartControl.SeriesSorting property that specifies the series sort order. Refer to Sorting Data topic for more information.

IMPORTANT

The series name template does not effect the names of the series that are added manually.

See Also