快速学习JasperReport-Chart图表

3 Chart图表

3.1 创建模板

在这里插入图片描述
(2)创建fileds
在这里插入图片描述
(3)创建chart图标
第一步:palette面板找到chart图表,拖拽到band中
第二步:选择需要的图表类型
在这里插入图片描述
第三步:设置图表参数
在这里插入图片描述

  1. Key: 圆饼图的内容是什么,也就是下面的 First,Second…的内容
  2. Value:这个圆饼图的比例依据,根据 Value 属性来显示每个 Key 占的比例
  3. Label:显示标签

3.2 PDF输出

3.2.1 实体类

public class UserCount {
    private String companyName;
    private Integer count;
    public UserCount(String companyName, Integer count) {
        this.companyName = companyName;
        this.count = count;
   }
    public String getCompanyName() {
        return companyName;
   }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
   }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
   }
}

3.2.2 PDF输出

//测试图表
    @GetMapping("/testJasper06")
    public void createPdf(HttpServletRequest request, HttpServletResponse response) 
throws Exception {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("templates/testChart.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2.创建JasperPrint,向jasper文件中填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            HashMap parameters = new HashMap();
            //parameters.put("userCountList",getUserList());
            //构造javaBean数据源
            JRBeanCollectionDataSource ds = new
JRBeanCollectionDataSource(getUserList());
            /**
             * 1.jasper文件流
             * 2.参数列表
             * 3.JRBeanCollectionDataSource
             */
            JasperPrint print = JasperFillManager.fillReport(fis, parameters,ds);
            //3.将JasperPrint已PDF的形式输出
            JasperExportManager.exportReportToPdfStream(print,os);
            response.setContentType("application/pdf");
       } catch (JRException e) {
            e.printStackTrace();
       }finally {
            os.flush();
       }
   }
    //创建数据库Connection
    public List<UserCount> getUserList() throws Exception {
        List<UserCount> list = new ArrayList<>();
        UserCount uc1 = new UserCount("传智播客",10);
        UserCount uc2 = new UserCount("黑马程序员",10);
        list.add(uc1);
        list.add(uc2);
        return list;
   }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地整合JasperReport,以下是整合步骤: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.17.0</version> </dependency> ``` 2. 配置JasperReport 在application.properties文件中添加以下配置: ``` # JasperReport配置 jasperreport.compile.on.first.use=true jasperreport.cache.directory=./jasperreport_cache ``` 3. 编写Controller 编写Controller,使用JasperReport生成报表并返回给前端。 ``` @RestController public class ReportController { @GetMapping("/report") public void generateReport(HttpServletResponse response) throws Exception { // 获取JasperReport模板文件 InputStream jasperStream = this.getClass().getResourceAsStream("/reports/report.jasper"); // 设置参数 Map<String, Object> params = new HashMap<>(); params.put("title", "报表标题"); // 获取数据源 List<ReportData> data = getData(); JRDataSource dataSource = new JRBeanCollectionDataSource(data); // 编译JasperReport模板文件 JasperReport jasperReport = JasperCompileManager.compileReport(jasperStream); // 填充JasperReport模板文件 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource); // 导出报表 response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=report.pdf"); final OutputStream outputStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); } private List<ReportData> getData() { // 获取数据源 // ... } } ``` 4. 编写JasperReport模板文件 在resources目录下创建reports目录,并在该目录下创建report.jrxml文件,编写JasperReport模板文件。 5. 运行程序 运行程序,访问http://localhost:8080/report,即可生成报表并在浏览器中显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值