代码之家  ›  专栏  ›  技术社区  ›  amachado

使用OpenXML格式在条形图中创建目标标记

  •  0
  • amachado  · 技术社区  · 6 年前

    使用apache poi和openxmlformats,我用条形图构建了一个Excel。我制作条形图的方法是:

    private static ctchart createbarchart(图表,字符串数据表,arraylist<integer>cols,int lastrow,布尔序列编码){
    ctchart ctchart=(xssfchart)chart).gettchart();
    ctplotara ctplotara=ctchart.getplotara();
    ctbarchart ctbarchart=ctplotara.addNewbarchart();
    ctBoolean ctBoolean=ctBarChart.addNewVaryColors();
    ctBoolean.setval(真);
    ctbarchart.addNewbardir().setval(cols.size()>1)是什么?stbardir.col:stbardir.bar);
    
    int idx=0;
    
    对于(整数c:cols){
    ctbarser ctbarser=ctbarchart.addnewser();
    ctsertx ctsertx=ctbarser.addnewtx();
    ctstrRef ctstrRef=ctsertx.addNewstrRef();
    ctstrRef.setf(new cellReference(datasheet,0,c,true,true).formatasstring());
    
    ctbarser.addnewidx().setval(idx++);
    ctaxDatasource cttaxDataSource=ctBarser.addNewcat();
    ctstrRef=ctTaxDataSource.addNewsTrRef();
    
    ctstrRef.setf(新区域引用(新单元格引用(数据表,1,0,真,真),新单元格引用(数据表,最后一行,0,真,真),电子表格版本.Excel2007)
    .formatasstring());
    
    ctNumDataSource ctNumDataSource=ctBarser.addNewVal();
    ctNumRef ctNumRef=ctNumDataSource.addNewNumRef();
    
    ctnumref.setf(新区域引用(新单元格引用(datasheet,1,c,true,true),新单元格引用(datasheet,lastrow,c,true,true),spreadsheetversion.excel2007)
    .formatasstring());
    
    //至少是libreoffice calc中的边框线;-)
    ctbarser.addnewspr().addnewln().addnewsolidfill().addnewsrgbclr().setval(新字节[]0,0,0);
    
    }
    
    //告诉条形图它有轴,并给出它们的ID
    ctbarchart.addnewaxid().setval(123456);
    ctbarchart.addnewaxid().setval(123457);
    
    //CAT轴
    ctCatax ctCatax=ctPlotara.addNewCatax();
    ctcatax.addnewaxid().setval(123456);//cat轴的ID
    ctscaling ctscaling=ctcatax.addNewscaling();
    ctscaling.addNewOrientation().setval(storientation.min_max);
    ctcatax.addnewdelete().setval(false);
    ctcatax.addnewaxpos().setval(statxpos.b);
    ctcatax.addnewcrossax().setval(123457);//val轴的ID
    ctcatax.addNewTicklBos().setval(stTicklBos.next_to);
    
    //VAL轴
    ctValax ctValax=ctPlotara.addNewValax();
    ctvalax.addnewaxid().setval(123457);//val轴的ID
    ctscaling=ctvalax.addNewscaling();
    ctscaling.addNewOrientation().setval(storientation.min_max);
    ctvalax.addnewdelete().setval(假);
    ctvalax.addnewaxpos().setval(statxpos.l);
    ctvalax.addnewcrossax().setval(123456);//CAT轴的ID
    ctvalax.addNewTicklBos().setval(stTicklBos.next_to);
    
    //图例
    如果(cols.size()>1){
    ctlegend ctlegend=ctchart.addNewLegend();
    ctlegend.addnewlegendpos().setval(stlegendpos.l);
    ctlegend.addnewoverlay().setval(假);
    }
    返回CTChart;
    
    }
    

    这个方法就是我生成图表的方法。 结果是

    但现在我需要在这张图上画一条目标线

    但我不知道如何,也没有找到任何有关的信息。

    谢谢你

    这个方法就是我生成图表的方法。 结果是 enter image description here

    但现在我需要在这张图上画一条目标线

    enter image description here

    但我不知道怎么做,也没有找到任何有关的信息。

    谢谢你

    1 回复  |  直到 6 年前
        1
  •  2
  •   Axel Richter    6 年前
    ctbarchart.addnewaxid().setval(123457); //CAT轴 ctCatax ctCatax=ctPlotara.addNewCatax(); ctcatax.addnewaxid().setval(123456);//cat轴的ID ctscaling ctscaling=ctcatax.addNewscaling(); ctscaling.addNewOrientation().setval(storientation.min_max); ctcatax.addnewdelete().setval(false); ctcatax.addnewaxpos().setval(statxpos.b); ctcatax.addnewcrossax().setval(123457);//val轴的ID ctcatax.addNewTicklBos().setval(stTicklBos.next_to); //VAL轴

    enter image description here

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.*;
    import org.apache.poi.xssf.usermodel.XSSFChart;
    import org.apache.poi.ss.SpreadsheetVersion;
    
    import org.openxmlformats.schemas.drawingml.x2006.chart.*;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import java.util.ArrayList;
    
    class ExcelCharWithTargetLine {
    
     private static CTChart createBarChart(XSSFChart chart, String dataSheet, ArrayList<Integer> cols, int lastRow, double target) {
    
      CTChart ctChart = chart.getCTChart();
      CTPlotArea ctPlotArea = ctChart.getPlotArea();
      CTBarChart ctBarChart = ctPlotArea.addNewBarChart();
      CTBoolean ctBoolean = ctBarChart.addNewVaryColors();
      ctBoolean.setVal(true);
      ctBarChart.addNewBarDir().setVal(cols.size() > 1 ? STBarDir.COL : STBarDir.BAR);
    
      int idx = 0;
    
      for (Integer c : cols) {
       CTBarSer ctBarSer = ctBarChart.addNewSer();
       CTSerTx ctSerTx = ctBarSer.addNewTx();
       CTStrRef ctStrRef = ctSerTx.addNewStrRef();
       ctStrRef.setF(new CellReference(dataSheet, 0, c, true, true).formatAsString());
    
       ctBarSer.addNewIdx().setVal(idx++);
       CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();
       ctStrRef = cttAxDataSource.addNewStrRef();
    
       ctStrRef.setF(new AreaReference(
                      new CellReference(dataSheet, 1, 0, true, true), 
                      new CellReference(dataSheet, lastRow, 0, true, true), 
                      SpreadsheetVersion.EXCEL2007).formatAsString()
                     );
    
       CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();
       CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();
    
       ctNumRef.setF(new AreaReference(
                      new CellReference(dataSheet, 1, c, true, true), 
                      new CellReference(dataSheet, lastRow, c, true, true), 
                      SpreadsheetVersion.EXCEL2007).formatAsString()
                     );
    
       // at least the border lines in Libreoffice Calc ;-)
       //ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { 0, 0, 0 });
    
      }
    
      // 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);
    
    
      // target line
      // line of a scatter chart from 0 (min) to 1 (max) having value of double target
      CTScatterChart ctScatterChart = ctPlotArea.addNewScatterChart();
      ctBoolean = ctScatterChart.addNewVaryColors();
      ctBoolean.setVal(true);
    
      CTScatterSer ctScatterSer = ctScatterChart.addNewSer();
      ctScatterSer.addNewIdx().setVal(idx++);
      ctScatterSer.addNewMarker().addNewSymbol().setVal(STMarkerStyle.NONE);
    
      CTNumData ctNumData = ctScatterSer.addNewXVal().addNewNumLit();
      ctNumData.addNewPtCount().setVal(2);
      CTNumVal ctNumVal = ctNumData.addNewPt();
      ctNumVal.setIdx(0);
    
      if (cols.size() > 1) {
       ctNumVal.setV("0"); // if STBarDir.COL then X axis is from 0 to 1
      } else {
       ctNumVal.setV("" + target); // if STBarDir.BAR then X axis is value axis
      }
    
      ctNumVal = ctNumData.addNewPt();
      ctNumVal.setIdx(1);
    
      if (cols.size() > 1) {
       ctNumVal.setV("1");
      } else {
       ctNumVal.setV("" + target);
      }
    
      ctNumData = ctScatterSer.addNewYVal().addNewNumLit();
      ctNumData.addNewPtCount().setVal(2);
      ctNumVal = ctNumData.addNewPt();
      ctNumVal.setIdx(0);
    
      if (cols.size() > 1) {
       ctNumVal.setV("" + target); // if STBarDir.COL then Y axis is value axis
      } else {
       ctNumVal.setV("0"); // if STBarDir.BAR then Y axis is from 0 to 1
      }
    
      ctNumVal = ctNumData.addNewPt();
      ctNumVal.setIdx(1);
    
      if (cols.size() > 1) {
       ctNumVal.setV("" + target);
      } else {
       ctNumVal.setV("1");
      }
    
      ctScatterChart.addNewAxId().setVal(123458);
      ctScatterChart.addNewAxId().setVal(123459);
    
      ctValAx = ctPlotArea.addNewValAx();
      ctValAx.addNewAxId().setVal(123458); // id of the X axis
      ctScaling = ctValAx.addNewScaling();
      ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);
      if (cols.size() > 1) {
       ctScaling.addNewMax().setVal(1.0);
      }
      ctValAx.addNewDelete().setVal(true);
      ctValAx.addNewAxPos().setVal(STAxPos.T);
      ctValAx.addNewCrossAx().setVal(123459); // id of the Y axis
      ctValAx.addNewTickLblPos().setVal(STTickLblPos.NONE);
    
      ctValAx = ctPlotArea.addNewValAx();
      ctValAx.addNewAxId().setVal(123459); // id of the Y axis
      ctScaling = ctValAx.addNewScaling();
      ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);
      if (cols.size() == 1) {
       ctScaling.addNewMax().setVal(1.0);
      }
      ctValAx.addNewDelete().setVal(true);
      ctValAx.addNewAxPos().setVal(STAxPos.R);
      ctValAx.addNewCrossAx().setVal(123458); // id of the X axis
      ctValAx.addNewTickLblPos().setVal(STTickLblPos.NONE);
    
    
      // legend
      if (cols.size() > 1) {
       CTLegend ctLegend = ctChart.addNewLegend();
       ctLegend.addNewLegendPos().setVal(STLegendPos.L);
    
       // delete additional target line series legent entry
       CTLegendEntry ctLegendEntry = ctLegend.addNewLegendEntry();
       ctLegendEntry.addNewIdx().setVal(0);
       ctLegendEntry.addNewDelete().setVal(true);
    
       ctLegend.addNewOverlay().setVal(false);
      }
    
      return ctChart;
    
     }
    
     public static void main(String[] args) throws Exception {
    
      Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelChart.xlsx"));
    
      Sheet sheet = workbook.getSheetAt(0);
    
      ArrayList<Integer> cols = new ArrayList<Integer>();
      //cols.add(1);
      cols.add(2);
      //cols.add(3);
    
      Drawing drawing = sheet.createDrawingPatriarch();
      ClientAnchor anchor = null;
      if (cols.size() > 1) {
       anchor = drawing.createAnchor(0, 0, 0, 0, 0, 8, 10, 23);
      } else {
       anchor = drawing.createAnchor(0, 0, 0, 0, 0, 8, 5, 23);
      }
      Chart chart = drawing.createChart(anchor);
      CTChart ctChart = ctChart = createBarChart((XSSFChart) chart, sheet.getSheetName(), cols, 6, 42.0);
    
      workbook.write(new FileOutputStream("ExcelChartNew.xlsx"));
      workbook.close();
    
     }
    }
    

    enter image description here

    ooxml-schemas-1.3.jar FAQ-N10025