java poi读取图表,Java利用Apache poi导出图表

jar

compile('org.apache.poi:poi:4.0.1')

compile('org.apache.poi:poi-scratchpad:4.0.1')

compile('org.apache.poi:poi-ooxml:4.0.1')

compile('org.apache.poi:ooxml-schemas:1.4')

public static class Inbound_chartExport{

public XSSFSheet sheet;//操作的sheet

public String title;//头部标题

public String seriesTitle;//系列标题

public String xName;//x轴标题

public String yName;

public int startRow;//开始行数

public int size;//结束行数

public int xStartCol;//x轴列数

public int yStartCol;

public int col1;//图标放置位置

public int row1;

public int col2;

public int row2;

}

public void excelBarChart(Inbounds.Inbound_chartExport inbound){

XSSFDrawing drawing = inbound.sheet.createDrawingPatriarch();

XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, inbound.col1, inbound.row1, inbound.col2, inbound.row2);

XSSFChart chart = drawing.createChart(anchor);//图表

chart.setTitleText(inbound.title);

chart.setTitleOverlay(false);

XDDFChartLegend legend = chart.getOrAddLegend();//图例项

legend.setPosition(LegendPosition.TOP_RIGHT);

// Use a category axis for the bottom axis.

XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);//x轴和位置

bottomAxis.setTitle(inbound.xName);

XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);//y轴和位置

leftAxis.setTitle(inbound.yName);

leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);//生成Data

XDDFDataSource xs = XDDFDataSourcesFactory.fromStringCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.xStartCol, inbound.xStartCol));//x轴range区域

XDDFNumericalDataSource ys1 = XDDFDataSourcesFactory.fromNumericCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.yStartCol, inbound.yStartCol));//y轴range区域

XDDFChartData.Series series1 = data.addSeries(xs, ys1);//生成Series 系列/维度

series1.setTitle(inbound.seriesTitle, null);

chart.plot(data);

XDDFBarChartData bar = (XDDFBarChartData) data;//由数据生成图表,Bar柱状图,Pie饼状图,Line折线图,Scatter散点图

bar.setBarDirection(BarDirection.BAR);//柱状如方向 BAR 竖向 COL横向

CTChart ctChart = chart.getCTChart();

CTPlotArea ctPlotArea = ctChart.getPlotArea();

CTBarChart ctBarChart = ctPlotArea.getBarChartArray(0);

CTBarSer ctBarSer = ctBarChart.getSerArray(0);

CTDLbls newDLbls = ctBarSer.addNewDLbls();//数据标签

CTBoolean ctBoolean = ctBarChart.addNewVaryColors();//多样颜色,true 选用category作为图例项,false选用系列作为图例项,最好false

ctBoolean.setVal(false);

newDLbls.setShowCatName(ctBoolean);//数据标签 显示类别名称

newDLbls.setShowSerName(ctBoolean);//数据标签 显示序列名称

newDLbls.setShowPercent(ctBoolean);

newDLbls.setShowBubbleSize(ctBoolean);

newDLbls.setShowLeaderLines(ctBoolean);

newDLbls.setShowLegendKey(ctBoolean);//图例化标签

newDLbls.addNewShowVal().setVal(true);//数据标签 显示值

solidFillSeries(data, 0, PresetColor.CHARTREUSE);//颜色PresetColor

}

private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {

XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));

XDDFChartData.Series series = data.getSeries().get(index);

XDDFShapeProperties properties = series.getShapeProperties();

if (properties == null) {

properties = new XDDFShapeProperties();

}

properties.setFillProperties(fill);

series.setShapeProperties(properties);

}

标签:Java,inbound,chart,apache,int,poi,Apache,public

来源: https://www.cnblogs.com/Jack-0824/p/11436846.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值