如何使用 Apache Poi 在折线图中设置轴标签 [英] How to set axis' labels in a Line Chart using Apache Poi

查看:56
本文介绍了如何使用 Apache Poi 在折线图中设置轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to automate a report creation using java and Apache POI. I'm almost there, but can't find how to set the axis' labels in a XSSFChart.

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

I already found how to to set the chart's title( Apache POI set Excel chart title ). Maybe there is a similar way to work it around, but I'm no developer and have no idea on how to start.

有人可以帮忙吗?

到目前为止我的代码:

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

}

推荐答案

尝试使用此方法.

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

这篇关于如何使用 Apache Poi 在折线图中设置轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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