How to display numbers inside the bars

Dear Aspose friends,

I have a silly question, how can I put the percentage numbers inside the bars of a stacked bar chart?

Best Regards

John

Hi John,

I have observed the requirements shared by you and like to share that Aspose.Slides allows you to set the number format for the text in chart. Please visit this documentation link where by you will find example for setting the number format to percentage. You can use the same approach shared for chart series labels as well.

Many Thanks,

Hi,

Actually, I mean how to display the numbers as attached ppt bar chart.

John

Hi John,

I have observed the requirement shared by you and like to share that you are actually wishing to set the labels with total and percent value. I have created a sample application code that describes the percentage for every series in a category and disaplay the values of labels in percentage. You can use the same concept in your application and apply the values as per your convenience. I am hopeful that the following code will serve the purpose for you.


public static void testLabels()
{
PresentationEx pres = new PresentationEx();
SlideEx slide = pres.getSlides().get_Item(0);

ChartEx chart = slide.getShapes().addChart(ChartTypeEx.StackedBar , 20, 20, 400, 400);
// ChartEx chart = slide.Shapes.AddChart(ChartTypeEx.StackedColumn, 20, 20, 400, 400);
ChartSeriesEx series = chart.getChartData().getSeries().get_Item(0);
ChartCategoryEx cat;
double total_value = 0.0f;

double[] total_for_Cat = new double[chart.getChartData().getCategories().size()];
for (int k = 0; k < chart.getChartData().getCategories().size(); k++)
{
cat = chart.getChartData().getCategories().get_Item(k);

for (int i = 0; i < chart.getChartData().getSeries().size(); i++)
{
Object tex=chart.getChartData().getSeries().get_Item(i).getValues().get_Item(k).getValue();
double val;
if(tex instanceof Double)
{
val=Double.parseDouble(tex.toString());

}
// String ss=(String)tex;

total_for_Cat[k] = (double)total_for_Cat[k] + Double.parseDouble(chart.getChartData().getSeries().get_Item(i).getValues().get_Item(k).getValue().toString());
}
}

double dataPontPercent = 0f;

for (int x = 0; x < chart.getChartData().getSeries().size(); x++)
{
series = chart.getChartData().getSeries().get_Item(x);
series.getLabels().setShowLegendKey( false);

for (int j = 0; j < series.getValues().getCount(); j++)
//for (int j = 0; j < series.Values.Count; j++)
{
DataLabelEx label = new DataLabelEx(series);
PortionEx port = new PortionEx();
label.setId(j);
// label.ShowValue = true;
// dataPontPercent = ((double)series.getValues().get_Item(j).getValue() / total_for_Cat[j]) * 100;
dataPontPercent = ( Double.parseDouble(series.getValues().get_Item(j).getValue().toString()) / total_for_Cat[j]) * 100;

// port.setText(String.format("{0:F2} %", dataPontPercent));
port.setText(String.format("%.2f", dataPontPercent)+" %");
port.getPortionFormat().setFontHeight(8f);
label.getTextFrame().getParagraphs().add(new ParagraphEx());
label.getTextFrame().getParagraphs().get_Item(0).getPortions().add(port);
series.getLabels().add(label);
label.setShowSeriesName (false);
label.setShowPercentage ( false);
label.setShowLegendKey (false);
label.setShowCategoryName ( false);
label.setShowBubbleSize ( false);

}
series.hasSeriesLines (false);
}

// Save presentation with chart
pres.write(“D:\Aspose Data\AsposeChart.pptx”);
}


Many Thanks,