Back to Devexpress

Generate Series from a Data Source

windowsforms-6562-controls-and-libraries-chart-control-provide-data-generate-series-from-a-data-source.md

latest6.7 KB
Original Source

Generate Series from a Data Source

  • Feb 01, 2021
  • 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.

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

  • Using the Properties window

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.

csharp
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.
vb
Class Sale {
    Public Property DepartmentName As String 
    Public Property Year As Int
    Public Property Volume As Double 
End Class
' ...
Dim sales As List(Of Sale) = 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.

SymbolDescription
ChartControl.SeriesTemplateReturns the series template the chart uses to generate its series.
SeriesTemplateThe series template that the chart uses to generate its series.
SeriesTemplate.SeriesDataMemberGets or sets the name of the data member whose values identify series.
SeriesBase.ArgumentDataMemberGets or sets the name of the data field that contains series point arguments.
SeriesBase.ValueDataMembersGets 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.

csharp
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];
        }
    }
}
vb
chartControl.BoundDataChanged = (chartControl.BoundDataChanged + OnBoundDataChanged)
' ...
Private Sub OnBoundDataChanged(ByVal sender As Object, ByVal args As EventArgs)
    Dim diagram As XYDiagram = CType(chartControl.Diagram,XYDiagram)
    If (diagram Is Nothing) Then
        Return
    End If
    For Each series As Series In chartControl.Series
        If (series.Name = "Temperature") Then
            Dim seriesView As LineSeriesView = CType(series.View,LineSeriesView)
            If (seriesView Is Nothing) Then
                Exit For
            End If
            seriesView.AxisX = diagram.SecondaryAxesX(0)
        End If           
    Next
End Sub

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.

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

Series

Series Scale Types

Data Representation