At least for the legend's font size setting using org.apache.poi.xddf.usermodel.text.XDDFTextBody
is possible. That has the advantage that this is a high level apache poi
class which gets developed further. So if you have XDDFChartLegend legend
, then construct a XDDFTextBody
from this and use this for font settings.
Example:
...
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
XDDFTextBody legendTextBody = new XDDFTextBody(legend);
legendTextBody.getXmlObject().addNewBodyPr();
legendTextBody.addNewParagraph().addDefaultRunProperties().setFontSize(8d);
legend.setTextBody(legendTextBody);
...
For axis font settings using the low level org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx
or org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx
is necessary. But there also is org.apache.poi.xddf.usermodel.chart.XDDFTitle
which is the high level wrapper for CTValAx
-title or CTCatAx
-title. So we should using that instead of directly using the CT*
classes.
Example:
Do having XDDFTitle getOrSetAxisTitle
methods:
private static XDDFTitle getOrSetAxisTitle(XDDFValueAxis axis) {
try {
java.lang.reflect.Field _ctValAx = XDDFValueAxis.class.getDeclaredField("ctValAx");
_ctValAx.setAccessible(true);
org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx ctValAx =
(org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx)_ctValAx.get(axis);
if (!ctValAx.isSetTitle()) {
ctValAx.addNewTitle();
}
XDDFTitle title = new XDDFTitle(null, ctValAx.getTitle());
return title;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private static XDDFTitle getOrSetAxisTitle(XDDFCategoryAxis axis) {
try {
java.lang.reflect.Field _ctCatAx = XDDFCategoryAxis.class.getDeclaredField("ctCatAx");
_ctCatAx.setAccessible(true);
org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx ctCatAx =
(org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx)_ctCatAx.get(axis);
if (!ctCatAx.isSetTitle()) {
ctCatAx.addNewTitle();
}
XDDFTitle title = new XDDFTitle(null, ctCatAx.getTitle());
return title;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
Then do using those as so:
...
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
//bottomAxis.setTitle("...");
XDDFTitle title = getOrSetAxisTitle(bottomAxis);
title.setOverlay(false);
title.setText("...");
title.getBody().getParagraph(0).addDefaultRunProperties().setFontSize(8d);
bottomAxis.getOrAddTextProperties().setFontSize(8d);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
//leftAxis.setTitle("...");
title = getOrSetAxisTitle(leftAxis);
title.setOverlay(false);
title.setText("...");
title.getBody().getParagraph(0).addDefaultRunProperties().setFontSize(8d);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
leftAxis.getOrAddTextProperties().setFontSize(8d);
...