apache poi: how to create chart with both bar and line?

给你一囗甜甜゛ 提交于 2019-12-10 21:49:59

问题


Is it possible to create a chart in apache poi with both bar and line in it? You can find an example here.

If yes, can you please provide sample code to achieve this?

Looking forward to hearing from you. Thanks in advance.


回答1:


This is the solution I found. I unfortunately couldn't change the axis for the line chart. However you can easily change this manually on the file.When you open it, click on top of the line series with the right button and select "Format Data Series" you can change it first to primary axis and then secondary axis, this will get the chart perfect! I don't really know how to fix this on the Code, if you find out please share it with me!

You are going to need the jar ooxml-schemas-1.3.jar. Its the full that has 15MB. https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3

I hope it helps!

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


来源:https://stackoverflow.com/questions/45336034/apache-poi-how-to-create-chart-with-both-bar-and-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!