小试jfreeChart

程序代码

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.jfree.chart.title.TextTitle" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="java.awt.Font" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.servlet.ServletUtilities" %>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection" %>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %>
<%@ page import="org.jfree.data.category.DefaultCategoryDataset" %>
<%@ page import="org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %>
<html>
<head><title>Jfree图表</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<%

//设置生成饼状图
    int i=0;
    String[] area={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    try{
        Class.forName(" oracle.jdbc.driver.OracleDriver").newInstance(); 
        String url="jdbc: oracle:thin:@amd02:1521:orcl"; 
        Connection conn= DriverManager.getConnection(url,"lucky","lucky"); 

        Statement sql=conn.createStatement();
        ResultSet rs=sql.executeQuery("select * from chart");

        PrintWriter pw=null;
        DefaultPieDataset dataset=new DefaultPieDataset();
        //设置数据源
        while(rs.next()){
          String dq=rs.getString("dq");

           dataset.setValue(area[i],Integer.parseInt(dq));
         
          //添加数据,参数包括名称和数值
           i++;
        }
        rs.close();
        sql.close();
        conn.close();
       
        JFreeChart chart=ChartFactory.createPieChart("饼型图",dataset,true,true,true);
        chart.setTitle("短信统计图");//设置图片标题
        ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
        String filename=ServletUtilities.saveChartAsJPEG(chart,600,400,info,session);
        //设置生成图片,包括图片的大小,长度是600,宽是400
        session.setAttribute("file_name1",filename);
        String URL= request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
        session.setAttribute("file_url1",URL);

        }
        catch(SQLException e){
          out.println(e);
       }
        


      %>

<%

//生成柱状图
        try{
         int j=0;
        Class.forName(" oracle.jdbc.driver.OracleDriver").newInstance(); 
        String url="jdbc: oracle:thin:@amd02:1521:orcl"; 
        Connection conn= DriverManager.getConnection(url,"lucky","lucky"); 
         Statement sql=conn.createStatement();
         ResultSet rs=sql.executeQuery("select * from chart");
         DefaultCategoryDataset dataset=new DefaultCategoryDataset();//设置数据源

         while(rs.next()){
         dataset.addValue(Integer.parseInt(rs.getString("dq")),"学校",area[j]);
         //添加数据,参数包括名称和数值
         j++;
         }

         JFreeChart chart1=ChartFactory.createLineChart("统计图","月份","短信数量",
                dataset,PlotOrientation.VERTICAL,false,false,false);
         
         //设置图表的背景颜色
          //chart1.setBackgroundPaint(Color.white);
         CategoryPlot plot=chart1.getCategoryPlot();
         plot.setBackgroundAlpha(0.3f);
         plot.setForegroundAlpha(1.0f);
         //自定义标题
         Font font = new Font("黑体",Font.CENTER_BASELINE,20);    
         TextTitle title = new TextTitle(" 的自定义标题");    
         title.setFont(font);     
         chart1.setTitle(title);    
  
         BarRenderer3D renderer1=new BarRenderer3D();
        
         renderer1.setItemMargin(80);//设置每个柱子之间的距离
         //plot.setRenderer(renderer1);//让上面对柱子的设置生效
        String filename=ServletUtilities.saveChartAsJPEG(chart1,600,400,session);
         //生成图片,包括图片的大小

         session.setAttribute("file_name2",filename);
         String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+filename;

         session.setAttribute("file_url2",URL);



         }catch(SQLException e){}

%>
<center><img src="<%=session.getAttribute("file_url1")%>" width=600 height=400 border=0 usemap="#<%=session.getAttribute("file_name1") %>">

<img src="<%=session.getAttribute("file_url2") %>" width=600 height=400 border=0 usemap="#<%=session.getAttribute("file_name2")%>"></center>

</html>

小试jfreeChart

你可能感兴趣的:(oracle,sql,jdbc,servlet,jfreechart)