直接上正文
public class DynamicReportsDemo {
/**
* @param args
*/
public static void main(String[] args) throws DRException {
//这是我之前生成好的柱形图类
JFreeChart ccc = demo_zhu.ccc();
//将柱形图变成图片,并设置大小
BufferedImage bufferedImage = ccc.createBufferedImage(800, 800);
JasperReportBuilder report = report();// 创建空报表
// 样式
StyleBuilder boldStl = DynamicReports.stl.style().bold();
StyleBuilder boldCenteredStl = DynamicReports.stl.style(boldStl)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
StyleBuilder titleStl = DynamicReports.stl.style(boldCenteredStl)
.setFontSize(16);
StyleBuilder columnTitleStl = DynamicReports.stl.style(boldCenteredStl)
.setBorder(DynamicReports.stl.pen1Point())
.setBackgroundColor(Color.LIGHT_GRAY);
//样式数据
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("caption","长度");
map.put("id", 123);
map.put("code", 234);
map.put("service", 345);
map.put("province",456);
map.put("city", 567);
map.put("type", 678);
map.put("name", 891);
list.add(map);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("caption","汇总");
map2.put("id", 1231);
map2.put("code", 2341);
map2.put("service", 3451);
map2.put("province",4561);
map2.put("city", 5671);
map2.put("type", 6781);
map2.put("name", 8911);
list.add(map2);
report
.columns(
Columns.column("材质_管径", "caption", DataTypes.stringType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),// 列
Columns.column("PE", "id", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("STEEL", "code", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("TONG", "service", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("TIE", "province", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("304", "city", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("305", "type", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER),
Columns.column("201", "name", DataTypes.integerType())
.setHorizontalAlignment(HorizontalAlignment.CENTER)
)
.setColumnTitleStyle(columnTitleStl)
.setHighlightDetailEvenRows(true)
// 标题
.title(Components.text("材质").setStyle(titleStl))
//底角
.pageFooter(Components.pageXofY().setStyle(boldCenteredStl))
.setDataSource(list);
StyleBuilder style = stl.style().setHorizontalImageAlignment(HorizontalImageAlignment.CENTER);
report.summary(
cmp.verticalList()
//插入生成后的图片
.add(cmp.image(bufferedImage).setFixedDimension(570, 400).setStyle(style))
);
report.show();
}
}
注:此方法经过实验,完全可以用Dynamicreports控件自带的导出工具,但是到处pdf时会遇到中文不显示的问题,给大家附上一个链接,我是从链接上学的,点我;
还有看使用JFreeChart生成的双Y轴柱形图/折线图的以及同一界面生成双饼图以及多饼图的可以点击文字链接看看