gpt4 book ai didi

java - 如何在 Apache POI XSSFChart 中旋转文本标签

转载 作者:行者123 更新时间:2023-12-01 18:32:02 25 4
gpt4 key购买 nike

我正在尝试使用 Apache POI 库生成包含酒吧聊天的 Excel 文档。我想旋转 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轴上旋转文本吗?

最佳答案

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()
...

关于java - 如何在 Apache POI XSSFChart 中旋转文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60145128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com