使用Apache POI设置Excel表散点图标记图标的颜色

4

Apache POI中包含的散点图示例展示了如何设置系列中连接标记的线条颜色,但我无法弄清楚如何设置系列中标记的颜色。我看到可以使用setMarkerStylejavadoc)更改标记图标,但似乎没有可编辑的颜色属性。

以前的SO问题表明,可能需要禁用变化颜色配置,但即使执行此步骤,我仍然不知道如何设置颜色(如果以下行甚至是必要的话)。

在下面的示例中,我该如何指定标记图标的颜色?

public class ScatterChart {

    public static void main(String[] args) throws IOException {
        try (XSSFWorkbook wb = new XSSFWorkbook()) {
            XSSFSheet sheet = wb.createSheet("Sheet 1");
            final int NUM_OF_ROWS = 3;
            final int NUM_OF_COLUMNS = 10;

            // Create a row and put some cells in it. Rows are 0 based.
            Row row;
            Cell cell;
            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
                row = sheet.createRow((short) rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                    cell = row.createCell((short) colIndex);
                    cell.setCellValue(colIndex * (rowIndex + 1.0));
                }
            }

            XSSFDrawing drawing = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            XSSFChart chart = drawing.createChart(anchor);
            XDDFChartLegend legend = chart.getOrAddLegend();
            legend.setPosition(LegendPosition.TOP_RIGHT);

            XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
            bottomAxis.setTitle("x"); // https://dev59.com/io7da4cB1Zd3GeqP8heA
            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
            leftAxis.setTitle("f(x)");
            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

            XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));


            XDDFScatterChartData data = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
            XDDFScatterChartData.Series series1 = (XDDFScatterChartData.Series) data.addSeries(xs, ys1);
            series1.setTitle("2x", null); // https://dev59.com/Ynzaa4cB1Zd3GeqPSaKc
            series1.setSmooth(false); // https://dev59.com/h5rga4cB1Zd3GeqPu_zm
            XDDFScatterChartData.Series series2 = (XDDFScatterChartData.Series) data.addSeries(xs, ys2);
            series2.setTitle("3x", null);
            chart.plot(data);

            solidLineSeries(data, 0, PresetColor.CHARTREUSE);
            solidLineSeries(data, 1, PresetColor.TURQUOISE);

            // Write the output to a file
            try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
                wb.write(fileOut);
            }
        }
    }

    private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
        XDDFLineProperties line = new XDDFLineProperties();
        line.setFillProperties(fill);
        XDDFChartData.Series series = data.getSeries().get(index);
        XDDFShapeProperties properties = series.getShapeProperties();
        if (properties == null) {
            properties = new XDDFShapeProperties();
        }
        properties.setLineProperties(line);
        series.setShapeProperties(properties);
    }
}
1个回答

5
你链接的上一个问题来自2018年5月,关于apache poi 3.17版本及以下中纯粹的XSSFChart内容。这已经过时了,因为在apache poi 4.0.0中引入了新的XDDF内容。
尽管如此,仅使用高级XDDF类仍不支持设置标记颜色。标记具有形状属性,其填充属性与系列本身相同。因此,我们可以像线条设置一样使用XDDFSolidFillProperties和XDDFShapeProperties。但要获取标记,我们需要使用底层的ooxml-schemas-1.4 beans。
示例:
...
series2.setMarkerStyle(MarkerStyle.DIAMOND);
series2.setMarkerSize((short)15);
XDDFSolidFillProperties fillMarker = new XDDFSolidFillProperties(XDDFColor.from(new byte[]{(byte)0xFF, (byte)0xFF, 0x00}));
XDDFShapeProperties propertiesMarker = new XDDFShapeProperties();
propertiesMarker.setFillProperties(fillMarker);
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(1).getMarker()
     .addNewSpPr().set(propertiesMarker.getXmlObject());
...

工作得很好,但标记边框颜色仍设置为默认颜色。更改边框颜色是否是类似的过程? - Ben Holland
3
好的,我明白了。XDDFNoFillProperties noFill = new XDDFNoFillProperties(); XDDFShapeProperties propertiesMarker = new XDDFShapeProperties(); propertiesMarker.setFillProperties(fillMarker); XDDFLineProperties lineProperties = new XDDFLineProperties(); lineProperties.setFillProperties(noFill); propertiesMarker.setLineProperties(lineProperties); - Ben Holland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接