【发布时间】:2019-03-16 02:09:24
【问题描述】:
我无法在现有图表的第二个轴(右轴)上添加一条线。有没有办法在 POI 4.0.0/1 中使用 Charts 的新实现来做到这一点?
所需的输出将如下所示(带有 2 个轴的简单 excel 图表): .以该图表的关联数据为例:
系列 1/Axis1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
系列 2/Axis2 = [200,300,400,500,600,700,800,900,1000]
这是我目前在 Java 中尝试的代码,它主要是从 LineChart.java example 复制而来的
//Initial code instantiates a document
XWPFDocument doc = new XWPFDocument();
...
// Generate Chart
// This was taken from the example https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java
XWPFChart prChart = doc.createChart();
//Values 1 on the Left Axis
//Values 2 on the Right Axis
String[] categories = dates.toArray(new String[dates.size()]);
BigDecimal[] values1 = prices1.toArray(new BigDecimal[prices1.size()]);
BigDecimal[] values2 = prices2.toArray(new BigDecimal[prices2.size()]);
XDDFChartAxis bottomAxis = prChart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setMajorTickMark(AxisTickMark.NONE);
XDDFValueAxis leftAxis = prChart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setMajorTickMark(AxisTickMark.OUT);
/*
* Is this made correctly?
*/
XDDFValueAxis rightAxis = prChart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
rightAxis.setMajorTickMark(AxisTickMark.IN);
final int numOfPoints = categories.length;
final String categoryDataRange = prChart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = prChart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final String valuesDataRange2 = prChart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1);
final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2);
XDDFLineChartData line = (XDDFLineChartData) prChart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) line.addSeries(categoriesData, valuesData);
series1.setTitle("Price", null);
series1.setSmooth(true);
series1.setMarkerStyle(MarkerStyle.NONE);
solidLineSeries(series1, PresetColor.BLUE_VIOLET);
// Am I adding the rightAxis correctly here?
XDDFLineChartData line2 = (XDDFLineChartData) prChart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) line2.addSeries(categoriesData, valuesData2);
series2.setTitle("Index", null);
series2.setSmooth(true);
series2.setMarkerStyle(MarkerStyle.NONE);
solidLineSeries(series2, PresetColor.BLACK);
prChart.plot(line);
prChart.plot(line2); /// <- Does this add to the same plot correctly?
prChart.displayBlanksAs(DisplayBlanks.GAP);
运行此代码不会产生任何编译错误。但是在打开文档“内容有问题”时,我确实遇到了错误。
我想我没有正确添加第二条线和第二条轴。
有没有办法做到这一点?
更新 w。解决方案 Axel 下面的解决方案非常有效。要知道的其他信息正是问题所在。
我也想知道你添加到情节中的顺序,这希望对其他人有所帮助
- 创建第一组轴
- 创建第一行
- 绘制第一行
- 创建新轴
- 创建第二行
- 绘制第二行
- 更新轴 ID!
【问题讨论】:
标签: java charts apache-poi