1. 下载jfreechart-1.0.9.zip 包,解压将下面的.jar 文件放入自己工程的lib下.
2. 在web.xml 文件中添加一个servlet,如下所示:
DisplayChart
org.jfree.chart.servlet.DisplayChart
DisplayChart
/servlet/DisplayChart
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 list=new ArrayList();
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
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 页面
String time = request.getParameter("time");
WebChart chart = new WebChart(time);
String filename = chart.getFolddown(request, session);
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
%>
代维走势图/css/css/CN/BLUE/mainWin.css"
type=text/css media=screen rel=stylesheet>
/css/css/CN/BLUE/query.css"
type=text/css media=screen rel=stylesheet>
/css/css/CN/BLUE/calendar.css"
type=text/css media=screen rel=stylesheet>
href="/css/css/comm/BLUE/mainWin.css"
type=text/css media=screen rel=stylesheet>
function searchbbs(){
var time=document.getElementByIdx_x_x("startDate").value;
if(time==""){
alert("时间不能为空,请输入时间.");
return;
}
window.location=""+"/delClient/reportForms.jsp?time="+time;
}
cellspacing="0">
cellspacing="0">
cellspacing="0">
此处可以查看任何一天的代维走势图 请输入日期:type="test" name="startDate" size="10" οnblur="TextOnBlur(this);"
id="startDate" styleClass="inputdate" οnfοcus="this.select();" />
οnclick="OpenDate(document.all.startDate);">
" border=0">
cellspacing="0">
cellspacing="7" bgcolor="#CCCCCC">
file="/common/foot.jsp"%>
至此 一个折线图就生成完毕!
-----------------------------------------------------
转载请注明来源此处
原地址:#