自定义图-Apache POI

时间:2018-08-09 06:14:07

标签: java excel apache-poi linechart

我已经使用Apache POI生成了一个图形。我正确地获得了图形,但在边界线上,角是圆的。我需要将它们更改为正常的锐角。我怎样才能做到这一点? 我的代码如下。 (由于代码太长,我只发布了边框代码。如果有人需要完整代码,请发表评论)

XSSFSheet sheet4 = wb.createSheet("Graph");
        Drawing<?> drawing = sheet4.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 17, 27);

        Chart chart = drawing.createChart(anchor);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.RIGHT);

        LineChartData data = chart.getChartDataFactory().createLineChartData();

        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 0, 0));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 1, 1));
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 3, 3));
        ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 4, 4));
        ChartDataSource<Number> ys4 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 8, 8));

        LineChartSeries series1 = data.addSeries(xs, ys1);
        series1.setTitle("VAL1");
        LineChartSeries series2 = data.addSeries(xs, ys2);
        series2.setTitle("VAL2");
        LineChartSeries series3 = data.addSeries(xs, ys3);
        series3.setTitle("VAL3");
        LineChartSeries series4 = data.addSeries(xs, ys4);
        series4.setTitle("VAL4");

        chart.plot(data, bottomAxis, leftAxis);

        if (chart instanceof XSSFChart) { 


        XSSFChart xchart = (XSSFChart) chart;
        CTChart ctChart = xchart.getCTChart();
        CTTitle title = ctChart.addNewTitle();
        CTTx tx = title.addNewTx();
        CTTextBody rich = tx.addNewRich();
        rich.addNewBodyPr(); 
        CTTextParagraph para = rich.addNewP();
        CTRegularTextRun r = para.addNewR();
        r.setT("TITLE");
            }


        if (chart instanceof XSSFChart) { 
        XSSFChart xssfChart = (XSSFChart)chart;

       //Plot area background and border line
       if (xssfChart.getCTChartSpace().getSpPr() == null) xssfChart.getCTChartSpace().addNewSpPr();
       if (xssfChart.getCTChartSpace().getSpPr().getSolidFill() == null) 
        xssfChart.getCTChartSpace().getSpPr().addNewSolidFill();
       if (xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr() == null)
        xssfChart.getCTChartSpace().getSpPr().getSolidFill().addNewSrgbClr();
       xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)255});
       if (xssfChart.getCTChartSpace().getSpPr().getLn() == null) xssfChart.getCTChartSpace().getSpPr().addNewLn();
       xssfChart.getCTChartSpace().getSpPr().getLn().setW(Units.pixelToEMU(0));
       if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill() == null)
        xssfChart.getCTChartSpace().getSpPr().getLn().addNewSolidFill();
       if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr() == null)
        xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().addNewSrgbClr();
       xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)0});

    CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
           plotArea.getLineChartArray()[0].getSmooth();
           CTBoolean ctBool = CTBoolean.Factory.newInstance();
           ctBool.setVal(false);
           plotArea.getLineChartArray()[0].setSmooth(ctBool);
           for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
               ser.setSmooth(ctBool);
           }

0 个答案:

没有答案