jfreechart曲线图[转]

TimeSeriesDemo.java

import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;

/**
 * @author neitbe
 */
public class TimeSeriesDemo
{
 //private static Log log = LogFactory.getLog(TimeSeriesDemo.class);
 //曲线图标题
 private String title = "趋势分析";
 //曲线图X轴提示
 private String domain = "月份走势";
 //曲线图Y轴提示
 private String range = "应收余额";
 //曲线图自标题
 private String subtitleStr = "2005财年分析";
 //创建时间数据源
 //每一个TimeSeries在图上是一条曲线
 private TimeSeries yongyou = new TimeSeries("用友");
 private TimeSeries jindie = new TimeSeries("金碟");
 private TimeSeries dongruan = new TimeSeries("东软");
 //时间曲线数据集合
 private static TimeSeriesCollection dataset = new TimeSeriesCollection();
 private void setDataset()
 {
  dataset.removeAllSeries();        //在javaBean中使用此类这个很重要,不然会不停的加入新的TimeSeries;
  dataset.addSeries(this.yongyou);
  dataset.addSeries(this.jindie);
  dataset.addSeries(this.dongruan);
 }
 private void setDongruan()
 {
  this.dongruan.clear();     //很重要,不然会报错
  /**
   * 报错
   * org.apache.jasper.JasperException: You are attempting to add an observation for the time period
   * 这里是在类属性里声明TimeSeries才用到的
   */
  for(int i = 1999; i < 2005; i++)
  {
   for(int mon = 0; mon < 12; mon++)
   {

    this.dongruan.add(new TimeSeriesDataItem(new Day(1,mon+1,i),new Double(300 - Math.random()*100)));
   }
  }
 }

 private void setJindie()
 {

  this.jindie.clear();    //很重要,不然会报错
  for(int i = 1999; i < 2005; i++)
  {
   for(int mon = 0; mon < 12; mon++)
   {
    this.jindie.add(new TimeSeriesDataItem(new Day(1,mon+1,i),new Double(400 - Math.random()*100)));
   }
  }
 }

 private void setYongyou()
 {
  this.yongyou.clear();   //很重要,不然会报错
  for(int i = 1999; i < 2005; i++)
  {
   for(int mon = 0; mon < 12; mon++)
   {
    this.yongyou.add(new TimeSeriesDataItem(new Day(1,mon+1,i),new Double(500 - Math.random()*100)));
   }
  }
 }

 private TimeSeriesCollection getDataset()
 {
  this.setDongruan();
  this.setJindie();
  this.setYongyou();
  this.setDataset();
  return dataset;
 }

 public String drawPic(HttpSession session)
 {
  String fileName = null;
  JFreeChart chart = ChartFactory.createTimeSeriesChart(title,domain,range,getDataset(),true,true,false);
  TextTitle subTitle = new TextTitle(subtitleStr,new Font("黑体",Font.BOLD,12));
  chart.addSubtitle(subTitle);
  chart.setTitle(new TextTitle(title,new Font("隶书",Font.ITALIC,15)));
  chart.setBorderPaint(new GradientPaint(0,0,Color.white,0,1000,Color.blue));
  //String fileName = "D:\\fuit2.jpeg";
  try
  {
   ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
   //log.debug("准备创建图片……");
   fileName = ServletUtilities.saveChartAsJPEG(chart,600,600,info,session);
   //log.debug("图片创建成功!");
  } catch (IOException e)
  {
   //log.error("IO异常:",e);
  }
  //log.debug("返回的文件名为:" + fileName);
  return fileName;
 }

 public static void main(String[] args)
 {
  // TimeSeriesDemo timeSeriesDemo = new TimeSeriesDemo();
  // System.out.println(timeSeriesDemo.drawPic(null));
 }
}

---------------------------------------------------------------------------------------------------------------------------

TimeSeriesDemo.jsp

 <%@ page language="java" contentType="text/html; charset=UTF8" pageEncoding="UTF8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:useBean id="timeSeries" scope="session" class="jfreechart.TimeSeriesDemo"/>
</head>
<body>
<%
 String fileName = timeSeries.drawPic(request.getSession());
 String graphURL = request.getContextPath()+"/servlet/DisplayChart?filename=" + fileName;
%>
<img src="<%= graphURL %>" width="900" height="300" border="1"/>
</body>
</html>

----------------------------------------------------------------------------------------------------------------------------------

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee "
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<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>
</web-app>

你可能感兴趣的:(Web,xml,jsp,servlet,jfreechart)