Back to Devexpress

Side-by-Side Full-Stacked Bar Chart

windowsforms-7564-controls-and-libraries-chart-control-series-views-2d-series-views-bar-series-views-side-by-side-full-stacked-bar-chart.md

latest8.8 KB
Original Source

Side-by-Side Full-Stacked Bar Chart

  • Oct 02, 2023
  • 4 minutes to read

Short Description

The Side-by-Side Full-Stacked Bar Chart is represented by the SideBySideFullStackedBarSeriesView object, which belongs to Bar Series Views. This view combines the advantages of both the Full-Stacked Bar and Side-by-Side Bar chart types, so that you can stack different bars, and combine them into groups shown side-by-side across the same axis value (via the SideBySideFullStackedBarSeriesView.StackedGroup property).

Note

When using series template binding for Side-by-Side Full-Stacked Bars, the SideBySideFullStackedBarSeriesView.StackedGroup property should be specified at runtime, in the ChartControl.BoundDataChanged event handler.

A Side-by-Side Full-Stacked Bar chart is shown in the image below. Note that this chart type is based upon the XYDiagram, so it can be rotated to show bars either vertically or horizontally.

Note

A Side-by-Side Full-Stacked Bar chart can display series containing data points with positive or negative values. However, a series with positive values is stacked only with other series containing positive values; and a series with negative values is stacked with other series containing negative values.

Note that if a series contains data points with both positive and negative values, it is treated as a series with positive values, while all its negative values are treated as zeros.

Chart Type Characteristics

The table below lists the main characteristics of this chart type.

FeatureValue
Series View typeSideBySideFullStackedBarSeriesView
Diagram type2D-XYDiagram
Number of arguments per series point1
Number of values per series point1

Note

For information on which chart types can be combined with the Side-by-Side Full-Stacked Bar Chart , refer to the Series Views Compatibility document.

Example

This example demonstrates how to populate the Chart Control with series that have the SideBySideFullStackedBarSeriesView at runtime:

csharp
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using DevExpress.XtraCharts;
// ...

namespace SideBySideFullStackedBarChart {
    public partial class Form1 : Form {
        ChartControl stackedBarChart;
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            // Create a new chart.
            stackedBarChart = new ChartControl();
            stackedBarChart.DataSource = AgeStructureDataReader.GetDataByAgeAndGender();
            stackedBarChart.SeriesTemplate.SeriesDataMember = "GenderAge";
            stackedBarChart.SeriesTemplate.ArgumentDataMember = "Country";
            stackedBarChart.SeriesTemplate.ValueDataMembers.AddRange("Population");        

            stackedBarChart.SeriesTemplate.View = new SideBySideFullStackedBarSeriesView();

            stackedBarChart.BoundDataChanged += this.OnChartBoundDataChanged;

            // Access the type-specific options of the diagram.
            XYDiagram diagram = (XYDiagram)stackedBarChart.Diagram;
            diagram.Rotated = true;
            diagram.AxisY.Label.TextPattern = "{VP:P}";
            diagram.AxisY.WholeRange.AutoSideMargins = false;
            diagram.AxisY.WholeRange.SideMarginsValue = 0;

            // Customize the legend.
            stackedBarChart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True;
            stackedBarChart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
            stackedBarChart.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside;
            stackedBarChart.Legend.MaxVerticalPercentage = 20;

            // Add a title to the chart (if necessary).
            stackedBarChart.Titles.Add(new ChartTitle());
            stackedBarChart.Titles[0].Text = "Population: Age-Gender Structure";
            stackedBarChart.Titles[0].WordWrap = true;            

            // Add the chart to the form.
            stackedBarChart.Dock = DockStyle.Fill;
            this.Controls.Add(stackedBarChart);
        }
        private void OnChartBoundDataChanged(object sender, EventArgs e) {
            foreach (Series series in stackedBarChart.Series) {
                if (!(series.Tag is GenderAgeInfo)) return;
                GenderAgeInfo item = (GenderAgeInfo)series.Tag;
                SideBySideFullStackedBarSeriesView view = series.View as SideBySideFullStackedBarSeriesView;
                if (view == null) return;
                view.StackedGroup = item.Gender;
            }
        }
    }
}
vb
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Linq
Imports System.Windows.Forms
Imports System.Xml.Linq
Imports DevExpress.XtraCharts
' ...

Namespace SideBySideFullStackedBarChart
    Partial Public Class Form1
        Inherits Form

        Private stackedBarChart As ChartControl
        Public Sub New()
            InitializeComponent()
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            ' Create a new chart.
            stackedBarChart = New ChartControl()
            stackedBarChart.DataSource = AgeStructureDataReader.GetDataByAgeAndGender()
            stackedBarChart.SeriesTemplate.SeriesDataMember = "GenderAge"
            stackedBarChart.SeriesTemplate.ArgumentDataMember = "Country"
            stackedBarChart.SeriesTemplate.ValueDataMembers.AddRange("Population")

            stackedBarChart.SeriesTemplate.View = New SideBySideFullStackedBarSeriesView()

            AddHandler stackedBarChart.BoundDataChanged, AddressOf Me.OnChartBoundDataChanged

            ' Access the type-specific options of the diagram.
            Dim diagram As XYDiagram = CType(stackedBarChart.Diagram, XYDiagram)
            diagram.Rotated = True
            diagram.AxisY.Label.TextPattern = "{VP:P}"
            diagram.AxisY.WholeRange.AutoSideMargins = False
            diagram.AxisY.WholeRange.SideMarginsValue = 0

            ' Customize the legend.
            stackedBarChart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True
            stackedBarChart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center
            stackedBarChart.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside
            stackedBarChart.Legend.MaxVerticalPercentage = 20

            ' Add a title to the chart (if necessary).
            stackedBarChart.Titles.Add(New ChartTitle())
            stackedBarChart.Titles(0).Text = "Population: Age-Gender Structure"
            stackedBarChart.Titles(0).WordWrap = True

            ' Add the chart to the form.
            stackedBarChart.Dock = DockStyle.Fill
            Me.Controls.Add(stackedBarChart)
        End Sub
        Private Sub OnChartBoundDataChanged(ByVal sender As Object, ByVal e As EventArgs)
            For Each series As Series In stackedBarChart.Series
                If Not (TypeOf series.Tag Is GenderAgeInfo) Then
                    Return
                End If
                Dim item As GenderAgeInfo = DirectCast(series.Tag, GenderAgeInfo)
                Dim view As SideBySideFullStackedBarSeriesView = TryCast(series.View, SideBySideFullStackedBarSeriesView)
                If view Is Nothing Then
                    Return
                End If
                view.StackedGroup = item.Gender
            Next series
        End Sub
    End Class
End Namespace