apache poi:如何使用条形图和折线图创建图表? [英] apache poi: how to create chart with both bar and line?

查看:65
本文介绍了apache poi:如何使用条形图和折线图创建图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 apache poi 中创建包含条形和线条的图表?你可以找到一个例子 这里.

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.

推荐答案

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

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!

您将需要 jar ooxml-schemas-1.3.jar.它的完整版有 15MB.https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3

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

希望能帮到你!

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

这篇关于apache poi:如何使用条形图和折线图创建图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆