【POI】POI+JFREECHART实现图表报表

大部分的实现细节,均参考“生若蜉蝣_涂宗勋”的博客使用poi和jfreechart生成excel图表图片

老哥写的很清楚,重复流程不再赘述。

实现代码:

1 整理数据

// 整理GMV具体数据
DefaultCategoryDataset dataset = createDataset(data);

2 设计图表

// 将数据加入图表中
JFreeChart chart = getGmvChart(dataset,ShopNick);

3 获取图表对象

try {
                            ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
String fileSavePath = "exTest.png";
BufferedImage bufferImg = ImageIO.read(new File(fileSavePath));
ImageIO.write(bufferImg, "png", byteArrayOut);
} catch (IOException e) {

}

4 将图表对象写入到POI对象中

// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

//插入图标
HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 255, 255, (short) 0, (short) 8, (short) 5, (short) 11);
anchor2.setAnchorType(3);

if(byteArrayOut.size()>0) {
patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
}

5 将excel保存到本地

//用当前的时间戳当xls文件名
            String fileName = String.valueOf(System.currentTimeMillis());
            String filePath = "D:\\"+fileName+".xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();

其他函数

private DefaultCategoryDataset createDataset(ECQueryBean data) {
        //获取月份列表
        List<String> monthList = getMonthList();
        //将gmv目标和预测&实际加入到listList<Long> planGmvList = new ArrayList<Long>();
        List<Long> gmvList = new ArrayList<Long>();

        for(String str:data.getGmvReal()) {
            Long tmp = Long.valueOf(str)/1000;
            gmvList.add(tmp);
        }

        for(String str:data.getGmvTarget()) {
            Long tmp = Long.valueOf(str)/1000;
            planGmvList.add(tmp);
        }
        // 设置数据区域
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < monthList.size(); i++) {
            String time = monthList.get(i);
            dataset.addValue(i<planGmvList.size()?planGmvList.get(i):Long.valueOf("0"), "予算", time);
            dataset.addValue(i<gmvList.size()?gmvList.get(i):Long.valueOf("0"), "実績/予測", time);
        }
        return dataset;
    }
private List<String> getMonthList() {
        List<String> monthList = new ArrayList<String>();
        monthList.add("1月");
        monthList.add("2月");
        monthList.add("3月");
        monthList.add("4月");
        monthList.add("5月");
        monthList.add("6月");
        monthList.add("7月");
        monthList.add("8月");
        monthList.add("9月");
        monthList.add("10月");
        monthList.add("11月");
        monthList.add("12月");

        return monthList;
    }
private JFreeChart getGmvChart(DefaultCategoryDataset dataset, String shopNick) {

        // 设置图片中的字体和颜色以及字号
        Font titleFont = new Font("宋体", Font.PLAIN, 20);
        Font xfont = new Font("宋体", Font.PLAIN, 14);
        Font labelFont = new Font("宋体", Font.PLAIN, 14);

        JFreeChart chart = ChartFactory.createBarChart(shopNick+" GMV (K RMB)", "", "", dataset, PlotOrientation.VERTICAL, true,true, false);
        // 设置图例字体
        chart.getLegend().setItemFont(xfont);

        // 3:设置抗锯齿,防止字体显示不清楚
        //ChartUtils.setAntiAlias(chart);// 抗锯齿
        //ChartUtils.setBarRenderer(chart.getCategoryPlot(), true);
        // 设置标注无边框
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));

        // 设置标题字体
        chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
        // 图形的绘制结构对象
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setRangeGridlinesVisible(true); 

        BarRenderer customBarRenderer = (BarRenderer) plot.getRenderer();
          //设定柱子上面的颜色
        //customBarRenderer.setSeriesPaint(0, Color.decode("#00cc99")); // 给series1 Bar
        //customBarRenderer.setSeriesPaint(1, Color.decode("#0066cc")); // 给series2 Bar
        customBarRenderer.setSeriesPaint(0, new Color(0, 204, 153));
        customBarRenderer.setSeriesPaint(1, new Color(0, 114, 204));

        customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
        customBarRenderer.setBaseItemLabelsVisible(true);  

        //默认的数字显示在柱子中,通过如下两句可调整数字的显示  
        //注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题  
        customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER));  
        customBarRenderer.setItemLabelAnchorOffset(10D);  


        // 获取显示线条的对象
/*      LineAndShapeRenderer lasp = (LineAndShapeRenderer) plot.getRenderer();
        // 设置拐点是否可见/是否显示拐点
        lasp.setBaseShapesVisible(true);
        // 设置拐点不同用不同的形状
        lasp.setDrawOutlines(true);
        // 设置线条是否被显示填充颜色
        lasp.setUseFillPaint(false);
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        // 设置折线大小以及折线的颜色
        renderer.setSeriesStroke(0, new BasicStroke(1.0F));
        renderer.setSeriesPaint(0, new Color(210, 105, 30));
        renderer.setSeriesStroke(1, new BasicStroke(1.0F));
        renderer.setSeriesPaint(1, new Color(0, 191, 255));
        // 设置折点的大小
        lasp.setSeriesOutlineStroke(0, new BasicStroke(0.025F));
        lasp.setSeriesOutlineStroke(1, new BasicStroke(0.05F));
        // 设置网格线
        plot.setDomainGridlinePaint(Color.gray);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.gray);
        plot.setRangeGridlinesVisible(true);*/
        // x轴
        CategoryAxis domainAxis = plot.getDomainAxis();
        // 设置x轴不显示,即让x轴和数据区重合
        domainAxis.setAxisLineVisible(false);
        // x轴标题
        domainAxis.setLabelFont(xfont);
        // x轴数据倾斜
        //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.95D));
        // X轴坐标上数值字体
        domainAxis.setTickLabelFont(labelFont);
        // 设置Y轴间隔
        NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
        numAxis.setTickUnit(new NumberTickUnit(50));
        numAxis.setVisible(false);

        // y轴
        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setLabelFont(xfont);
        // 设置y轴不显示,即和数据区重合
        rangeAxis.setAxisLineVisible(false);
        // y轴坐标上数值字体
        rangeAxis.setTickLabelFont(labelFont);
        rangeAxis.setFixedDimension(0);

        CategoryPlot cp = chart.getCategoryPlot();
        // 背景色设置
        cp.setBackgroundPaint(ChartColor.WHITE);
        cp.setRangeGridlinePaint(ChartColor.WHITE);
        // 创建图例,设置图例的位置,这里的设置实际不起作用,怎么设都在下边
        LegendTitle legendTitle = new LegendTitle(chart.getPlot());
        legendTitle.setPosition(RectangleEdge.BOTTOM);
        return chart;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值