1、在tmeplate4_charts模板里创建Field
grade属性, 类型为java.lang.String
nums属性 类型为java.lang.Long
2、在右边的Basic Element中拖拽Chart
这里我们选择Pie Chart
点击完成。
最终的模板如下图所示
创建StudentCount 类
public class StudentCount { private String grade; private Long nums; public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public Long getNums() { return nums; } public void setNums(Long nums) { this.nums = nums; } }
java代码
@GetMapping("/jasper4_chart") public void jasper4_chart( HttpServletResponse response) throws Exception { HashMap<String, Object> parameters = new HashMap<String, Object>(); List<StudentCount> studentList = new ArrayList<>(); for (int i = 1; i <= 6; i++) { Random random = new Random(); int count = ((Double) (random.nextDouble() * 10)).intValue(); StudentCount s1 = new StudentCount(); s1.setGrade("grade"+ i ); s1.setNums(i * 20L + count); studentList.add(s1); } String templatePath = "templates/template4_charts.jasper"; JasperReportUtil.exportToPdf(templatePath, parameters, studentList, response); }
最终展示的pdf效果如下图