1. 下载jfreechart-1.0.9.zip 包,解压将下面的.jar 文件放入自己工程的lib下.
地址:http://download.csdn.net/detail/chenjianhuacool/4441609
2. 在web.xml 文件中添加一个servlet,如下所示:
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class> org.jfree.chart.servlet.DisplayChart </servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping>
3. 编写java 类
package com.cchc.thfnt.delClient; import java.awt.Color; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.time.Day; import org.jfree.data.time.Hour; import org.jfree.data.time.Minute; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.ui.RectangleInsets; import com.cchc.common.util.DateFormat; public class WebChart { private String stringdate=""; public WebChart(String time){ if("".equals(time)||time==null){ Date date=new Date(System.currentTimeMillis()); stringdate=DateFormat.shortDate(date); }else{ stringdate=time; } } public String getFolddown(HttpServletRequest request, HttpSession session) { // 设置曲线, TimeSeries timeseries1 = new TimeSeries("usl", Minute.class); List<String> list=new ArrayList<String>(); list.add(stringdate+" 08:00"); list.add(stringdate+" 08:15"); list.add(stringdate+" 08:30"); list.add(stringdate+" 08:45"); list.add(stringdate+" 09:00"); list.add(stringdate+" 09:15"); list.add(stringdate+" 09:30"); list.add(stringdate+" 09:45"); list.add(stringdate+" 10:00"); list.add(stringdate+" 10:15"); list.add(stringdate+" 10:30"); list.add(stringdate+" 10:45"); list.add(stringdate+" 11:00"); list.add(stringdate+" 11:15"); list.add(stringdate+" 11:30"); list.add(stringdate+" 11:45"); list.add(stringdate+" 12:00"); list.add(stringdate+" 12:15"); list.add(stringdate+" 12:30"); list.add(stringdate+" 12:45"); list.add(stringdate+" 13:00"); list.add(stringdate+" 13:15"); list.add(stringdate+" 13:30"); list.add(stringdate+" 13:45"); list.add(stringdate+" 14:00"); list.add(stringdate+" 14:15"); list.add(stringdate+" 14:30"); list.add(stringdate+" 14:45"); list.add(stringdate+" 15:00"); list.add(stringdate+" 15:15"); list.add(stringdate+" 15:30"); list.add(stringdate+" 15:45"); list.add(stringdate+" 16:00"); list.add(stringdate+" 16:15"); list.add(stringdate+" 16:30"); list.add(stringdate+" 16:45"); list.add(stringdate+" 17:00"); list.add(stringdate+" 17:15"); list.add(stringdate+" 17:30"); list.add(stringdate+" 17:45"); list.add(stringdate+" 18:00"); list.add(stringdate+" 18:15"); list.add(stringdate+" 18:30"); list.add(stringdate+" 18:45"); list.add(stringdate+" 19:00"); list.add(stringdate+" 19:15"); list.add(stringdate+" 19:30"); list.add(stringdate+" 19:45"); list.add(stringdate+" 20:00"); list.add(stringdate+" 20:15"); list.add(stringdate+" 20:30"); list.add(stringdate+" 20:45"); list.add(stringdate+" 21:00"); list.add(stringdate+" 21:15"); list.add(stringdate+" 21:30"); list.add(stringdate+" 21:45"); list.add(stringdate+" 22:00"); list.add(stringdate+" 22:15"); list.add(stringdate+" 22:30"); list.add(stringdate+" 22:45"); list.add(stringdate+" 23:00"); list.add(stringdate+" 23:15"); list.add(stringdate+" 23:30"); list.add(stringdate+" 23:45"); list.add(stringdate+" 24:00"); for(int i=0;i<list.size()-1;i++){ String time=(String) list.get(i+1); float y1=new Page().getCounts(list.get(i), list.get(i+1)); int x = Integer.parseInt(time.substring(0, 4)); int y = Integer.parseInt(time.substring(5, 7)); int z = Integer.parseInt(time.substring(8, 10)); int a = Integer.parseInt(time.substring(11, 13)); int b = Integer.parseInt(time.substring(14, 16)); timeseries1.add(new Minute(b, new Hour(a, new Day(z, y, x))), y1); } // 连接曲线 TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(timeseries1); dataset.setDomainIsPointsInTime(true); // 设置曲线图 XYDataset xydataset = (XYDataset) dataset; JFreeChart chart = ChartFactory.createTimeSeriesChart( "穿墙网--8时到24时帖子代维走势图", "时间", "15分钟内代维的帖子数量", xydataset, false, false, false); chart.setBackgroundPaint(Color.white);// 设置曲线图背景色 XYPlot plot = (XYPlot) chart.getPlot(); XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot .getRenderer(); plot.setBackgroundPaint(Color.white);// 设置网格背景颜色 plot.setDomainGridlinePaint(Color.pink);// 设置网格竖线颜色 plot.setRangeGridlinePaint(Color.pink);// 设置网格横线颜色 plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));// 设置曲线图与xy轴的距离,即曲线与xy轴贴近的距离 xylineandshaperenderer.setBaseShapesVisible(true);// 设置曲线是否显示数据点 String filename=""; try { filename = ServletUtilities.saveChartAsPNG(chart, 970, 400, null, session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; return filename; } }
4. 编写jsp 页面
<%@ page contentType="text/html;charset=GBK"%> <%@ page import="com.cchc.thfnt.delClient.WebChart"%> <% String time = request.getParameter("time"); WebChart chart = new WebChart(time); String filename = chart.getFolddown(request, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %> <HTML> <HEAD> <TITLE>代维走势图</TITLE> <script language="javascript" src="<%=request.getContextPath() %>/js/js/pageCommon.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/CN/systemMsg.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/general.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/CN/calendar.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/calendar.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/clock.js"></script> <script language="javascript" src="<%=request.getContextPath() %>/js/js/CN/clock.js"></script> <LINK href="<%=request.getContextPath() %>/css/css/CN/BLUE/mainWin.css" type=text/css media=screen rel=stylesheet> <LINK href="<%=request.getContextPath() %>/css/css/CN/BLUE/query.css" type=text/css media=screen rel=stylesheet> <LINK href="<%=request.getContextPath() %>/css/css/CN/BLUE/calendar.css" type=text/css media=screen rel=stylesheet> <LINK href="<%=request.getContextPath() %>/css/css/comm/BLUE/mainWin.css" type=text/css media=screen rel=stylesheet> <LINK href="css/mgcss.css" type=text/css rel=stylesheet> <script language="javascript"> function searchbbs(){ var time=document.getElementByIdx_x_x("startDate").value; if(time==""){ alert("时间不能为空,请输入时间."); return; } window.location="<%=request.getContextPath()%>"+"/delClient/reportForms.jsp?time="+time; } </script> </HEAD> <BODY> <table width="970" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><jsp:include flush="true" page="/common/head.jsp"></jsp:include></td> </tr> </table> <table width="970" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="980" height="4" align="center" valign="middle"></td> </tr> </table> <table width="970" height="2" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="4" align="" width="100%"><font color="red">此处可以查看任何一天的代维走势图</font> 请输入日期:<input type="test" name="startDate" size="10" onblur="TextOnBlur(this);" id="startDate" styleClass="inputdate" onfocus="this.select();" /> <INPUT class="DateButton" type="button" value="..." onclick="OpenDate(document.all.startDate);"> <input type="button" name="Submit" value="查询" class="btn_03" onclick="searchbbs()"></td> </tr> </table> <img src="<%= graphURL %>" border=0"> <table width="970" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="980" height="4" align="center" valign="middle"></td> </tr> </table> <table width="970" height="84" border="0" align="center" cellpadding="0" cellspacing="7" bgcolor="#CCCCCC"> <tr> <td height="23" bgcolor="#FFFFFF"><%@ include file="/common/foot.jsp"%></td> </tr> </table> </BODY> </HTML>
至此 一个折线图就生成完毕!
-----------------------------------------------------
转载请注明来源此处
原地址:#
发表