ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java利用Apache poi导出图表

2019-08-30 20:00:45  阅读:354  来源: 互联网

标签:Java inbound chart apache int poi Apache public


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<String> xs = XDDFDataSourcesFactory.fromStringCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.xStartCol, inbound.xStartCol));//x轴range区域
        XDDFNumericalDataSource<Double> 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);
    }

Excel导出(Java)资料
柱状图(poi 3.17):https://stackoverflow.com/questions/38913412/create-bar-chart-in-excel-with-apache-poi
饼图(poi 3.17):https://stackoverflow.com/questions/34718734/apache-poi-supports-only-scattercharts-and-linecharts-why
利用导入的模板导出(poi 3.17之前):https://blog.cacoveanu.com/2015/2015.08.27.15.15.charts.apache.poi.html
poi 4.0.1chart(Example):https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/
官方文档:https://poi.apache.org/apidocs/dev/org/apache/poi/xddf/usermodel/chart/package-summary.html
中文事例:https://blog.csdn.net/qq_24365145/article/details/89146549
https://blog.csdn.net/Lonely_boy_/article/details/88870079

标签:Java,inbound,chart,apache,int,poi,Apache,public
来源: https://www.cnblogs.com/Jack-0824/p/11436846.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有