Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache POI设置Excel工作表散点图标记图标的颜色_Java_Excel_Charts_Apache Poi_Scatter Plot - Fatal编程技术网

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

Java 使用Apache POI设置Excel工作表散点图标记图标的颜色,java,excel,charts,apache-poi,scatter-plot,Java,Excel,Charts,Apache Poi,Scatter Plot,显示了如何设置线的颜色(连接系列中的标记),但我不知道如何为系列设置标记的颜色。我看到我可以更改标记图标(使用setMarkerStyle(),但它似乎没有可编辑的颜色属性) 我怀疑有必要禁用“不同颜色”配置,但我仍然不知道在该步骤之后如何设置颜色(如果以下行是必需的) ((XSSFChart)图表).getCTChart().getPlotArea().getScatterChartArray(0) .addNewVaryColors().setVal(false) 如何在下面的示例中指定标记

显示了如何设置线的颜色(连接系列中的标记),但我不知道如何为系列设置标记的颜色。我看到我可以更改标记图标(使用
setMarkerStyle
(),但它似乎没有可编辑的颜色属性)

我怀疑有必要禁用“不同颜色”配置,但我仍然不知道在该步骤之后如何设置颜色(如果以下行是必需的)

((XSSFChart)图表).getCTChart().getPlotArea().getScatterChartArray(0)
.addNewVaryColors().setVal(false)

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

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://stackoverflow.com/questions/32010765
            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://stackoverflow.com/questions/21855842
            series1.setSmooth(false); // https://stackoverflow.com/questions/39636138
            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);
    }
}
公共类散点图{
公共静态void main(字符串[]args)引发IOException{
尝试(XSSFWorkbook wb=new XSSFWorkbook()){
XSSFSheet sheet=wb.createSheet(“第1张”);
行的最终整数=3;
_列的最终int NUM_=10;
//创建一行并在其中放置一些单元格。行基于0。
行行;
细胞;
for(int-rowIndex=0;rowIndex
您链接的上一个问题来自2018年5月,是关于从
ApachePOI
3.17
版本的纯
XSSFChart
内容。这已经过时了,因为
apachepoi4.0.0
引入了新的
xdf
东西

但是,直到现在,仅使用高级
XDDF
类才支持设置标记颜色。标记具有与系列本身具有相同类型的填充特性的形状特性。因此,我们可以使用
XDDFSolidFillProperties
XDDFShapeProperties
作为行设置。但是为了获得标记,我们需要使用底层的
ooxml-schemas-1.4
bean

例如:

...
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());
...

效果很好,但标记边框颜色仍设置为默认颜色。改变边框颜色也是一个类似的过程吗?是的,我知道了<代码>XDDFNoFillProperties noFill=新的XDDFNoFillProperties()
XDDFShapeProperties属性标记器=新XDDFShapeProperties()
propertiesMarker.setFillProperties(fillMarker)
XDDFLineProperties lineProperties=新的XDDFLineProperties()
lineProperties.setFillProperties(noFill)
propertiesMarker.setLineProperties(lineProperties)