Free Support Forum - aspose.com

Change font and text format of line chat data label

Hi,

I am able to set the alignment to center, and set the value of my data labels but my attempts to change the font size, color, font etc. do not take affect. Please could you advise my error?

	IChartDataWorkbook workBook = chart.getChartData().getChartDataWorkbook();
	chart.getChartData().getSeries().clear();
	chart.getChartData().getCategories().clear();		
	chart.setLegend(false);

	chart.getAxes().getVerticalAxis().setVisible(false);
	chart.getAxes().getHorizontalAxis().setVisible(false);
	chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
	chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
	chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
	chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

	chart.getChartData().getCategories().add(workBook.getCell(0, 1, 0, "320"));
	chart.getChartData().getCategories().add(workBook.getCell(0, 2, 0, "240"));

	IChartSeries series = chart.getChartData().getSeries().add(workBook.getCell(0, 0, 1, "MY SERIES"),chart.getType());
	chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setOverlap((byte) 0);
	chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setGapWidth(20);	
	IChartDataPoint dP = series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 1, 1, 340));
	dP.getLabel().getTextFrameForOverriding().setText("NUMBER IS 340");

	dP.getLabel().getDataLabelFormat().setPosition(LegendDataLabelPosition.Center);
	dP.getLabel().getDataLabelFormat().getTextFormat().getPortionFormat().setFontBold(NullableBool.True);
	dP.getLabel().getDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont (new FontData("DINPro-Bold"));
	dP.getLabel().getDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(14);
	dP.getLabel().getDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().setFillType (FillType.Solid );
	dP.getLabel().getDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor (Color.WHITE);

Thanks,
Andy

@andysteady,

I suggest you to please add following modification to your sample code to serve the purpose. I hope the shared code shall work.

ITextFrame text= series.getDataPoints().get_Item(0).getLabel().addTextFrameForOverriding("Test Label");  
IPortionFormat portionformat=   text.getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
portionformat.setFontBold(NullableBool.True);
portionformat.setLatinFont (new FontData("DINPro-Bold"));
portionformat.setFontHeight(14);
portionformat.getFillFormat().setFillType (FillType.Solid );
portionformat.getFillFormat().getSolidFillColor().setColor (Color.WHITE);
portionformat.getFillFormat().getSolidFillColor().setColor (Color.RED);

Hi,

Many thanks for your response. However it’s still not applying the font changes, anything else you can suggest?

Kind regards,
Andy

@andysteady,

You need to make sure that the font that you are trying to set must be installed in your system. For sake of confirmation, you can apply some other font like Arial or Times New Roman in your application.

Hi,

Thanks for the suggestion but I’m already using that font when setting the label on another chart.
I tried Arial and no luck. Also its not just the font that is not set, but also the font color, size etc.

All the settings work if I set them on

series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat()

But then if I do that, when I set specific text for one label using getTextFrameForOverriding().setText() the text formatting reverts again. If you can show me how to edit the label text without losing the format that would also be fine.

Any help much appreciated

Andy

@andysteady,

I have observed your comments and like to share that when you override text frame for setting custom text, it returns you TextFrame object. In this case all the default formatting for that text frame will be lost and you have to set the text formatting for overridden text frame your self they way I have shared with you earlier. I have used Arial Black font in the same sample code example that I have shared earlier with you and it worked fine without any issue on my end. For your convenience, I have attached the generated presentation as well.

bar_chart.pptx.zip (28.9 KB)

Hi,

Thanks, I’m sure you’re right, I just can’t see what I’m doing wrong. I’ve upgraded to 18.5 just to be sure. Would you mind sharing with me your entire Java program that created the ppt which you shared with me?

My entire code looks like this, in case you can see any issue?

IChart chart = slide.getShapes().addChart(ChartType.ClusteredBar,
Utils.getPixelValue(2.42f),//x
Utils.getPixelValue(15.82f),//y
Utils.getPixelValue(17.44f),//width
Utils.getPixelValue(4.48f),//height
false);

IChartDataWorkbook workBook = chart.getChartData().getChartDataWorkbook();
IChartSeries series = chart.getChartData().getSeries().add(workBook.getCell(0, 0, 1, “MY SERIES”),chart.getType());

chart.getChartData().getCategories().add(workBook.getCell(0, 1, 0, “X”));
chart.getChartData().getCategories().add(workBook.getCell(0, 2, 0, “Y”));
series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 1, 1, 240));
series.getDataPoints().addDataPointForBarSeries(workBook.getCell(0, 2, 1, 340));

ITextFrame text= series.getDataPoints().get_Item(0).getLabel().addTextFrameForOverriding(“Test Label”);
IPortionFormat portionformat= text.getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
portionformat.setFontBold(NullableBool.True);
portionformat.setLatinFont (new FontData(“Arial”));
portionformat.setFontHeight(14);
portionformat.getFillFormat().setFillType (FillType.Solid );
portionformat.getFillFormat().getSolidFillColor().setColor (Color.WHITE);

text= series.getDataPoints().get_Item(1).getLabel().addTextFrameForOverriding(“Test Label2”);
portionformat= text.getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
portionformat.setFontBold(NullableBool.True);
portionformat.setLatinFont (new FontData(“Arial”));
portionformat.setFontHeight(14);
portionformat.getFillFormat().setFillType (FillType.Solid );
portionformat.getFillFormat().getSolidFillColor().setColor (Color.WHITE);

Thanks again for your help,
Andy

@andysteady,

I have used the attached sample code on my end in order to generate the chart that I have shared earlier with you. I hope this will be helpful.

barchart.zip (1.3 KB)

Hi,

Thanks very much - I found that I was missing this line of code which made it work:

series.getDataPoints().get_Item(0).getLabel().getTextFormat().getTextBlockFormat().setAutofitType(TextAutofitType.Shape);

Thanks for your help,
Andy

@andysteady,

Its good to know things are finally working on your end. Please share, if I may help you further in this regard.

Hi,

Thanks… see attached picture, could you advise if it is possible to resize the label box so I can make it much wider and the text won’t wrap?

image.png (1.4 KB)

Kind regards,
Andy

@andysteady,

I have observed your comments. in this case you can try

a) call TextFrameFormat.setWrapText()
b) for textframe holder, i.e. autoshape, call setWidth
c) for textframe holder set TextFrameFormat.AutofitType to suitable

Please share feedback with us if there is still an issue.

Hi,

The setWrapText helped, thanks, but I still need to adjust the width and if possible move it a bit too.
I couldn’t find what you mean by “textframe holder” or the setWidth method, any further insight would be much appreciated.

Thanks,
Andy

@andysteady,

I suggest to please visit this API reference link for available methods associated with DataLabel which includes method for setting width, height and position of label. I hope this will be helpful.