Problem with stacked bars

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

Posts: 20
Joined: Tue Jun 28, 2016 7:54 am

Problem with stacked bars

Post by jorge » Wed Aug 24, 2016 9:02 am


I am trying to draw a stacked bars chart but I am facing some issues.

This is what I would like to achieve: I have, let's say, 3 stacks bars. So I would like 3 stacks in the chart. Each stack is composed of 4 values, all these values sum up 100%. So I have 3 stacks, with 4 values per stack. Also, the legend box should have 3 entries, one per stack.

I have tried different approaches:

- Create 3 BarSeries, each of which containing 4 BarSeriesValue, and grouping ByLocation. So each BarSeries will use a different location value for its BarSeriesValues. This way I can see 3 stacks in the chart, but only the first BarSeriesValue is shown, instead of 4.

- Create 3 BarSeries, each of which containing 4 BarSeries, each of which containing 1 BarSeriesValue. Grouping the same way as above. This way I can see the bars correctly, 3 bars each of which containing 4 elements. But there are 3x4 elements in the legend box.

- Group ByIndex. This way it seems to use the BarSeriesValue index to decide in which stack the block goes. I find this way of grouping confusing, as the elements of a BarSeries are displayed in different stacks, so they end up displayed horizontally instead of stacked. I guess I could use 3 different indices (1 per stack), and 4 BarSeries, each of which containing these 3 BarSeriesValues. But the legend instead of showing the names of what I want in the stacks, would show the name of the possible values in the stacks.

Am I missing anything about how stacked bars work? Is there a better way of doing what I try to achieve?


User avatar
Posts: 1366
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Re: Problem with stacked bars

Post by ArctionPasi » Wed Aug 24, 2016 6:36 pm

I didn't completely understand how you want to apply the colors etc.

E.g. with this code:

Code: Select all


            ViewXY v = lightningChartUltimate1.ViewXY;
            v.XAxes[0].ValueType = AxisValueType.Number;

            int seriesCount = 4;
            int valuesCount = 3; 

            for(int s =0; s < seriesCount; s++)

                BarSeries series = new BarSeries(v, v.XAxes[0], v.YAxes[0]);
                BarSeriesValue[] vals = new BarSeriesValue[valuesCount];
                for(int b=0; b< valuesCount ; b++)
                    vals[b].Value = (s + 1) + b;
                series.Values = vals;
                series.Fill.Color = DefaultColors.SeriesForBlackBackground[s];
                series.Fill.GradientFill = GradientFill.Solid;  
            v.BarViewOptions.Grouping = BarsGrouping.ByIndex;
            v.BarViewOptions.Stacking = BarsStacking.StackStretchToSum;
            v.BarViewOptions.StackSum = 100; 
This kind of bar chart appears:
BarSeriesGroupingStacking.jpg (64.51 KiB) Viewed 6379 times

Note you can set series.ShowInLegendBox = false for series you don't want to show in the legedbox.

The most flexible solution to do any bar chart, is to use one PolygonSeries for each bar... One polygon point for every corner. Might be quite time consuming, though, but in many cases the only approach when special layout is required. ;)
LightningChart Support Team, PT

Posts: 20
Joined: Tue Jun 28, 2016 7:54 am

Re: Problem with stacked bars

Post by jorge » Thu Aug 25, 2016 2:34 pm

Hi Parsi,

Thanks, it is working now.