JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 JFreeChart 的TimeSeriesChart的問題
sasayaman





發文: 11
積分: 0
於 2009-03-20 18:50 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大家好,目前小弟是把資料庫中的資料取出,然後用JFreeChart在JSP上呈現,但目前遇到一個問題,因為是要比較這個月跟前一個月數字上的差異,所以日期上不能一致,因為這個月的日期還沒過完,是不是日期上有不一致就會導致我的圖變成下面這樣呢? 我也有嘗試把日期取出來判斷是否要加入那個timeseries中,或是分兩個rs各去撈資料,但似乎也沒什麼效果,請告知該如何做或是往那方面去找資料呢?
感謝大家
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<%@ page language="java" contentType="text/html; charset=BIG5" pageEncoding="BIG5"%>
<%@ page import="java.sql.*,java.lang.*,java.text.SimpleDateFormat,org.jfree.chart.ChartFactory,
org.jfree.chart.title.TextTitle,org.jfree.data.time.TimeSeries,org.jfree.data.time.*,
org.jfree.data.time.TimeSeriesCollection,org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.CategoryLabelPositions, org.jfree.chart.axis.DateAxis,
org.jfree.chart.axis.DateTickMarkPosition,org.jfree.chart.axis.DateTickUnit,
org.jfree.chart.axis.NumberAxis,org.jfree.chart.axis.NumberTickUnit,org.jfree.chart.axis.ValueAxis,
org.jfree.chart.plot.XYPlot,org.jfree.chart.renderer.xy.XYLineAndShapeRenderer,
java.awt.Color,org.jfree.ui.RectangleInsets,java.awt.Font,
org.jfree.chart.renderer.xy.XYItemRenderer,org.jfree.chart.JFreeChart,
org.jfree.chart.servlet.ServletUtilities,org.jfree.chart.labels.*,
org.jfree.ui.*,org.jfree.data.xy.XYDataset,org.jfree.chart.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>UOIchart</title>
</head>
<body>
 
<%
//連接資料庫
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("aa","a1","123");
Statement stmt2 = con.createStatement();
ResultSet rs = stmt2.executeQuery("SELECT revenue_yyyymm,
revenue_date,SUM (revenue_amount)revenue_total"+" FROM 
(SELECT bill_date,CASE WHEN (bill_date <= '01-2月-09' or bill_date is null) 
THEN '20090201' ELSE to_char(bill_date,'yyyymmdd')"+
"END AS revenue_date,revenue_yyyymm,revenue_amount 
FROM UOI_TB_RV_REVENUE_DTL where revenue_yyyymm='200902') 
GROUP BY   revenue_yyyymm, revenue_date union 
SELECT revenue_yyyymm,revenue_date,SUM (revenue_amount)revenue_total"
+" FROM (SELECT bill_date,CASE WHEN (bill_date <= '01-3月-09' or bill_date is null) 
THEN '20090301' ELSE to_char(bill_date,'yyyymmdd')"+"END AS revenue_date,
revenue_yyyymm,revenue_amount FROM UOI_TB_RV_REVENUE_DTL where revenue_yyyymm='200903') 
GROUP BY revenue_yyyymm, revenue_date");
 
//設曲線
TimeSeries timeseries=new TimeSeries("200902",Day.class);
TimeSeries timeseries2=new TimeSeries("200903",Day.class);
//從資料庫取值
while(rs.next())
{
    String ev_yyyymm=rs.getString(1);
    int year=Integer.parseInt(ev_yyyymm);
   
    
    if( year==200902)
    {
      double total=rs.getDouble(3);
      String ev_date=rs.getString(2);
      int x=Integer.parseInt(ev_date.substring(0,4));
      int y=Integer.parseInt(ev_date.substring(4,6));
      int z=Integer.parseInt(ev_date.substring(6,8));
       timeseries.add(new Day(z,y,x),total);
    }
    else
    {
      double total2=rs.getDouble(3);
      String ev_date=rs.getString(2);
      int a=Integer.parseInt(ev_date.substring(0,4));
      int b=Integer.parseInt(ev_date.substring(4,6));
      int c=Integer.parseInt(ev_date.substring(6,8));
      
      timeseries2.add(new Day(c,b,a),total2);
    }
  
 
   
}
//連接曲線
TimeSeriesCollection dataset=new TimeSeriesCollection();
dataset.addSeries(timeseries);
dataset.addSeries(timeseries2);
 
//設置曲線圖
XYDataset xydataset = (XYDataset) dataset;
JFreeChart chart = ChartFactory.createTimeSeriesChart("ABC","DEF","GHI",xydataset,true,true,false);
 
chart.setBackgroundPaint(Color.white);//設置曲線背景色
 
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd"));
 
plot.setBackgroundPaint(Color.white);//設置網格背景色
plot.setDomainGridlinePaint(Color.pink);// 設置網格直線色
plot.setRangeGridlinePaint(Color.pink);//設置網格橫現色
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
xylineandshaperenderer.setBaseShapesVisible(true);//設置曲線圖是否顯示數據點
 
//設置曲線圖是否顯示數據點上的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 8));
plot.setRenderer(xyitem);
 
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 600, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
 
%>
 
<img src="<%= graphURL %>" border=0 usemap="#<%= filename %>">
 
</body>
</html>




sasayaman edited on 2009-03-20 18:58
reply to postreply to post
作者 Re:JFreeChart 的TimeSeriesChart的問題 [Re:sasayaman]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2009-03-21 01:10 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這張圖是怎麼樣不對?
請說明你想要以甚麼方式呈現線圖。


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:JFreeChart 的TimeSeriesChart的問題 [Re:sasayaman]
sasayaman





發文: 11
積分: 0
於 2009-03-23 10:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思 問題描述不清楚,因為我是要本月與前一個月比較,例如二月與三月做比較,但是二月的天數已經有28天了,三月到目前為止只有23天,我想要在同一個日期上有二月與三月兩個點,但我做出來的卻是分開兩邊的日期(紅的是二月 藍的是三月),所以想問問該如何處理呢?謝謝!

reply to postreply to post
作者 Re:JFreeChart 的TimeSeriesChart的問題 [Re:sasayaman]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2009-03-23 13:09 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sasayaman wrote:
不好意思 問題描述不清楚,因為我是要本月與前一個月比較,例如二月與三月做比較,但是二月的天數已經有28天了,三月到目前為止只有23天,我想要在同一個日期上有二月與三月兩個點,但我做出來的卻是分開兩邊的日期(紅的是二月 藍的是三月),所以想問問該如何處理呢?謝謝!


兩條線會分開是因為兩個數據的 domain 的範圍是分開的:一個數據是在二月,一個數據是在三月,兩者所包含的日期在 x 軸上是不同的。

比較簡單的作法是你使用 XY line chart 來呈現數據,以 year/month/day 中的 day 當作 x 軸(domain) 的值。


reply to postreply to post

給我
辣味豆腐 其餘免談
» JWorld@TW »  Java Tools

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8