实战jfreechart

这是我几年前做过的一个真实的小项目,结构和设计上可能不怎么样,仅供刚接触jfreechart的朋友参考。 

显示图表的jsp:

 

test.jsp

 

<%@ page language="java" contentType="image/png;charset=GB2312"
	import="org.jfree.chart.ChartFactory"
	import="org.jfree.chart.JFreeChart"
	import="org.jfree.chart.plot.PlotOrientation"
	import="org.jfree.data.category.DefaultCategoryDataset"
	import="org.jfree.chart.servlet.ServletUtilities"
	import="org.jfree.chart.ChartRenderingInfo"
	import="org.jfree.chart.entity.StandardEntityCollection"
	import="testsystem.dao.chart.DatasetFromBuglist"
%>

<%  
  int width=600;
  int height=375;

  JFreeChart chart = new DatasetDAO ().get3DChartFromBuglist();
  ChartRenderingInfo info = new ChartRenderingInfo(new  StandardEntityCollection());

  // 设置图表的格式及文件名
  String fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);

  // 设置图表浏览路径
  String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
%>

<HTML>
 <HEAD>
       <TITLE></TITLE>
 </HEAD>
 <BODY>
       <P ALIGN="CENTER">
       <img src="<%=graphURL %>" border="1" alt="3d bar chart">
       </P>
 </BODY>
</HTML>

   

 

从数据库中检索要显示的数据:

 

public class DatasetDAO {

  public JFreeChart get3DChartFromBuglist() {
    return createChart(readData());
  }

  /**
   * get data from oracle database
   */
  public JDBCCategoryDataset readData() {
    JDBCCategoryDataset data = null;

    String dbUrl = "jdbc:oracle:thin:@localhost:1521:oradb";
    Connection conn;

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      System.out.println("loading oracle driver...!");

      conn = DriverManager.getConnection(dbUrl,"test","test");
      System.out.println("get Connection!");

      data = new JDBCCategoryDataset(conn);
      //SQL查询
	String sqlStr = “…”;	
      data.executeQuery(sqlStr);
      System.out.println("execute query!");

    } catch(ClassNotFoundException cnfe) {
      System.out.println("oracle driver can not be found!");
      System.out.println(cnfe.getMessage());

    } catch(SQLException sqle) {
      System.out.println("SQLException:");
      System.out.println(sqle.getMessage());

    } catch(Exception e) {
      System.out.println("Other Exception:");
      System.out.println(e.getMessage());
    }finally{
        conn.close();
    }

     return data;
  }

  public static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart3D(
      "各模块bug数量对比图",
      "当前版本中",
      "数量",
      categorydataset,
      PlotOrientation.VERTICAL,
      true,
      true,
      false
    );

    CategoryPlot categoryplot = jfreechart.getCategoryPlot();
    categoryplot.setForegroundAlpha(1.0F);
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    CategoryLabelPositions categorylabelpositions = categoryaxis.getCategoryLabelPositions();
    CategoryLabelPosition categorylabelposition = new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0D, CategoryLabelWidthType.RANGE, 0.3F);
    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.replaceLeftPosition(categorylabelpositions, categorylabelposition));

    //set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) categoryplot.getRangeAxis();
    rangeAxis.setRange(0,20);

    NumberTickUnit numberTickUnit = new NumberTickUnit(1, new DecimalFormat("0"));
    rangeAxis.setTickUnit(numberTickUnit);

    return jfreechart;
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值