我想将图表添加到 XWPFDocument 中的特定表格单元格。我想要表格单元格中的图表,以便我可以确保与以后添加的其他元素对齐。那么,
如何在不将其添加到文档的情况下创建 XWPF/XDDF 图表(例如,不使用 document.createChart())?
如何获取该图表并将其添加到特定段落/运行(例如,在表格单元格中创建的那个)?
已经尝试过:
- 创建一个图表镜像XWPFDocument.createChart()中的代码
- 将XWPFRun.addChart()与我使用的 R elationPart.getRelationship.getId()一起使用
- 到达表格的具体位置后使用document.createChart()
- 试图制作图表 - > XDDFDrawing 并将其添加到run.getCTR.addDrawing的运行中......我不认为它会这样吗?
/代码示例
// Create a document with some initial text
XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("text");
tmpRun.setFontSize(18);
// Try making the chart
// the same code as here, https://stackoverflow.com/questions/55192804/how-do-i-add-a-second-line-with-a-second-axis-to-an-xddfchart-in-poi-4-0-1
try{
String[] categories = new String[]{"1","2","3","4","5","6","7","8","9"};
Double[] values1 = new Double[]{1d,2d,3d,4d,5d,6d,7d,8d,9d};
Double[] values2 = new Double[]{200d,300d,400d,500d,600d,700d,800d,900d,1000d};
// create the chart
// XWPFChart chart = document.createChart(7* Units.EMU_PER_CENTIMETER, 7*Units.EMU_PER_CENTIMETER);
// Try to make a chart stand alone
// using the same code as here,
// https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?view=markup
int chartNumber = document.getCharts().size();
POIXMLDocumentPart.RelationPart rp = document.createRelationship(XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false);
XWPFChart chart = rp.getDocumentPart();
chart.setChartIndex(chartNumber);
chart.setChartBoundingBox(7* Units.EMU_PER_CENTIMETER, 7*Units.EMU_PER_CENTIMETER);
document.getCharts().add(chart);
// This stuff to make the chart is not part of the question
// create data sources
int numOfPoints = categories.length;
String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
String valuesDataRange1 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFNumericalDataSource<Double> valuesData1 = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange1, 1);
XDDFNumericalDataSource<Double> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2);
// first line chart
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesData1);
chart.plot(data);
solidLineSeries(data, 0, PresetColor.BLUE);
// second line chart
// bottom axis must be there but must not be visible
bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setVisible(false);
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
// set correct cross axis
bottomAxis.crossAxis(rightAxis);
rightAxis.crossAxis(bottomAxis);
data = chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
series = data.addSeries(categoriesData, valuesData2);
chart.plot(data);
// correct the id and order, must not be 0 again because there is one line series already
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getIdx().setVal(1);
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getOrder().setVal(1);
solidLineSeries(data, 0, PresetColor.RED);
// End of extra stuff
// Back to question
// Add the chart by relation id
XWPFParagraph p2 = document.createParagraph();
XWPFRun r2 = p2.createRun();
r2.addChart(rp.getRelationship().getId());
// Add a new run to try to add a new drawing?
XWPFRun r3 = p2.createRun();
CTDrawing drawing = r3.getCTR().addNewDrawing();
????
}catch(Exception e){}
当我通过 r2.addChart() 添加图表时,什么都没有显示?所以也许我没有正确创建图表?或者我没有正确地将它添加到运行中?
是否可以将图表转换为绘图?
- 段落
- 跑
- 绘画?
- 图表?