简体   繁体   English

如何在Java中使用POI和org.openxmlformats在PPT中为条形图添加数据标签

[英]How to add Data Label for a Bar chart in PPT using POI and org.openxmlformats in Java

I am working to generate a power point presentation. 我正在努力生成一个幻灯片演示文稿。 Each slide have a graph (Bar or Pie). 每个幻灯片都有一个图形(条形或饼形)。 I am using Apache POI Library and org.openxmlformats. 我正在使用Apache POI库和org.openxmlformats。

I am able to plot a Data table for the graph but I am not able to plot a Data label for a graph. 我能够为图形绘制数据表,但无法为图形绘制数据标签。

Actual Output- 实际输出

Please find the generated graph below without data label - 请在下面找到没有数据标签的生成图形- 图1没有数据标签 Expected Output- 预期产出 图2带数据标签

Code - 代码-

CTChartSpace chartSpace = myXSLFChartShape.getMyXSLFChart().getChartSpace();
    CTChart cTChart = chartSpace.addNewChart();
    CTPlotArea cTPlotArea = cTChart.addNewPlotArea();
    CTBarChart cTBarChart = cTPlotArea.addNewBarChart();
    cTBarChart.addNewVaryColors().setVal(true);
    cTBarChart.addNewBarDir().setVal(STBarDir.COL);
    //int c = 0;
    for (int r = 0; r < 2; r++) {
        //c=1;
        CTBarSer cTBarSer = cTBarChart.addNewSer();
        CTStrRef cTStrRef = cTBarSer.addNewTx().addNewStrRef();
        cTStrRef.setF("Label " + r);
        cTStrRef.addNewStrCache().addNewPtCount().setVal(1);
        CTStrVal cTStrVal = cTStrRef.getStrCache().addNewPt();
        cTStrVal.setIdx(0);
        cTStrVal.setV("Val" + r);

        cTBarSer.addNewIdx().setVal(r);
        cTStrRef = cTBarSer.addNewCat().addNewStrRef();
        cTStrRef.setF("Categories");
        cTStrRef.addNewStrCache().addNewPtCount().setVal(1);

        for (int c = 0; c < 2; c++) {
            cTStrVal = cTStrRef.getStrCache().addNewPt();
            cTStrVal.setIdx(c);             
            cTStrVal.setV("Cat" + c);
        }

        CTNumRef cTNumRef = cTBarSer.addNewVal().addNewNumRef();
        cTNumRef.setF("" + 0);
        cTNumRef.addNewNumCache().addNewPtCount().setVal(1);
        for (int c = 0; c < 2; c++) {
            CTNumVal cTNumVal = cTNumRef.getNumCache().addNewPt();
            cTNumVal.setIdx(c);
            cTNumVal.setV("" + (10 * (c + 1)));
        }
            //c++;
    }

    // telling the BarChart that it has axes and giving them Ids
    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.B);
    cTCatAx.addNewCrossAx().setVal(123457); // id of the val axis
    cTCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);

    // val axis
    CTValAx cTValAx = cTPlotArea.addNewValAx();
    cTValAx.addNewAxId().setVal(123457); // id of the val axis
    cTScaling = cTValAx.addNewScaling();
    cTScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);
    cTValAx.addNewDelete().setVal(false);
    cTValAx.addNewAxPos().setVal(STAxPos.L);
    cTValAx.addNewCrossAx().setVal(123456); // id of the cat axis
    cTValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
    cTValAx.addNewDispUnits();
    // legend


    CTLegend cTLegend = cTChart.addNewLegend();
    cTLegend.addNewLegendPos().setVal(STLegendPos.R);

    CTDTable c = cTPlotArea.addNewDTable();
    c.addNewShowKeys();

it would be good if some one can help me to achieve the target. 如果有人可以帮助我实现目标,那将是很好的。 Thanks in advance. 提前致谢。

Adding the data labels for Openxmlformats library can be achieved by CTDLbls class. 通过CTDLbls类可以为Openxmlformats库添加数据标签。 find the below code snippet - 找到以下代码段-

        CTDLbls dLbls = cTBarChart.addNewDLbls();
        dLbls.addNewShowBubbleSize().setVal(false);
        dLbls.addNewShowLegendKey().setVal(false);
        dLbls.addNewShowCatName().setVal(false);
        dLbls.addNewShowSerName().setVal(false);
        dLbls.addNewShowPercent().setVal(false);
        dLbls.addNewShowVal().setVal(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM