как повернуть текстовые метки в Apache POI XSSFChart - PullRequest
0 голосов
/ 10 февраля 2020

Я пытаюсь сгенерировать документ Excel, который содержит чаты с использованием Apache библиотеки POI. Я хочу повернуть текст в метках оси X, но нет возможности повернуть текст. Это то, что я пробовал до сих пор

XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing
                .createAnchor(0, 0, 0, 0, graphStartColumn, graphStartRow, graphEndColumn, graphEndRow);
 XSSFChart chart = drawing.createChart(anchor);

XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle(report.getX().getLegend());
bottomAxis.getOrAddTextProperties().setBold(true);
// here I'm trying to add text rotation to x-axis labels, but I don't see an option in axis

XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(report.getY().get(0).getLegend());
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

Кто-нибудь знает, как вращать текст по оси X?

1 Ответ

0 голосов
/ 10 февраля 2020

Это еще не поддерживается XDDFCategoryAxis. Но, конечно, базовые классы ooxml-schemas поддерживают это.

Таким образом, мы можем получить org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody от оси категории и установить там свойство поворота. Из-за настройки bottomAxis.getOrAddTextProperties() мы можем быть уверены, что уже есть TxPr, а также BodyPr. Поэтому нам не нужно проверять null для них.

Только дополнительным вызовом являются значения вращения. Мы можем установить вращение от 0 до 90 градусов и от 0 до -90 градусов. Там 90 градусов это значение 5400000.

Пример:

...
  bottomAxis.getOrAddTextProperties().setBold(true);

  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(bottomAxis);
  org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody text = ctCatAx.getTxPr(); // there is a TxPr already because of bottomAxis.getOrAddTextProperties()

  int rotAngle = 0;
  //int plus90Deg = 5400000; rotAngle = plus90Deg;
  //int minus90Deg = -5400000; rotAngle = minus90Deg;
  //int plus45Deg = (int)Math.round(5400000/2d); rotAngle = plus45Deg;
  int minus45Deg = (int)Math.round(-5400000/2d); rotAngle = minus45Deg;

  text.getBodyPr().setRot(rotAngle); // there is a BodyPr already because of bottomAxis.getOrAddTextProperties()
...
...