2017-07-26 49 views
1

是否有可能在apache poi中同時在bar和line中創建一個圖表?你可以找到一個例子hereapache poi:如何用條和線創建圖表?

如果是,請提供示例代碼來實現此目的?

期待您的迴音。提前致謝。

+0

這可能與我在此提供的所有其他圖表示例相同。在Excel中創建圖表的最簡單版本。保存爲'* .xlsx'。解壓縮* .xlsx'。查看'/ xl/charts/chart * .xml'中的'XML'。嘗試使用'apache poi'使用低級別的'CT *'類來創建它。 –

回答

0

這是我找到的解決方案。 我不幸無法更改折線圖的軸。但是,您可以輕鬆地在文件上手動更改此項。當您打開它時,用右鍵單擊該系列的頂部,然後選擇「格式化數據系列」,您可以先將它更改爲主軸,然後再更改次軸,圖表完美!我真的不知道如何解決這個問題,如果你發現請與我分享!

您將需要jar ooxml-schemas-1.3.jar。 它的全部有15MB。 https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3

我希望它有幫助!

XSSFWorkbook workbook=new XSSFWorkbook(); 
    XSSFSheet chartdisplay=workbook.createSheet("ChartDisplay") 
    XSSFDrawing drawing=chartdisplay.createDrawingPatriarch(); 
    ClientAnchor anchor=drawing.createAnchor(0,0,0,0,5,5,13,13); 
    Chart chart=drawing.createChart(anchor); 

    CTChart ctChart=((XSSFChart)chart).getCTChart(); 
    CTPlotArea ctPlotArea=ctChart.getPlotArea(); 
    //Bar Chart 
    CTBarChart ctBarChart=ctPlotArea.addNewBarChart(); 
    CTBoolean ctBoolean=ctBarChart.addNewVaryColors(); 
    ctBoolean.setVal(false); 
    ctBarChart.addNewBarDir().setVal(STBarDir.COL); 
    CTBarSer ctBarSer=ctBarChart.addNewSer(); 
    CTSerTx ctSerTx=ctBarSer.addNewTx(); 
    CTStrRef ctStrRef=ctSerTx.addNewStrRef(); 
    ctStrRef.setF("\"BarSeriesName\""); 
    //Labels For Bar Chart 

    ctBarSer.addNewIdx().setVal(0); //0 = Color Blue 
    CTAxDataSource ctAxDataSource=ctBarSer.addNewCat(); 
    ctStrRef=ctAxDataSource.addNewStrRef(); 
    String labelsRefer="ChartDisplay!B2:B7";//Excel Range where the Labels Are 
    ctStrRef.setF(labelsRefer); 
    //Values For Bar Chart 
    CTNumDataSource ctNumDataSource=ctBarSer.addNewVal(); 
    CTNumRef ctNumRef=ctNumDataSource.addNewNumRef(); 
    String valuesRefer="ChartDisplay!C2:C7";//Excel range where values for barChart are 
    ctNumRef.setF(valuesRefer); 
    ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{0,0,0}); 
    // Axis 
    ctBarChart.addNewAxId().setVal(123456); 
    ctBarChart.addNewAxId().setVal(123457); 
    //cat axis 
    CTCatAx ctCatAx=ctPlotArea.addNewCatAx(); 
    ctCatAx.addNewAxId().setVal(123456); //id of the cat axis 
    CTScaling ctScaling=ctCatAx.addNewScaling(); 
    ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX); 
    ctCatAx.addNewDelete().setVal(false); 
    ctCatAx.addNewAxPos().setVal(STAxPos.L); 
    ctCatAx.addNewCrossAx().setVal(123457); //id of the val axis 
    ctCatAx.addNewMinorTickMark().setVal(STTickMark.NONE); 
    ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO); 

    //val Left Axis 
    CTValAx ctValAx1=ctPlotArea.addNewValAx(); 
    ctValAx1.addNewAxId().setVal(123457); //id of the val axis 
    ctScaling=ctValAx1.addNewScaling(); 
    ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX); 
    ctValAx1.addNewDelete().setVal(false); 
    ctValAx1.addNewAxPos().setVal(STAxPos.L); 
    ctValAx1.addNewCrossAx().setVal(123456); //id of the cat axis 
    ctValAx1.addNewMinorTickMark().setVal(STTickMark.NONE); 
    ctValAx1.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO); 
    ctValAx1.addNewMajorGridlines(); 

    // =======Line Chart 
    //val Right Axis 
    CTLineChart ctLineChart=ctPlotArea.addNewLineChart(); 
    CTBoolean ctBooleanLine=ctLineChart.addNewVaryColors(); 
    ctBooleanLine.setVal(false); 
    CTLineSer ctLineSer=ctLineChart.addNewSer(); 
    CTSerTx ctSerTx1=ctLineSer.addNewTx(); 
    CTStrRef ctStrRef1=ctSerTx1.addNewStrRef(); 
    ctStrRef1.setF("\"LineSeriesName\""); 
    ctLineSer.addNewIdx().setVal(2); //2= Color Grey 
    CTAxDataSource ctAxDataSource1=ctLineSer.addNewCat(); 
    ctStrRef1=ctAxDataSource1.addNewStrRef(); 
    ctStrRef1.setF(labelsRefer); 
    ctLineSer.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{0,0,0}); 

    String values2Refer="ChartDisplay!D2:D7"; //Excel Range Where Values for Line Values are 
    CTNumDataSource ctNumDataSource1=ctLineSer.addNewVal(); 
    CTNumRef ctNumRef1=ctNumDataSource1.addNewNumRef(); 
    ctNumRef1.setF(values2Refer); 

    //Axis 
    ctLineChart.addNewAxId().setVal(1234);//id of the cat axis 
    ctLineChart.addNewAxId().setVal(12345); 

    CTCatAx ctCatAx1=ctPlotArea.addNewCatAx(); 
    ctCatAx1.addNewAxId().setVal(1234);// id of the cat Axis 
    ctScaling=ctCatAx1.addNewScaling(); 
    ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX); 
    ctCatAx1.addNewDelete().setVal(true); 
    ctCatAx1.addNewAxPos().setVal(STAxPos.R); 
    ctCatAx1.addNewCrossAx().setVal(12345); //id of the val axis 
    CTBoolean ctBoolean1=ctCatAx1.addNewAuto(); 


    CTValAx ctValAx=ctPlotArea.addNewValAx(); 
    ctValAx.addNewAxId().setVal(12345); //id of the val axis 
    ctScaling=ctValAx.addNewScaling(); 
    ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX); 
    ctValAx.addNewDelete().setVal(false); 
    ctValAx.addNewAxPos().setVal(STAxPos.R); 
    ctValAx.addNewCrossAx().setVal(1234); //id of the cat axis 
    ctValAx.addNewMinorTickMark().setVal(STTickMark.NONE); 
    ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO); 

    //Legend 
    CTLegend ctLegend=ctChart.addNewLegend(); 
    ctLegend.addNewLegendPos().setVal(STLegendPos.B); 
    ctLegend.addNewOverlay().setVal(false);