java jfreechart 生成柱状图

1、maven引入

<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
   <groupId>org.jfree</groupId>
   <artifactId>jfreechart</artifactId>
   <version>1.5.5</version>
</dependency>

 2、编写java 执行入口

    public static void main(String[] args) throws Exception  {
        Document document = new Document(PageSize.A4, 25, 25, 30, 55);
        String path = "chartView-" + System.currentTimeMillis() + ".pdf";
        final String dir = System.getProperty("user.dir") + "/output";
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        path = dir + File.separator + path;
        FileOutputStream os = new FileOutputStream(path);
        PdfWriter writer = PdfWriter.getInstance(document, os);
        // 设置页面布局
        writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
        document.open();
        PdfPTable pdfPTable = ElementUtil.addTable(1, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
        com.itextpdf.text.Image image = barChartH("开关触发次数");
        //com.itextpdf.text.Image image = barChart("开关触发次数");
        PdfPCell pdfPCell = ElementUtil.addTableCellImg(image, 1, 1, Element.ALIGN_CENTER, Element.ALIGN_CENTER, new BaseColor(255, 255, 255));
        pdfPTable.addCell(pdfPCell);
        document.add(pdfPTable);
      //  image.scaleToFit(PageSize.A4.getWidth()-60, PageSize.A4.getHeight() - 100);
      //  document.add(image);
        document.close();
    }

3、简单PDF生成这里不简述,下面编写生成柱状图图片方法

    /**
     * 生成柱状图
     */
    public static com.itextpdf.text.Image barChartH(String xAxisTitle) throws Exception {
        //x轴名称列表
        List<String> xAxisNameList = new ArrayList<>(Arrays.asList(
                "开关1",
                "开关2",
                "开关3",
                "开关4",
                "开关5",
                "开关6",
                "开关7",
                "开关8",
                "开关9",
                "开关10"));
        //图例名称列表
        List<String> legendNameList = new ArrayList<>(Arrays.asList("智能保护跳闸次数"));
        //数据列表
        List<List<Object>> dataList = new ArrayList<>();
        dataList.add(new ArrayList<>(Arrays.asList(540, 90, 80, 78,70,50, 45, 0, 0, 0)));

        //图例背景颜色
        List<Color> legendColorList = new ArrayList<>(Arrays.asList(new Color(232, 83, 6)));
        JFreeChart chart = GenerateChartUtil.createBarChartH("智能开关断路保护", legendNameList, xAxisNameList
                , dataList, JFreeChartUtil.createChartTheme("宋体"), "跳闸/次", xAxisTitle,
                0d, null, legendColorList, true, "{2}");
        //在D盘目录下生成图片

        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, (int)PageSize.A4.getHeight()-10, 475, null);
        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bas.toByteArray());
        return image;
    }

 

    /**
     * 生成柱状图
     */
    public static com.itextpdf.text.Image barChart(String xAxisTitle) throws Exception {
        //x轴名称列表
        List<String> xAxisNameList = new ArrayList<>(Arrays.asList(
                "开关1",
                "开关2",
                "开关3",
                "开关4",
                "开关5",
                "开关6",
                "开关7",
                "开关8",
                "开关9",
                "开关10"));
        //图例名称列表
        List<String> legendNameList = new ArrayList<>(Arrays.asList("智能保护跳闸次数"));
        //数据列表
        List<List<Object>> dataList = new ArrayList<>();
        dataList.add(new ArrayList<>(Arrays.asList(540, 90, 80, 78,70,50, 45, 0, 0, 0)));

        //图例背景颜色
        List<Color> legendColorList = new ArrayList<>(Arrays.asList(new Color(232, 83, 6)));
        JFreeChart chart = GenerateChartUtil.createBarChart("智能开关断路保护", legendNameList, xAxisNameList
                , dataList, JFreeChartUtil.createChartTheme("宋体"), "跳闸/次", xAxisTitle,
                0d, null, legendColorList, true, "{2}");
        //在D盘目录下生成图片

        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, (int)PageSize.A4.getHeight()-10, 475, null);
        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bas.toByteArray());
        return image;
    }

4、简单工具类后续再单独文件提供,下面是简单结果展示

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值