JFreeChart项目实例

最近利用JFreeChart和MySQL数据库做了一个JSP网页,展现Android Martet全球12个国家的TOP800游戏排名的曲线走势


准备知识,请先阅读我先前写的博客
JFreeChart学习示例

Linux JSP连接MySQL数据库


需导入jar包如下:


完整代码:

[java]   view plain copy print ?
  1. <%@ page language="java" contentType="text/html" pageEncoding="utf-8" %>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">  
  3.    
  4.  <%@ page language="java" import="java.sql.*" %>  
  5.  <%@ page language="java" import="java.io.*" %>  
  6.    
  7.  <%@ page language="java" import="java.io.*" %>  
  8.  <%@ page language="java" import="java.awt.*" %>  
  9.  <%@ page language="java" import="org.jfree.data.*" %>  
  10.  <%@ page language="java" import="org.jfree.data.category.*" %>  
  11.  <%@ page language="java" import="org.jfree.data.general.*" %>  
  12.  <%@ page language="java" import="org.jfree.chart.*" %>  
  13.  <%@ page language="java" import="org.jfree.chart.entity.*" %>  
  14.  <%@ page language="java" import="org.jfree.chart.plot.*" %>  
  15.    
  16.   
  17. <%  
  18.     // GLOBLE PARAMS  
  19.     // MySQL 连接JDBC  
  20.     String MYSQL_DRIVER = "com.mysql.jdbc.Driver";  
  21.     String MYSQL_URL = "jdbc:mysql://localhost:3306/top800";  
  22.   
  23.     // 数据库查询语句  
  24.     String chartTitle = "GameName of Country";  
  25.     String sql = "select * from (select name, id, dtime, top, country, category, rating, ratingcount, download, price, version, filesize, requireandroid, url from gametop800 where name like \"%3D Bowling%\" and country = \"usa\" order by dtime desc limit 0, 2147483647) as tbl order by dtime asc";  
  26.  %>  
  27.   
  28.   
  29.   
  30. <%@page import="org.jfree.chart.plot.PlotOrientation"%>  
  31. <%@page import="java.util.Date"%>  
  32. <%@page import="org.jfree.chart.servlet.ServletUtilities"%>  
  33. <%@page import="org.jfree.chart.axis.NumberAxis"%>  
  34. <%@page import="org.jfree.chart.axis.CategoryAxis"%>  
  35. <%@page import="org.jfree.chart.axis.CategoryLabelPositions"%><html>  
  36. <head>  
  37. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  38. <title>Top800 Game Free Chart Line</title>  
  39. </head>  
  40.   
  41. <body>  
  42. <center>  
  43. <form method="post">  
  44.   
  45. <div style="margin: auto 30px; width: 1000px" >  
  46.   
  47.     <select name="game_type">  
  48.         <option value="游戏名称">游戏名称</option>  
  49.         <option value="游戏包名">游戏包名</option>  
  50.     </select>:  
  51.     <input type="text" name="game_keyword" style="width:300px" />  
  52.       
  53.           
  54.     所属国家:  
  55.     <select name="countryname"  style="width:100px">  
  56.         <option value="usa">usa</option>  
  57.         <option value="england">england</option>  
  58.         <option value="france">france</option>  
  59.         <option value="japan">japan</option>  
  60.         <option value="italy">italy</option>  
  61.         <option value="german">german</option>  
  62.         <option value="india">india</option>  
  63.         <option value="spain">spain</option>  
  64.         <option value="russia">russia</option>  
  65.         <option value="china">china</option>  
  66.         <option value="eu">eu</option>  
  67.     </select>  
  68.       
  69.           
  70.     记录时间:  
  71.     <select name="game_dtime">  
  72.         <option value="全部">全部</option>  
  73.         <option value="最近一周">最近一周</option>  
  74.         <option value="最近一月">最近一月</option>  
  75.     </select>  
  76.       
  77.           
  78.     <input type="submit" value="查询" style="width:60px">  
  79.   
  80. </div>  
  81.   
  82.     <hr style="width: 80%" />  
  83.       
  84.     <%  
  85.             String game_type = new String("游戏名称");  
  86.             String game_keyword = "3D Bowling";  
  87.             String countryname = "usa";  
  88.             String game_dtime = new String("全部");  
  89.   
  90.             request.setCharacterEncoding("utf-8");  
  91.             game_type    = request.getParameter("game_type");  
  92.             game_keyword = request.getParameter("game_keyword");  
  93.             countryname  = request.getParameter("countryname");  
  94.             game_dtime   = request.getParameter("game_dtime");  
  95.   
  96.             int limitTop = Integer.MAX_VALUE;  
  97.   
  98.             game_type = game_type == null ? game_type = "游戏名称" : game_type.trim();  
  99.             game_keyword = game_keyword == null ? game_keyword = "3D Bowling" : game_keyword.trim();  
  100.             countryname  = countryname  == null ? countryname  = "usa" : countryname.trim();  
  101.             game_dtime   = game_dtime   == null ? game_dtime   = "全部" : game_dtime.trim();  
  102.   
  103.             if (game_dtime != null) {  
  104.                 if (game_dtime.equals("全部")) {  
  105.                     limitTop = Integer.MAX_VALUE;  
  106.                 } else if (game_dtime.equals("最近一周")) {  
  107.                     limitTop = 7;  
  108.                 } else if (game_dtime.equals("最近一月")) {  
  109.                     limitTop = 30;  
  110.                 } else {  
  111.                     limitTop = Integer.MAX_VALUE;  
  112.                 }  
  113.             }  
  114.   
  115.             if (game_keyword != null && game_keyword != "") {  
  116.                 chartTitle = game_keyword + " of " + countryname;  
  117.   
  118.                 if (game_type.equals("游戏名称")) {  
  119.                     sql = "select * from (select name, id, dtime, top, country, category, rating, ratingcount, download, price, version, filesize, requireandroid, url from gametop800 where name like \"%"  
  120.                             + game_keyword  
  121.                             + "%\" and country = \""  
  122.                             + countryname  
  123.                             + "\" order by dtime desc limit 0, "  
  124.                             + limitTop + ") as tbl order by dtime asc";  
  125.   
  126.                 } else if (game_type.equals("游戏包名")) {  
  127.                     sql = "select * from (select name, id, dtime, top, country, category, rating, ratingcount, download, price, version, filesize, requireandroid, url from gametop800 where id = \""  
  128.                             + game_keyword  
  129.                             + "\" and country = \""  
  130.                             + countryname  
  131.                             + "\" order by dtime desc limit 0, "  
  132.                             + limitTop + ") as tbl order by dtime asc";  
  133.   
  134.                 } else {  
  135.                     sql = "select * from (select name, id, dtime, top, country, category, rating, ratingcount, download, price, version, filesize, requireandroid, url from gametop800 where name like \"%"  
  136.                             + game_keyword  
  137.                             + "%\" and country = \""  
  138.                             + countryname  
  139.                             + "\" order by dtime desc limit 0, "  
  140.                             + limitTop + ") as tbl order by dtime asc";  
  141.   
  142.                 }  
  143.             } else {  
  144.                 game_type = "游戏名称";  
  145.                 game_keyword = "3D Bowling";  
  146.                 countryname  = "usa";  
  147.                 game_dtime   = "全部";  
  148.             }  
  149.         %>  
  150.   
  151.   
  152.     <table border="2" width="40%">  
  153.         <tr>  
  154.             <td valign="middle"><b><%=game_type%>:</b></td>  
  155.             <td><%=game_keyword%> </td>  
  156.         </tr>  
  157.         <tr>  
  158.             <td valign="middle"><b>所属国家:</b></td>  
  159.             <td><%=countryname%></td>  
  160.         </tr>  
  161.         <tr>  
  162.             <td valign="middle"><b>记录时间:</b></td>  
  163.             <td><%=game_dtime%></td>  
  164.         </tr>  
  165.     </table>  
  166.       
  167.       
  168.     <hr style="width: 80%" />  
  169.       
  170. <%  
  171.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  172.   
  173.         Connection conn = null;  
  174.         Statement stmt = null;  
  175.         ResultSet rs = null;  
  176.   
  177.         try {  
  178.             // 连接 MySQL 数据库  
  179.             Class.forName(MYSQL_DRIVER).newInstance();  
  180.             conn = DriverManager.getConnection(MYSQL_URL, "root""");  
  181.             stmt = conn.createStatement();  
  182.             rs = stmt.executeQuery(sql);  
  183.   
  184.             // 遍历读取的数据集  
  185.             while (rs.next()) {  
  186.                 String name = rs.getString(1);  
  187.                 String dtime = rs.getString(3);  
  188.                 int top = rs.getInt(4);  
  189.   
  190.                 if (name != null && dtime != null) {  
  191.                     dataset.addValue(top, name, dtime);     // 构造JFreeChart的数据集dataset  
  192.                 }  
  193.             }  
  194.   
  195.         } catch (Exception e) {  
  196.             System.err.println(e.getMessage());  
  197.   
  198.         }  
  199.   
  200.         JFreeChart chart = ChartFactory.createLineChart(  
  201.                 chartTitle,     // 标题  
  202.                 "记录时间",         // 横轴名称  
  203.                 "游戏排名",         // 纵轴名称  
  204.                 dataset,        // 数据集  
  205.                 PlotOrientation.VERTICAL,   // 垂直视图   
  206.                 true,   
  207.                 true,   
  208.                 true  
  209.         );  
  210.   
  211.         final CategoryPlot plot = (CategoryPlot) chart.getPlot();  
  212.         plot.setBackgroundPaint(Color.lightGray);   // 背景色  
  213.         plot.setRangeGridlinePaint(Color.blue);     // 横轴虚线  
  214.         plot.setRangeGridlinesVisible(true);        // 横轴虚线是否可见  
  215.   
  216.         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();  
  217.         rangeAxis.setAutoRangeMinimumSize(1);       // 距离为1  
  218.         rangeAxis.setAutoRangeIncludesZero(true);   // 从零计算  
  219.         rangeAxis.setInverted(true);                // 纵轴逆序(原点到顶端,是从大到小)  
  220.   
  221.         final CategoryAxis categoryAxis = plot.getDomainAxis();  
  222.         if (limitTop == 7) {  
  223.             categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);    // 横轴标准显示(水平)  
  224.         } else {  
  225.             categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);       // 横轴45度显示(倾斜)  
  226.         }  
  227.   
  228.         FileOutputStream fos_jpg = null;  
  229.         String file_jpg = null;  
  230.         String url_jpg = null;  
  231.   
  232.         try {  
  233.             final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());  
  234.   
  235.             file_jpg = ServletUtilities.saveChartAsJPEG(chart, 1200600, info, null);          // 生成图片  
  236.             url_jpg = request.getContextPath() + "/servlet/DisplayChart?filename=" + file_jpg;  // 图片路径  
  237.   
  238.         } catch (Exception e) {  
  239.             out.println(e);  
  240.         } finally {  
  241.             try {  
  242.                 fos_jpg.close();  
  243.             } catch (Exception e) {  
  244.                 e.printStackTrace();  
  245.             }  
  246.         }  
  247.     %>  
  248.    
  249. <img src=<%=url_jpg%>  border="1"  />   <!-- 显示图片(url_jpg为JFreeChart生成图片的路径) -->   
  250.   
  251. <hr style="width: 80%" />  
  252.   
  253. <table border="2" borderColor="#00000" cellPadding="0" cellSpacing="0" width="auto" >  
  254.     <tbody>  
  255.         <tr>  
  256.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  257.                 <font color="#ffffff"><b>name</b></font></td>  
  258.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  259.                 <font color="#ffffff"><b>id</b></font></td>  
  260.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  261.                 <font color="#ffffff"><b>dtime</b></font></td>  
  262.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  263.                 <font color="#ffffff"><b>top</b></font></td>  
  264.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  265.                 <font color="#ffffff"><b>country</b></font></td>  
  266.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  267.                 <font color="#ffffff"><b>category</b></font></td>  
  268.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  269.                 <font color="#ffffff"><b>rating</b></font></td>  
  270.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  271.                 <font color="#ffffff"><b>ratingcount</b></font></td>  
  272.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  273.                 <font color="#ffffff"><b>download</b></font></td>  
  274.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  275.                 <font color="#ffffff"><b>price</b></font></td>  
  276.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  277.                 <font color="#ffffff"><b>version</b></font></td>  
  278.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  279.                 <font color="#ffffff"><b>filesize</b></font></td>  
  280.             <td bgColor="#008080" height="28" align="center" valign="middle">  
  281.                 <font color="#ffffff"><b>requireandroid</b></font></td>  
  282.         </tr>  
  283.         <%  
  284.             try {  
  285.                 rs = stmt.executeQuery(sql);  
  286.                 while (rs.next()) {  
  287.         %>  
  288.         <tr>  
  289.             <td height="18" vAlign="middle" align="center"><a href="<%=rs.getString(14)%>" target="_blank"><%=rs.getString(1)%></a></td>  
  290.             <td height="18" valign="middle" align="center"><%=rs.getString(2)%></td>  
  291.             <td height="18" valign="middle" align="center"><%=rs.getString(3)%></td>  
  292.             <td height="18" valign="middle" align="center" bgColor="#ffcc68"><%=rs.getInt(4)%></td>  
  293.             <td height="18" valign="middle" align="center"><%=rs.getString(5)%></td>  
  294.             <td height="18" valign="middle" align="center"><%=rs.getString(6)%></td>  
  295.             <td height="18" valign="middle" align="center"><%=rs.getString(7)%></td>  
  296.             <td height="18" valign="middle" align="center"><%=rs.getString(8)%></td>  
  297.             <td height="18" valign="middle" align="center"><%=rs.getString(9)%></td>  
  298.             <td height="18" valign="middle" align="center"><%=rs.getString(10)%></td>  
  299.             <td height="18" valign="middle" align="center"><%=rs.getString(11)%></td>  
  300.             <td height="18" valign="middle" align="center"><%=rs.getString(12)%></td>  
  301.             <td height="18" valign="middle" align="center"><%=rs.getString(13)%></td>  
  302.         </tr>  
  303.         <%  
  304.             }  
  305.             } catch (Exception e) {  
  306.                 System.out.println(e.getMessage());  
  307.             }  
  308.         %>  
  309.   
  310.     </tbody>  
  311. </table>  
  312.   
  313.   
  314. <%  
  315.     try {  
  316.         rs.close();  
  317.         stmt.close();  
  318.         conn.close();  
  319.   
  320.     } catch (Exception e) {  
  321.         System.out.println(e.getMessage());  
  322.   
  323.     }  
  324. %>  
  325.   
  326. </form>  
  327. </center>  
  328. </body>  
  329. </html>  

效果图:



这是我们公司(创新工场 Doodle Mobile)出品的经典休闲游戏——3D Bowling,目前已经进入Android Market全球排名前10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值