java - 如何在 Apache POI XSSFChart 中旋转文本标签
问题描述
我正在尝试使用 Apache POI 库生成一个包含 Bar 聊天的 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()
...
推荐阅读
- regex - 正则表达式中的逗号分隔值
- python - 在 django 中打印 pdf 时出现此错误,错误状态如下模块“'urllib.request' has no attribute 'splithost'”
- c - OpenGL三角形旋转错误
- python - 如何查找源 URL 以抓取网站
- android - Square POS API - 交易与支付 + 已弃用的端点?
- jquery-mobile - 如果网站是网络网站,如何应用 data-mini="true"(而不是在移动设备上应用)
- wordpress - 在 Wordpress 媒体库中搜索多个文件
- c# - 将 ASP.NET Core razor 页面中的下拉列表绑定到 ado.net 存储过程
- python - Selenium 找不到元素,尽管 xpath 在开发人员工具中工作
- dataweave - 有没有办法使用 mule 4 中的 dataweave 2.0 在 mule 4 中合并两个管道分隔的 Csv 文件