【问题标题】:How to set axis' labels in a Line Chart using Apache Poi如何使用 Apache Poi 在折线图中设置轴标签
【发布时间】:2015-08-14 13:00:51
【问题描述】:

我正在尝试使用 java 和 Apache POI 自动创建报告。我快到了,但找不到如何在 XSSFChart 中设置轴标签。

我已经找到了如何设置图表的标题( Apache POI set Excel chart title)。也许有类似的方法可以解决它,但我不是开发人员,不知道如何开始。

谁能帮忙?

到目前为止我的代码:

public void grafico(String nomeplanilhadados, String nomeplanilhagrafico, Date datainicial, Date datafinal, String[] nomesmarcos, String titulo){

    if (datainicial.after(datafinal)){
        throw new IllegalArgumentException("A data inicial precisa anteceder a data final");
    }
    Sheet dados = this.wb.getSheet(nomeplanilhadados);
    Sheet planilhagrafico = this.wb.getSheet(nomeplanilhagrafico);
    Drawing drawing = planilhagrafico.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 4, 17, 20);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);
    LineChartData data = chart.getChartDataFactory().createLineChartData();

    // Use a category axis for the bottom axis.
    ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
    bottomAxis.setNumberFormat("MMM/yyyy");

    ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
    leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

    //retrieve the data
    int linhainicial=-1;
    int linhafinal=-1;
    Iterator<Row> rowIterator = dados.iterator();
    while(rowIterator.hasNext()){
        Row row = rowIterator.next();
        Cell cell = row.getCell(0);
        if(cell!=null){
            SimpleDateFormat formatodata = new SimpleDateFormat("dd-MM-yyyy");
            Date date=cell.getDateCellValue();

            if (linhainicial==-1 && date.compareTo(datainicial)>=0){
                linhainicial=cell.getRowIndex();
            }
            if( date.compareTo(datafinal)<=0){
                linhafinal=cell.getRowIndex();
            }
        }
    }

    ChartDataSource<Number> xs = DataSources.fromNumericCellRange(dados, new CellRangeAddress(linhainicial, linhafinal, 0, 0));
    Row primeiralinha = dados.getRow(0);
    Iterator<Cell> cellIterator = primeiralinha.iterator();
    while(cellIterator.hasNext()){
        Cell cell=cellIterator.next();
        if(cell!=null && Arrays.asList(nomesmarcos).contains(cell.getStringCellValue())){
            ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(dados, new CellRangeAddress(linhainicial, linhafinal, cell.getColumnIndex(), cell.getColumnIndex()));
            //data.addSerie(xs, ys1);
            LineChartSerie chartSerie = data.addSerie(xs, ys1);
            chartSerie.setTitle(cell.getStringCellValue());
        }
    }

    XSSFChart xchart = (XSSFChart) chart;
    CTChart ctChart = xchart.getCTChart();
    CTTitle title = ctChart.addNewTitle();
    CTTx tx = title.addNewTx();
    CTTextBody rich = tx.addNewRich();
    rich.addNewBodyPr();  // body properties must exist, but can be empty
    CTTextParagraph para = rich.addNewP();
    CTRegularTextRun r = para.addNewR();
    r.setT(titulo);




    chart.plot(data, bottomAxis, leftAxis);    

}

【问题讨论】:

    标签: java excel charts apache-poi


    【解决方案1】:

    尝试使用此方法。

    public static void setAxisTitle(XSSFChart chart, int axisIdx, String title) {
        CTValAx valAx = chart.getCTChart().getPlotArea().getValAxArray(axisIdx);
        CTTitle ctTitle = valAx.addNewTitle();
        ctTitle.addNewLayout();
        ctTitle.addNewOverlay().setVal(false);
        CTTextBody rich = ctTitle.addNewTx().addNewRich();
        rich.addNewBodyPr();
        rich.addNewLstStyle();
        CTTextParagraph p = rich.addNewP();
        p.addNewPPr().addNewDefRPr();
        p.addNewR().setT(title);
        p.addNewEndParaRPr();
    }
    

    【讨论】:

      【解决方案2】:

      我不确定你是否得到了答案。我不确定你的问题是什么。 我猜您想设置轴标签,以便您的 x 轴从最低/最高点开始。你可以用这个

      bottomAxis.setCrosses(AxisCrosses.MIN)
      

      bottomAxis.setCrosses(AxisCrosses.MAX)
      

      你已经在你的代码中使用了这个:

      bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO)
      

      【讨论】:

        【解决方案3】:

        Александр Кушнир 的回答是添加轴标题的好方法,但他给出的方法需要修改。

        //设置类别轴的标题

        public static void setCatAxisTitle(XSSFChart chart, int axisIdx, String title) {
            CTCatAx valAx = chart.getCTChart().getPlotArea().getCatAxArray(axisIdx);
            CTTitle ctTitle = valAx.addNewTitle();
            ctTitle.addNewLayout();
            ctTitle.addNewOverlay().setVal(false);
            CTTextBody rich = ctTitle.addNewTx().addNewRich();
            rich.addNewBodyPr();
            rich.addNewLstStyle();
            CTTextParagraph p = rich.addNewP();
            p.addNewPPr().addNewDefRPr();
            p.addNewR().setT(title);
            p.addNewEndParaRPr();
        }
        
        
        public static void setValueAxisTitle(XSSFChart chart, int axisIdx, String title) {
            CTValAx valAx = chart.getCTChart().getPlotArea().getValAxArray(axisIdx);
            CTTitle ctTitle = valAx.addNewTitle();
            ctTitle.addNewLayout();
            ctTitle.addNewOverlay().setVal(false);
            CTTextBody rich = ctTitle.addNewTx().addNewRich();
            rich.addNewBodyPr();
            rich.addNewLstStyle();
            CTTextParagraph p = rich.addNewP();
            p.addNewPPr().addNewDefRPr();
            p.addNewR().setT(title);
            p.addNewEndParaRPr();
        }
        

        // 使用类别轴作为底轴。

         ChartAxis bottomAxis =chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
         ValueAxis leftAxis =chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        

        //但是我没有找到获取axisIdx的好方法。如果绘图区只有一个图表,可以这样设置轴标题。

        setValueAxisTitle(xchart,0,"title of bottom axis");
         setCatAxisTitle(xchart,0, "title of left axis");
        

        【讨论】:

          【解决方案4】:

          C# 示例

          //设置值轴 private static void setValueAxisTitle(XSSFChart chart, int axisIdx, String title) { NPOI.OpenXmlFormats.Dml.Chart.CT_ValAx valAx = chart.GetCTChart().plotArea.valAx[axisIdx]; valAx.title = new NPOI.OpenXmlFormats.Dml.Chart.CT_Title(); NPOI.OpenXmlFormats.Dml.Chart.CT_Title ctTitle = valAx.title; ctTitle.layout = new NPOI.OpenXmlFormats.Dml.Chart.CT_Layout(); ctTitle.overlay = new NPOI.OpenXmlFormats.Dml.Chart.CT_Boolean(); ctTitle.overlay.val = 1; ctTitle.AddNewTx(); NPOI.OpenXmlFormats.Dml.Chart.CT_TextBody rich = ctTitle.tx.AddNewRich(); 丰富的.AddNewBodyPr(); 丰富的.AddNewLstStyle(); 丰富的.AddNewP(); NPOI.OpenXmlFormats.Dml.CT_TextParagraph p = rich.p[0]; p.AddNewPPr(); p.pPr.defRPr = new NPOI.OpenXmlFormats.Dml.CT_TextCharacterProperties(); p.AddNewR().t=标题; p.AddNewEndParaRPr(); } //设置猫轴 私有静态无效 setCatAxisTitle(XSSFChart 图表,intaxisIdx,字符串标题) { chart.GetCTChart().plotArea.catAx[axisIdx].title = new NPOI.OpenXmlFormats.Dml.Chart.CT_Title(); NPOI.OpenXmlFormats.Dml.Chart.CT_Title ctTitle = chart.GetCTChart().plotArea.catAx[axisIdx].title;// new NPOI.OpenXmlFormats.Dml.Chart.CT_Title(); ctTitle.layout = new NPOI.OpenXmlFormats.Dml.Chart.CT_Layout(); ctTitle.layout.AddNewManualLayout(); NPOI.OpenXmlFormats.Dml.Chart.CT_Boolean ctbool = new NPOI.OpenXmlFormats.Dml.Chart.CT_Boolean(); ctbool.val = 1; ctTitle.overlay = ctbool; ctTitle.AddNewTx(); NPOI.OpenXmlFormats.Dml.Chart.CT_TextBody rich = ctTitle.tx.AddNewRich(); 丰富的.AddNewBodyPr(); 丰富的.AddNewLstStyle(); 丰富的.AddNewP(); NPOI.OpenXmlFormats.Dml.CT_TextParagraph p = rich.p[0]; p.AddNewPPr(); p.AddNewR().t = 标题; p.AddNewEndParaRPr(); }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多