Hi,
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?
Thanks
Problem with stacked bars
Moderator: Queue Moderators
- ArctionPasi
- Posts: 1367
- Joined: Tue Mar 26, 2013 10:57 pm
- Location: Finland
- Contact:
Re: Problem with stacked bars
I didn't completely understand how you want to apply the colors etc.
E.g. with this code:
This kind of bar chart appears:
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.
E.g. with this code:
Code: Select all
lightningChartUltimate1.BeginUpdate();
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.BarSeries.Add(series);
}
v.BarViewOptions.Grouping = BarsGrouping.ByIndex;
v.BarViewOptions.Stacking = BarsStacking.StackStretchToSum;
v.BarViewOptions.StackSum = 100;
lightningChartUltimate1.EndUpdate();
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
Re: Problem with stacked bars
Hi Parsi,
Thanks, it is working now.
Thanks, it is working now.