简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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