Stacked bars are created by using several instances of the BarSeries class. The first bar series created must have its MultiBarMode property set to MultiBarMode.Series. The MultiBarMode property of the subsequent bar series must be set to MultiBarMode.Stacked. Stacked bar series are displayed on top of each other. The following figure shows a typical stack bar chart:
figure 1.
Creating the Stacked Bar Series
The following example demonstrates how to create a stack bar chart with two stacks:
VB.NET | |
---|---|
|
C# | |
---|---|
// there is one chart created by default BarSeries bar1 = (BarSeries)chart.Series.Add(SeriesType.Bar); bar1.MultiBarMode = MultiBarMode.Series; |
If you want to create a second stack bar with two stacks behind the stack bar created in the previous sample, you must add the following code:
VB.NET | |
---|---|
|
C# | |
---|---|
BarSeries bar4 = (BarSeries)Chart.Series.Add(SeriesType.Bar); bar4.MultiBarMode = MultiBarMode.Series; |
Stack Origin
When a bar series is stacked it is always displayed with origin 0, regardless of the values of the UseOrigin and Origin properties.
Stacks with Negative Values
Stacked bar charts support negative values, which means that if you insert negative values in the Values data series, the respective stacks will be piled below the zero. In this case since the values for a concrete category can be both positive and negative the chart will display two piles for each category: one for the positive values, which grows above the zero, and another one for the negative values, which grows below the zero.
The following figure demonstrates a typical stack bar chart with negative and positive values:
figure 2.
Stacked Bar Formatting Commands
The following formatting commands inherited from the Series class have different meanings when a bar series is stacked:
<total> - Displays the positive or negative value sum of the values in the current stack. If the current value is negative, the negative sum is displayed. Otherwise, the positive sum is displayed.
<cumulative> - Displays the sum of the values up to the current stack value.
<percent> - Displays the percent contribution of the value to the total pile sum.
Related Examples
Windows Forms: Series\Bar\Stacked Bar