动态定位使用 jfree 图表生成的表格和条形图



嗨,我使用的是itextpdf-5.1.0.jar,显示表格(此表中的值是根据输入屏幕中的开始日期和结束日期从数据库中获取的)和上表中值的条形图。

问题是我想将条形图放置在表格下方,我

从数据库中获取的值可能会根据开始日期和结束日期而有所不同,现在我通过给出一些静态值(234,567)来定位表格和条形图,如果有大量值表值和条形图被覆盖。有没有其他方法可以动态定位表格和条形图。

Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
document.open();
document.add(new Paragraph("Batch Report:", FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, new CMYKColor(255, 255, 255, 255))));
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(4);
document.add(paragraph1);
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell c1;
for (BoardBean bean : listHeader) {
addColumn(bean.getID(),c1,table,myColor,btableheadercolor);   
}

向表中添加列值

private void addColumn(String text,PdfPCell c1,PdfPTable dataTable,BaseColor   myColor,BaseColor btablecolumncolor) {
  try {
  final Font tabletdcolor = new Font(Font.FontFamily.HELVETICA, 6, Font.NORMAL, BaseColor.BLACK);
  c1 = new PdfPCell(new Paragraph(text, tabletdcolor));
  cellStyle(c1, myColor, btablecolumncolor);
  dataTable.addCell(c1);
  } catch (Exception ex) {
ex.printStackTrace();
  }
}

生成条形图

JFreeChart reportBarChart = genBatchReportBarChart(listHeader);
PdfTemplate reportTemplate = contentByte.createTemplate(280, 230);
        Graphics2D reportGraphics = reportTemplate.createGraphics(280, 230, new DefaultFontMapper());
Rectangle2D reportRectangle = new Rectangle2D.Double(0, 0, 280, 230);
reportBarChart.draw(reportGraphics, reportRectangle);
reportGraphics.dispose();
contentByte.addTemplate(reportTemplate, 10, height+150);

现在在上面的代码中,我正在固定条形图的位置,如果值在数字上很低,那就很好,但如果它们在数字中很大,条形图将覆盖表格值。根据表格值,条形图需要对齐,我该如何实现这一目标。

表格添加到文档后,您可以询问表格的总高度,并使用该表格高度来决定图表的放置位置。

或者(甚至更容易实现):您可以将PdfTemplate包装在Image对象中:

Image img = Image.getInstance(reportTemplate);
添加

表后立即添加带有document.add()的图像(假设您要添加带有 document.add() 的表)。

最新更新