当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

iReport整合向量图形的使用心得


  补丁方式
  通过svg-patch-src修补jasperreports cvs源码,编译后与iReport源码整合。笔者发现了少数中文和xml校验等问题,通过修改源码可以解决。所以建议iText,jasperreports,jfreechart,iReport都使用cvs源码。
  
  中文问题
  修改com.lowagie.text.pdf.DefaultFontMapper类的awtToPdf方法:
  BaseFont bf = null;
    try {
      bf =
        BaseFont.createFont(
  System.getProperty(“font.chinese.SIMYOU”),"D:/wiki/iReport-0.3.0/fonts/SIMYOU.TTF",
          BaseFont.IDENTITY_H,
          BaseFont.EMBEDDED);
    } catch (DocumentException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  
  在Linux平台上部署的时候需要修改jfreechart相应的字体。
  
  iReport Chart
  iReport目前对chart支持很有限,不过通过image标签和scriptlet可以很好的嵌入jfreechart图形, 嵌入其它chart比如jcharts方法一样。
  

  Scriptlet代码
  public class ChartScriptlet extends JRDefaultScriptlet
  {
  /**
   *
   */
  public void afterReportInit() throws JRScriptletException
  {
    Connection con=null;
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      System.out.println("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    try {
  
      con =
        DriverManager.getConnection(
          "jdbc:oracle:thin:@127.0.0.1.38:1521:ora8i",
          "user",
          "password");
  
      String query =
        "select province.name as name,sum(bill.total) as total from province,bill,node where bill.node_id =node.id and node.province_id=province.id and bill.CREATE_TIME > to_date('2004-1-1','yyyy-MM-dd') group by province.name";
      Statement stmt = con.createStatement();
  
      // Submit a query, creating a ResultSet object
  
      ResultSet rs = stmt.executeQuery(query);
  
      while (rs.next()) {
        System.out.println(rs.getString("name"));
        pieDataset.setValue(
          rs.getString("name"),
          rs.getDouble("total"));
      }
      rs.close();
      stmt.close();
      con.close();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  
    JFreeChart chart =
      ChartFactory.createPieChart(
        "2004-1-1至今各省帐单数据统计图",
        pieDataset,
        true,
        true,
        false);
    JCommonDrawableWrapper chartWrapper = new JCommonDrawableWrapper(chart);
  
    this.setVariableValue("ChartImage", chartWrapper);
  
    BufferedImage image = chart.createBufferedImage(246, 303);
    this.setVariableValue("ChartImage2", image);
  
  }
  }
相关内容
赞助商链接