axis idea 设置apache,如何使用Apache Poi在折线图中设置轴的标签

本文介绍如何在Java中使用Apache POI库创建和自动化报告,特别是聚焦于设置XSSF图表的轴标签。作者已经成功设置了图表标题,但需要帮助来设定轴的标签。提供的代码示例展示了创建和配置图表、数据源、轴以及系列的基本步骤,并提供了设置轴标题的方法。
摘要由CSDN通过智能技术生成

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.

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.

Can anyone help?

My code up to now:

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 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 xs = DataSources.fromNumericCellRange(dados, new CellRangeAddress(linhainicial, linhafinal, 0, 0));

Row primeiralinha = dados.getRow(0);

Iterator cellIterator = primeiralinha.iterator();

while(cellIterator.hasNext()){

Cell cell=cellIterator.next();

if(cell!=null && Arrays.asList(nomesmarcos).contains(cell.getStringCellValue())){

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

}

解决方案

try use this method.

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值