今天用JFreeChart做曲线图来表示一个矿物局在一个月内的销售量.在开发的过程中遇到了不少问题.
首先我要说的是
求出当前年月..哪年哪月...
然后求出 这个月有多少天.(并不是所有月的天数是固定的,所以要根据实际情况求出月份天数)
//
年份
SimpleDateFormat nf
=
new
SimpleDateFormat(
"
yyyy
"
); java.util.Date nownf
=
new
java.util.Date() ; String currentyear
=
nf.format(nownf); System.
out
.println(currentyear);
int
year
=
Integer.parseInt(currentyear);
//
为了Calendar 才转化成int类型
//
月份
SimpleDateFormat df
=
new
SimpleDateFormat(
"
MM
"
); java.util.Date now
=
new
java.util.Date() ; String currentmonth
=
df.format(now); System.
out
.println(currentmonth);
int
month
=
Integer.parseInt(currentmonth);
//
为了Calendar 才转化成int类型
//
一个月有多少天数
Calendar cal
=
Calendar.getInstance(); cal.
set
(Calendar.YEAR,year); cal.
set
(Calendar.MONTH,month
-
1
);
//
Calendar.MONTH - 用来设置 MONTH 日历字段的值。Month 值是基于 0 的。例如,0 表示 1月份。
int
maxDate
=
cal.getActualMaximum(Calendar.DATE);
//
返回指定日历字段可能拥有的最大值
System.
out
.println(
"
maxDate=
"
+
maxDate);
int
minDate
=
cal.getActualMinimum(Calendar.DATE);
//
返回指定日历字段可能拥有的最小值
System.
out
.println(
"
minDate=
"
+
minDate);
然出开始做"曲线图"
org.jfree.data.time Class Month Month() Constructs a
new
Month, based on the current system time. Month(java.util.Date time) Constructs a
new
month instance, based on a date
/
time and the
default
time zone. Month(java.util.Date time, java.util.TimeZone zone) Constructs a Month, based on a date
/
time and a time zone. Month(
int
month,
int
year) Constructs a
new
month instance. Month(
int
month, Year year) Constructs a
new
month instance. org.jfree.data.time Class Day Day() Creates a
new
instance, derived from the system date
/
time (and assuming the
default
timezone). Day(java.util.Date time) Constructs a
new
instance, based on a particular date
/
time and the
default
time zone. Day(java.util.Date time, java.util.TimeZone zone) Constructs a
new
instance, based on a particular date
/
time and time zone. Day(
int
day,
int
month,
int
year) Constructs a
new
one day time period. Day(org.jfree.date.SerialDate serialDate) Constructs a
new
one day time period. org.jfree.data.time Class Minute Minute() Constructs a
new
Minute, based on the system date
/
time. Minute(java.util.Date time) Constructs a
new
Minute, based on the supplied date
/
time. Minute(java.util.Date time, java.util.TimeZone zone) Constructs a
new
Minute, based on the supplied date
/
time and timezone. Minute(
int
minute, Hour hour) Constructs a
new
Minute. Minute(
int
minute,
int
hour,
int
day,
int
month,
int
year) Creates a
new
minute. org.jfree.data.time Class Hour Hour() Constructs a
new
Hour, based on the system date
/
time. Hour(java.util.Date time) Constructs a
new
Hour, based on the supplied date
/
time. Hour(java.util.Date time, java.util.TimeZone zone) Constructs a
new
Hour, based on the supplied date
/
time evaluated in the specified time zone. Hour(
int
hour, Day day) Constructs a
new
Hour. Hour(
int
hour,
int
day,
int
month,
int
year) Creates a
new
hour.
例如:我自己的程序。
//
当前月份 当前年份(哈哈,month,year要int类型的,所以我在前面就类型转换了)
//
Month month1
=
new
Month(
int
month,
int
year); //Day day
=
new
Day(
1
,
11
,
2007
);
//
定义日期
//
Minute current
=
new
Minute(
2
,
14
,
14
,
11
,
2007
);
//
图形使用的时间
//
以上三个构造函数所要的参数都是int类型的哦
具体的代码如下:
ChartServlet1.java
package
com.test.chart;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.text.DateFormat;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
java.util.Date;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.jfree.chart.ChartFactory;
import
org.jfree.chart.ChartUtilities;
import
org.jfree.chart.JFreeChart;
import
org.jfree.chart.axis.DateAxis;
import
org.jfree.chart.axis.DateTickUnit;
import
org.jfree.chart.axis.ValueAxis;
import
org.jfree.chart.plot.XYPlot;
import
org.jfree.data.Range;
import
org.jfree.data.time.Day;
import
org.jfree.data.time.TimeSeries;
import
org.jfree.data.time.TimeSeriesCollection;
public
class
ChartServlet1
extends
HttpServlet
...
{ private static final String CONTENT_TYPE = " text/html; charset=GBK " ; // Initialize global variables public void init() throws ServletException ... { } // service public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ... { OutputStream out = response.getOutputStream(); // 定义输出流 try ... { JFreeChart chart = null ; // 创建图标工具 chart = createTimeSeriesChart(); // 产生折线图 if (chart != null ) ... { response.setContentType( " image/png " ); // 定义类型 ChartUtilities.writeChartAsPNG(out, chart, 600 , 300 ); // 输出 } } catch (Exception e) ... { System.err.println(e.toString()); } finally ... { out.close(); // 关闭流 } } private JFreeChart createTimeSeriesChart() ... { // 年份 SimpleDateFormat nf = new SimpleDateFormat( " yyyy " ); java.util.Date nownf = new java.util.Date() ; String currentyear = nf.format(nownf); System.out.println(currentyear); int year = Integer.parseInt(currentyear); // 月份 SimpleDateFormat df = new SimpleDateFormat( " MM " ); java.util.Date now = new java.util.Date() ; String currentmonth = df.format(now); System.out.println(currentmonth); int month = Integer.parseInt(currentmonth); // 一个月有多少天数 Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,year); cal.set(Calendar.MONTH,month - 1 ); // Calendar.MONTH - 用来设置 MONTH 日历字段的值。Month 值是基于 0 的。例如,0 表示 1月份。 int maxDate = cal.getActualMaximum(Calendar.DATE); // 返回指定日历字段可能拥有的最大值 System.out.println( " maxDate= " + maxDate); int minDate = cal.getActualMinimum(Calendar.DATE); // 返回指定日历字段可能拥有的最小值 System.out.println( " minDate= " + minDate); // TimeSeries series2 = new TimeSeries("实时状态图",Month.class); // 第一条折线 TimeSeries series = new TimeSeries( " 实时状态图 " ,Day. class ); // 第一条折线 Day day; // 添加测试数据 maxDate 就是当前月份最大的天数 // 最大值就是表示当前月有多少天 for ( int i = 1 ; i <= maxDate; i ++ ) ... { // day=new Day(1,11,2007); // 2007年11月1日开始 day = new Day(i,month, year); // 定义日期 series.add(day, Math.random() * 100 ); // 添加数据 System.out.println( " day= " + day); } TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); // 定义折线集合 timeseriescollection.addSeries(series); // 添加第一条曲线 // timeseriescollection.addSeries(series2); // 添加第二条曲线 JFreeChart chart = ChartFactory.createTimeSeriesChart( " xxx矿物局煤炭销售总量 " , " 日 " , " 销售量 " , timeseriescollection, true , true , false ); // Date valueOf(String s)将 JDBC 日期转义形式的字符串转换成 Date 值。 // public Date(int year, int month, int date, int hrs, int min) { // year - 减 1900 的年份。 // month - 0-11 之间的月份。 // date - 一月中 1-31 之间的某一天。 // hrs - 0-23 之间的小时数。 // min - 0-59 之间的分钟数。 int years = year - 1900 ; int months = month - 1 ; Date min = new Date(years, months, minDate); Date max = new Date(years, months, maxDate); XYPlot xyplot = chart.getXYPlot(); DateAxis dateAxis = (DateAxis) xyplot.getDomainAxis(); // 横坐标设置 // dateAxis.setMaximumDate(max); // 日期轴上的最大日期 // dateAxis.setMinimumDate(min); // 日期轴上的最小日期 dateAxis.setRange(min,max); // 日期轴上的最小日期,最大日期 // dateAxis.setTickLabelsVisible(true); // 坐标轴标尺值是否显示 dateAxis.setTickUnit( new DateTickUnit( 2 , 1 )); // 设置坐标轴标尺值显示间隔 public void setTickUnit(DateTickUnit unit) dateAxis.setDateFormatOverride( new SimpleDateFormat( " dd " )); // 日期轴日期标签的显示格式 只显示 日(号) // 纵坐标设定 ValueAxis valueaxis = null ; valueaxis = xyplot.getRangeAxis(); valueaxis.setRange( 0.0D , 200D); // 高度范围为 0 - 200 sp:销售量 return chart; } // Clean up resources public void destroy() ... { } }
显示的页面:
<%
@page contentType
=
"
text/html;charset=GB2312
"
language
=
"
java
"
%>
<
html
>
<
head
>
</
head
>
<
script type
=
"
text/javascript
"
>
var
req;
//
setInterval( "castVote()" , 3000 ) //每隔3秒调用一次
function
castVote(rank)
...
{ // This demo uses simple Html/JS only, so we load a static html page var url = " ii.jsp " ; //ii.jsp随便写什么东西 "hello" var callback = processAjaxResponse; executeXhr(callback, url); }
function
executeXhr(callback, url)
...
{ // branch for native XMLHttpRequest object if (window.XMLHttpRequest) ... { req = new XMLHttpRequest(); req.onreadystatechange = callback; req.open( " GET " , url, true ); req.send( null ); } // branch for IE/Windows ActiveX version else if (window.ActiveXObject) ... { req = new ActiveXObject( " Microsoft.XMLHTTP " ); if (req) ... { req.onreadystatechange = callback; req.open( " GET " , url, true ); req.send(); } } }
function
processAjaxResponse()
...
{ // only if req shows "loaded" if (req.readyState == 4 ) ... { // only if "OK" if (req.status == 200 ) ... { document.getElementById( ' votes ' ).innerHTML = req.responseText; } else ... { alert( " There was a problem retrieving the XML data: " + req.statusText); } } }
</
script
>
<
body
>
<
div id
=
"
votes
"
><
IMG src
=
'
myjfreecharttest
'
BORDER
=
1
WIDTH
=
600
HEIGHT
=
300
/></
div
>
</
body
>
</
html
>
web.xml 配置
<
servlet
>
<
servlet-name
>
myjfreechart
</
servlet-name
>
<
servlet-class
>
com.test.chart.ChartServlet1
</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
myjfreechart
</
servlet-name
>
<
url-pattern
>
/myjfreecharttest
</
url-pattern
>
</
servlet-mapping
>