apache poi:如何创建带有条形图和折线图的图表?

2024-04-03

是否可以在 apache poi 中创建一个包含条形图和折线图的图表?你可以找个例子here https://blogs.office.com/en-us/2012/06/21/combining-chart-types-adding-a-second-axis/.

如果是,您能否提供示例代码来实现这一目标?

期待您的回音。提前致谢。


这是我找到的解决方案。 不幸的是,我无法更改折线图的轴。但是,您可以轻松地在文件上手动更改此设置。打开它时,用右键单击线系列顶部并选择“格式化数据系列”,您可以先将其更改为主轴,然后再更改为次轴,这将得到图表完美!我真的不知道如何在代码上解决这个问题,如果您发现请与我分享!

您将需要 jar ooxml-schemas-1.3.jar。 其完整版有 15MB。https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

apache poi:如何创建带有条形图和折线图的图表? 的相关文章

随机推荐