Free Support Forum - aspose.com

Clustered Bar Chart - Problems in labels formatting

Dear,


In a .pptx presentation generated using ASPose.Slide v 7.1 I added a ClusteredBar chart. I faced some problems in formatting the bar’s labels. Kindly find bellow the code I am using:

private void CreateSlide()
{
SlideEx sld = pres.Slides[0];

Aspose.Slides.Pptx.ChartEx lChart = sld.Shapes.AddChart(ChartTypeEx.ClusteredBar, 0, 0, 300, 200);

lChart.HasLegend = false;

lChart.ValueAxis.IsVisible = false;
lChart.ValueAxis.MajorGridLines.FillFormat.FillType = FillTypeEx.NoFill;
lChart.CategoryAxis.IsVisible = true;
lChart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low;

lChart.HasLegend = false;
lChart.HasTitle = false;

lChart.ChartData.Series.Clear();
lChart.ChartData.Categories.Clear();

ChartDataCellFactory fact = lChart.ChartData.ChartDataCellFactory;

lChart.ChartData.Series.Add(fact.GetCell(0, 0, 1, “”), lChart.Type);

lChart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, “C - 1”));
lChart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, “C - 2”));
lChart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, “C - 3”));
lChart.ChartData.Categories.Add(fact.GetCell(0, 4, 0, “C - 4”));
lChart.ChartData.Categories.Add(fact.GetCell(0, 5, 0, “C - 5”));
lChart.ChartData.Categories.Add(fact.GetCell(0, 6, 0, “C - 6”));

ChartSeriesEx series = lChart.ChartData.Series[0];
series.Format.Fill.FillType = FillTypeEx.Solid;

series.Values.Add(fact.GetCell(0, 1, 1, 20));
AddLegentValueToSeries(series, 1, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 0, Color.Blue);
series.Values.Add(fact.GetCell(0, 2, 1, 10));
AddLegentValueToSeries(series, 2, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 1, Color.Green);
series.Values.Add(fact.GetCell(0, 3, 1, 5));
AddLegentValueToSeries(series, 3, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 2, Color.Red);
series.Values.Add(fact.GetCell(0, 4, 1, 8));
AddLegentValueToSeries(series, 4, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 3, Color.Yellow);
series.Values.Add(fact.GetCell(0, 5, 1, -48));
AddLegentValueToSeries(series, 5, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 4, Color.Orange);
}
internal void AddLegentValueToSeries(ChartSeriesEx series, int id, int height, string fontName, System.Drawing.Color fontColor, LegendDataLabelPositionEx position)
{
DataLabelEx lbl = new DataLabelEx(series);
lbl.ShowValue = true;

lbl.Position = position;

PortionFormatEx pt = lbl.TextProperties.Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
pt.FontHeight = height;
pt.LatinFont = new FontDataEx(fontName);
pt.FillFormat.FillType = FillTypeEx.Solid;
pt.FillFormat.SolidFillColor.Color = fontColor;

lbl.Id = id;
series.Labels.Add(lbl);
}
internal void FormatSeriesPoint(ChartSeriesEx series, int index, Color color)
{
ChartPointEx point = new ChartPointEx(series);
point.Index = index;
point.Format.Fill.FillType = FillTypeEx.Solid;
point.Format.Fill.SolidFillColor.Color = color;
series.Points.Add(point);
}

The problem I am facing is as follow (I have attached a screenshot for each step in the bellow list for your reference):
1- When running the above code I get a chart with labels on all bars except last bar C-1 (Chart Label step 1.png)
2- In the “Format Data Series” window I realize the following (Chart Label step 2.png):
“Outside End” is selected in “Label Position” and not “Center”
“Values” checkbox is not checked
3- When I manually check the “Values” checkbox, the label for the last bar shows, but with a different format than the one specified in the code (Chart Label step 3.png)

Kindly share with me if there is any mistake in my code, although I am using almost the same code for different kind of charts and non shows a similar problem.

Thank you so much for your support
Sami Simon

Hi Sami Simon,


I have observed the sample code shared and like to share that there has been a minor issue your code. The issue 1 and 3 have get resolved by adding the following correct code in your sample application.

series.Values.Add(fact.GetCell(0, 1, 1, 20));
AddLegentValueToSeries(series, 0, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 0, Color.Blue);
series.Values.Add(fact.GetCell(0, 2, 1, 10));
AddLegentValueToSeries(series, 1, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 1, Color.Green);
series.Values.Add(fact.GetCell(0, 3, 1, 5));
AddLegentValueToSeries(series, 2, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 2, Color.Red);
series.Values.Add(fact.GetCell(0, 4, 1, 8));
AddLegentValueToSeries(series, 3, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 3, Color.Yellow);
series.Values.Add(fact.GetCell(0, 5, 1, -48));
AddLegentValueToSeries(series, 4, 12, “Arial”, Color.Black, LegendDataLabelPositionEx.Center);
FormatSeriesPoint(series, 4, Color.Orange);

series.Labels.Position = LegendDataLabelPositionEx.Center;
series.Labels.ShowCategoryName = false;
series.Labels.ShowPercentage = false;
series.Labels.ShowSeriesName = false;
series.Labels.ShowValue = false;

The issue 2 is in fact not an issue. Actually, you are setting the labels parameters independently. So, please verify the values based on individual labels rather than collectively. Please observe the Info.png for your kind reference.

Please share, if I may help you further in this regard.

Many Thanks,