corelibraries-devexpress-dot-xtracharts-4c54bbd5.md
Contains settings that define the common prefix and postfix for the names of data bound series.
Namespace : DevExpress.XtraCharts
Assembly : DevExpress.XtraCharts.v25.2.dll
NuGet Package : DevExpress.Charts
public class SeriesNameTemplate :
ChartElement
Public Class SeriesNameTemplate
Inherits ChartElement
The following members return SeriesNameTemplate objects:
| Library | Related API Members |
|---|---|
| WinForms Controls | ChartControl.SeriesNameTemplate |
| ASP.NET MVC Extensions | ChartControlSettings.SeriesNameTemplate |
| ASP.NET Web Forms Controls | WebChartControl.SeriesNameTemplate |
| .NET Reporting Tools | XRChart.SeriesNameTemplate |
The SeriesNameTemplate class serves as a naming template for series which are not present in the ChartControl.Series collection but are bound to their data at the chart control level (in particular, via the ChartControl.DataSource, ChartControl.SeriesDataMember and both the SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties).
The SeriesNameTemplate class exposes two properties (SeriesNameTemplate.BeginText and SeriesNameTemplate.EndText) that define the common prefixes and postfixes for the names of data bound series. The series names which these prefix and postfix are added to are taken by each series from the data field defined by the ChartControl.SeriesDataMember property.
The following example demonstrates how to bind a chart to data at runtime using series templates. It uses the same approach as the design-time example, but another data table is generated in this code to simplify the example. For this example to work correctly, don’t forget to include all necessary assemblies to the References list of your project.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-charts-bind-chart-to-data-using-series-templates
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...
namespace BindUsingTemplatesRuntimeCS {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private DataTable CreateChartData() {
// Create an empty table.
DataTable table = new DataTable("Table1");
// Add three columns to the table.
table.Columns.Add("Month", typeof(String));
table.Columns.Add("Section", typeof(String));
table.Columns.Add("Value", typeof(Int32));
// Add data rows to the table.
table.Rows.Add(new object[] { "Jan", "Section1", 10 });
table.Rows.Add(new object[] { "Jan", "Section2", 20 });
table.Rows.Add(new object[] { "Feb", "Section1", 20 });
table.Rows.Add(new object[] { "Feb", "Section2", 30 });
table.Rows.Add(new object[] { "March", "Section1", 15 });
table.Rows.Add(new object[] { "March", "Section2", 25 });
return table;
}
private void Form1_Load(object sender, EventArgs e) {
// Create a chart.
ChartControl chart = new ChartControl();
// Generate a data table and bind the chart to it.
chart.DataSource = CreateChartData();
// Specify data members to bind the chart's series template.
chart.SeriesDataMember = "Month";
chart.SeriesTemplate.ArgumentDataMember = "Section";
chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] {"Value"});
// Specify the template's series view.
chart.SeriesTemplate.View = new StackedBarSeriesView();
// Specify the template's name prefix.
chart.SeriesNameTemplate.BeginText = "Month: ";
// Dock the chart into its parent, and add it to the current form.
chart.Dock = DockStyle.Fill;
this.Controls.Add(chart);
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...
Namespace BindUsingTemplatesRuntimeCS
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Function CreateChartData() As DataTable
' Create an empty table.
Dim table As New DataTable("Table1")
' Add three columns to the table.
table.Columns.Add("Month", GetType(String))
table.Columns.Add("Section", GetType(String))
table.Columns.Add("Value", GetType(Int32))
' Add data rows to the table.
table.Rows.Add(New Object() { "Jan", "Section1", 10 })
table.Rows.Add(New Object() { "Jan", "Section2", 20 })
table.Rows.Add(New Object() { "Feb", "Section1", 20 })
table.Rows.Add(New Object() { "Feb", "Section2", 30 })
table.Rows.Add(New Object() { "March", "Section1", 15 })
table.Rows.Add(New Object() { "March", "Section2", 25 })
Return table
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' Create a chart.
Dim chart As New ChartControl()
' Generate a data table and bind the chart to it.
chart.DataSource = CreateChartData()
' Specify data members to bind the chart's series template.
chart.SeriesDataMember = "Month"
chart.SeriesTemplate.ArgumentDataMember = "Section"
chart.SeriesTemplate.ValueDataMembers.AddRange(New String() {"Value"})
' Specify the template's series view.
chart.SeriesTemplate.View = New StackedBarSeriesView()
' Specify the template's name prefix.
chart.SeriesNameTemplate.BeginText = "Month: "
' Dock the chart into its parent, and add it to the current form.
chart.Dock = DockStyle.Fill
Me.Controls.Add(chart)
End Sub
End Class
End Namespace
Object ChartElement SeriesNameTemplate
See Also