java生成图表格式图片(1)

因为前段时间有这个需求,并且一张图片中可能有多个图表,每个图表有都有各自的标题和TableHeader(th). 所以研究了一下,这一篇的思路是直接生成图片:
1.工具类

    public static String graphicsGeneration(List<List<List<String>>> allValue,List<String> titles,List<String[]> headers ,String receiver,int totalcol) throws Exception {
        int rows = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                rows += (2+typeV.size());
            }
        }
        // 实际数据行数+标题+备注
        int totalrow = 1+rows;
        int imageWidth = 800;
        int imageHeight = totalrow * 30 + 20;
        int rowheight = 30;
        int startHeight = 10;
        int startWidth = 10;
        int colwidth = ((imageWidth - 20) / totalcol);

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        //画背景
        graphics.setColor(new Color(0, 112, 192));
        int startH = 1;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.fillRect(startWidth+1, startHeight+startH*rowheight+1, imageWidth - startWidth-5-1,rowheight-1);
                startH+=2+typeV.size();
            }
        }

        graphics.setColor(new Color(220, 240, 240));
        // 画横线

        for (int j = 0; j < totalrow - 1; j++) {
            graphics.setColor(Color.black);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
                    startHeight + (j + 1) * rowheight);
        }

        // 画竖线
        graphics.setColor(Color.black);
        startH = 1;
        int rightLine = 0 ;
        for (List<List<String>> typeV : allValue) {

            if (typeV != null && typeV.size() > 0) {
                for (int k = 0; k < totalcol+1; k++) {
                    rightLine = getRightMargin(k,startWidth, colwidth,imageWidth);
                    graphics.drawLine(rightLine, startHeight + startH*rowheight, rightLine,
                            startHeight + (typeV.size()+1+startH)*rowheight);
                }
                startH+=2+typeV.size();
            }
        }

        // 设置字体
        Font font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);

        // 写标题
        startH = 1;
        int i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.drawString(titles.get(i), imageWidth / 3 + startWidth+30, startHeight + startH*rowheight - 10);
                startH+=2+typeV.size();
            }
            i++;
        }


        // 写入表头
        graphics.setColor(Color.WHITE);
        font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);
        startH = 2;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {

                String[] headCells = headers.get(i);
                for (int m = 0; m < headCells.length; m++) {
                    rightLine = getRightMargin(m,startWidth, colwidth,imageWidth)+5;
                    graphics.drawString(headCells[m].toString(), rightLine,
                            startHeight + rowheight * startH - 10);
                }
                startH+=2+typeV.size();
            }
            i++;
        }


        // 写入内容
        graphics.setColor(Color.black);
        font = new Font("华文楷体", Font.PLAIN, 20);
        graphics.setFont(font);
        startH = 3;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                for (int n = 0; n < typeV.size(); n++) {
                    List<String> arr = typeV.get(n);
                    for (int l = 0; l < arr.size(); l++) {
                        rightLine = getRightMargin(l,startWidth, colwidth,imageWidth)+5;
                        graphics.drawString(arr.get(l).toString(), rightLine,
                                startHeight + rowheight * (n + startH) - 10);
                    }
                }
                startH+=2+typeV.size();
            }
            i++;
        }

        String path = "1.jpg";
        ImageIO.write(image, "jpg", new File(path));
        return path;
    }

    /**
     * 获取竖线和文字的水平位置
     * @param k
     * @param startWidth
     * @param colwidth
     * @param imageWidth
     * @return
     */
    private static int getRightMargin(int k, int startWidth, int colwidth, int imageWidth) {
        int rightLine = 0;
        if (k == 0) {
            rightLine = startWidth;
        } else if (k == 1) {
            rightLine = startWidth + colwidth / 2;
        } else if (k == 2) {
            rightLine = startWidth + 3 * colwidth / 2;
        } else if (k == 3) {
            rightLine = startWidth + 7 * colwidth / 2;
        } else if (k == 4) {
            rightLine = imageWidth - 5;
        }
        return rightLine;
    }

2.传递图表数据

public void initChartData(){
        List<List<List<String>>> allValue = new ArrayList<>();                                                                                
        List<String> content1 =Arrays.asList(new String[]{"刘丹丹","25","163cm","未婚"});  
         List<String> content2 = Arrays.asList(new String[]{"刘丹丹","25","163cm","未婚"});  
         List<String> content3 = Arrays.asList(new String[]{"刘丹丹","宿迁","本科","未婚"});  
         List<List<String>> contentArray1 = new ArrayList<>();
         contentArray1.add(content1);
         contentArray1.add(content2);
         List<List<String>> contentArray2 = new ArrayList<>();
         contentArray2.add(content3);
         allValue.add(contentArray1);
         allValue.add(contentArray2);

         List<String[]> headTitles = new ArrayList<>();
         String[] h1 = new String[]{"名字","年龄","身高","婚姻"};
         String[] h2 = new String[]{"名字","籍贯","学历","婚姻"};
         headTitles.add(h1);
         headTitles.add(h2);

         List<String> titles = new ArrayList<>();
         titles.add("制造部门人员统计");
         titles.add("SQE部门人员统计");
         graphicsGeneration(allValue,titles,headTitles ,"",4)
 }

3.测试代码

    public class ImageTest {
         public static void main(String[] args) {  
             initChartData();
         }  
    }

执行之后,可以看到生成的图表图片。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用Java生成echarts图片,可以使用Java的开源库echarts-java。该库提供了Java与echarts的集成,可以通过Java代码生成echarts图表,并将其保存为图片。 首先,需要在项目中导入echarts-java的依赖,可以使用Maven或Gradle进行管理。 在代码中,首先创建一个echarts对象,通过设置不同的属性来配置图表的内容和样式。例如,可以设置图表类型、标题、横纵坐标等。 然后,可以创建一个echarts图片生成器对象,将echarts对象作为参数传递给生成器。可以设置生成图片格式、大小和保存路径等。 最后,调用生成器的generate方法,即可根据echarts对象生成相应的图片并保存。 以下是一个简单的示例代码: ```java import com.github.abel533.echarts.Option; import com.github.abel533.echarts.json.GsonOption; import com.github.abel533.echarts.utils.EchartsUtils; import com.github.abel533.echarts.util.EnhancedOption; import com.github.abel533.echarts.image.ZEChartsConfig; import com.github.abel533.echarts.image.ZEChartsRenderTool; public class EchartsImageGenerator { public static void main(String[] args) { // 创建echarts对象 EnhancedOption option = new EnhancedOption(); option.title().text("示例图表"); option.legend().data("A", "B"); option.xAxis().data("1月", "2月", "3月", "4月", "5月"); option.yAxis().name("销量"); // 添加数据系列 option.series("A", "bar", new Integer[]{10, 20, 30, 40, 50}); option.series("B", "line", new Integer[]{5, 10, 15, 20, 25}); // 创建echarts图片生成器对象 ZEChartsConfig config = new ZEChartsConfig(); config.setRenderTool(ZEChartsConfig.getRenderToolOrInstance()); config.setImagePath("data:image/png;base64"); config.setCharsetName("UTF-8"); ZEChartsRenderTool renderTool = new ZEChartsRenderTool(); renderTool.setConfig(config); // 生成图片并保存 String outputPath = "path/to/output/image.png"; renderTool.renderToPath(EchartsUtils.getInstanceFromOption(option, GsonOption.class), outputPath); } } ``` 通过上述代码,我们可以利用Java调用echarts-java库来生成echarts图片。可以根据实际需求,使用不同的图表类型和数据来定制生成图片

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值