Back to Devexpress

How to: Individually Change the View Type of Automatically Created Series

windowsforms-6317-controls-and-libraries-chart-control-examples-creating-charts-providing-data-how-to-individually-change-the-view-type-of-automatically-created-series.md

latest7.3 KB
Original Source

How to: Individually Change the View Type of Automatically Created Series

  • Nov 13, 2018
  • 4 minutes to read

This example demonstrates how an auto-created series (a series generated automatically based on the series template settings) can be accessed at runtime, to individually change its view type.

This is performed in a special ChartControl.BoundDataChanged event of the chart control.

csharp
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...

namespace ChangeViewOfAnAutoSeries {
    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[] { "Jan", "Section3", 40 });
            table.Rows.Add(new object[] { "Feb", "Section1", 20 });
            table.Rows.Add(new object[] { "Feb", "Section2", 30 });
            table.Rows.Add(new object[] { "Feb", "Section3", 80 });
            table.Rows.Add(new object[] { "March", "Section1", 30 });
            table.Rows.Add(new object[] { "March", "Section2", 40 });
            table.Rows.Add(new object[] { "March", "Section3", 100 });

            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 SideBySideBarSeriesView();

            // Specify the BoundDataChanged event handler.
            chart.BoundDataChanged +=
                new BoundDataChangedEventHandler(chart_BoundDataChanged);

            // 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);
        }

        private void chart_BoundDataChanged(object sender, EventArgs e) {
            ChartControl chart = (ChartControl)sender;

            // Change the view of the "Month: Feb" series from 
            // SideBySideBarSeriesView to LineSeriesView.
            Series feb = chart.GetSeriesByName("Month: Feb");
            if (feb != null)
                feb.ChangeView(ViewType.Line);

            // Change the view of the "Month: March" series from 
            // SideBySideBarSeriesView to SplineSeriesView.
            Series march = chart.GetSeriesByName("Month: March");
            if (march != null)
                march.ChangeView(ViewType.Spline);
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Namespace ChangeViewOfAnAutoSeries
    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() { "Jan", "Section3", 40 })
            table.Rows.Add(New Object() { "Feb", "Section1", 20 })
            table.Rows.Add(New Object() { "Feb", "Section2", 30 })
            table.Rows.Add(New Object() { "Feb", "Section3", 80 })
            table.Rows.Add(New Object() { "March", "Section1", 30 })
            table.Rows.Add(New Object() { "March", "Section2", 40 })
            table.Rows.Add(New Object() { "March", "Section3", 100 })

            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 SideBySideBarSeriesView()

            ' Specify the BoundDataChanged event handler.
            AddHandler chart.BoundDataChanged, AddressOf chart_BoundDataChanged

            ' 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

        Private Sub chart_BoundDataChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim chart As ChartControl = CType(sender, ChartControl)

            ' Change the view of the "Month: Feb" series from 
            ' SideBySideBarSeriesView to LineSeriesView.
            Dim feb As Series = chart.GetSeriesByName("Month: Feb")
            If feb IsNot Nothing Then
                feb.ChangeView(ViewType.Line)
            End If

            ' Change the view of the "Month: March" series from 
            ' SideBySideBarSeriesView to SplineSeriesView.
            Dim march As Series = chart.GetSeriesByName("Month: March")
            If march IsNot Nothing Then
                march.ChangeView(ViewType.Spline)
            End If
        End Sub
    End Class
End Namespace

The result is shown in the following image.

See Also

Series Views

How to: Change the View of a Series