java - POI 4.1.0 折线图 y 系列显示错误的图例

标签 java apache-poi

如何在 y 系列上创建带有单个图例的折线图,而不是在 x 系列上显示多个图例

int rows = numberOfRows - 1;
int cols = headers.size();

XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
XSSFChart chart = drawing.createChart(anchor);
chart.displayBlanksAs(DisplayBlanks.GAP);

XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);

// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setOrientation(AxisOrientation.MAX_MIN);
bottomAxis.setTitle("Date");

XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setTitle("Rates");
rightAxis.setCrosses(AxisCrosses.AUTO_ZERO);

XDDFChartLegend chartLegend = chart.getOrAddLegend();
chartLegend.setPosition(LegendPosition.TOP_RIGHT);
chartLegend.setOverlay(false);

XDDFLineChartData lineChartData = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);

XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, rows, 0, 0));

for (int col = 1; col < cols; col++) {
    XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, rows, col, col));
    XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) lineChartData.addSeries(xs, ys1);
    series1.setTitle(headers.get(col), null);
    series1.setSmooth(false);
    series1.setMarkerStyle(MarkerStyle.NONE);
}

chart.plot(lineChartData);

enter image description here

上图显示了我当前的绘图折线图,y 系列预计只有 1 个图例,但显示了多个

enter image description here

上图折线图是使用Excel绘制的,y系列仅显示1个图例

最佳答案

有一个设置可以改变图表中同系列数据标记的颜色。请参阅:Vary the colors of same-series data markers in a chart 。在 Excel 2007 之前,默认情况下该值为 false。现在,Microsoft决定将该设置设置为默认true。因此,如果您不想要它,则需要明确设置 false

就您而言:

...
XDDFLineChartData lineChartData = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
lineChartData.setVaryColors(false);
...

关于java - POI 4.1.0 折线图 y 系列显示错误的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352058/

相关文章:

java - 如何使用 Apache POI 在 Word 中添加图像标题

java - mvn 测试不适用于 Apache Poi

java - 将特定单元格宽度设置为 XSSF Apache POI 中的列

Java 打印线程卡住 : WHY?

java - GWT 中的 Hibernate 序列化异常但 Eclipselink 中没有

JAVA和JODA Date解析即使格式错误也不会抛出异常

java - 新值 EditText 未显示,没有任何错误

java - 使用 OpenCV 从图像序列中获取中值图片

java - JAVA从Excel表格中获取特定数据

java - 如何使用java在工作簿中创建Excel工作表?