关于jfreechar从数据库取数据画图的问题
程序的源码
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import =" java.awt.*,
java.text.SimpleDateFormat,
javax.swing.JPanel,
org.jfree.chart.ChartFactory,
org.jfree.chart.ChartPanel,
org.jfree.chart.JFreeChart,
org.jfree.chart.axis.DateAxis,
org.jfree.chart.plot.XYPlot,
org.jfree.chart.renderer.xy.XYItemRenderer,
org.jfree.chart.renderer.xy.XYLineAndShapeRenderer,
org.jfree.data.time.*,
org.jfree.data.time.TimeSeries,
org.jfree.data.time.TimeSeriesCollection,
org.jfree.data.xy.XYDataset"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="java.util.*"%>
<%@ page import = "org.jfree.ui.RectangleInsets"%>
<%@ page import = "java.sql.*"%>
<%
//连接数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jfreechart";
String usr="sa";//自己可以根据自己的需要设置登录名
String pwd="123123";//设置登录密码
Connection conn=DriverManager.getConnection(url,usr,pwd);
Statement stmt=conn.createStatement();
String sql="select * from jfree order by lengthavg";
ResultSet rs=stmt.executeQuery(sql);
//设置曲线,共三条曲线
TimeSeries timeseries = new TimeSeries("平均",Minute.class);//时间的最小单位为分,线值名是“平均”
TimeSeries timeseries1 = new TimeSeries("usl",Minute.class);
TimeSeries timeseries2 = new TimeSeries("lsl",Minute.class);
//从数据库中取值,数据和时间
while(rs.next())
{
float y0=rs.getFloat("lengthavg");
float y1=rs.getFloat("lengthusl");
float y2=rs.getFloat("lengthlsl");
String s=rs.getString("addtime");
//从yyyyMMddHHmm时间格式的char中取出年月日小时分设为int值
int x=Integer.parseInt(s.substring(0,4));
int y=Integer.parseInt(s.substring(4,6));
int z=Integer.parseInt(s.substring(6,8));
int a=Integer.parseInt(s.substring(8,10));
int b=Integer.parseInt(s.substring(10,12));
timeseries.add(new Minute(b,new Hour(a,new Day(z,y,x))),y0);
timeseries1.add(new Minute(b,new Hour(a,new Day(z,y,x))),y1);
timeseries2.add(new Minute(b,new Hour(a,new Day(z,y,x))),y2);
}
//连接曲线
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(timeseries);
dataset.addSeries(timeseries1);
dataset.addSeries(timeseries2);
dataset.setDomainIsPointsInTime(true);
//设置曲线图
XYDataset xydataset = (XYDataset) dataset;
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"走势图",
"时间",
"值",
xydataset,
true,
true,
true
);
chart.setBackgroundPaint(Color.white);//设置曲线图背景色
XYPlot plot = (XYPlot)chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
plot.setBackgroundPaint(Color.white);
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 = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" border=0 usemap="#<%= filename %>">
|在相应的XML中还需要设置|
<servlet>
<servlet-name>FileUpServlet</servlet-name>
<servlet-class>org.svse.action.FileUpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</se