Chinaunix首页 | 论坛 | 博客
  • 博客访问: 502927
  • 博文数量: 102
  • 博客积分: 4001
  • 博客等级: 上校
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-21 16:01
文章分类

全部博文(102)

文章存档

2011年(1)

2010年(1)

2009年(56)

2008年(44)

我的朋友

分类:

2009-05-07 22:16:14

今天心血來潮研究了一下統計圖表的寫法,步驟如下

1.系統建立(linux + apache tomcate 6.0 或 linux + jboss4.0 或 windows  + apache tomcate 6.0 或 windows + jboss4.0)

下載JFreeChart套件,將jar檔放置於webapp 的/WEB-INF/lib/

2.設定web.xml


           DisplayChart
           org.jfree.chart.servlet.DisplayChart


           DisplayChart
           /servlet/DisplayChart

3.範例檔demo.jsp(當然要先用mysql建立一數據庫)

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ 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.Year,
              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.*"%>

<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test?user=root&password=123456";
Connection conn = DriverManager.getConnection(url);
conn.setCatalog("test");
Statement s=conn.createStatement();
ResultSet rs=s.executeQuery("select * from grade where s_id=’s0001’");
Statement s1=conn.createStatement();
ResultSet rs1=s1.executeQuery("select SUM(code) AS tcode from grade where s_id=’s0001’");
int totalRecord=0;
if(rs1.next())  
  {  
        totalRecord   =   rs1.getInt(1);  
  }

Statement s2=conn.createStatement();
ResultSet rs2=s2.executeQuery("select * from grade where s_id=’s0001’");
int RowCount = 0;
rs2.last();
RowCount = rs2.getRow();

TimeSeries timeSeries = new TimeSeries("", Year.class);

TimeSeriesCollection lineDataset = new TimeSeriesCollection();

int i=2001;
while(rs.next()){
          String code = rs.getString("code");
              int grade = Integer.parseInt( code );

          timeSeries.add(new Year(i), grade); 
          i=i+1; 
         
         }
 
lineDataset.addSeries(timeSeries);
JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "分數", lineDataset, true, false, true);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
DateAxis   axis   =   (DateAxis) plot.getDomainAxis();  
axis.setDateFormatOverride(new SimpleDateFormat("yyy"));
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
xylineandshaperenderer.setBaseShapesVisible(true);
XYItemRenderer xyitem = plot.getRenderer();  
xyitem.setBaseItemLabelsVisible(true);  
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 12));
plot.setRenderer(xyitem);

String tcountstr="學生學習成長曲線圖(總施測次數"+ RowCount + "次)(平均分數:" + totalRecord/RowCount + "分)";

TextTitle subtitle = new TextTitle(tcountstr, new Font("", Font.BOLD, 16));
chart.addSubtitle(subtitle);
 
String filename = ServletUtilities.saveChartAsPNG(chart, 600, 450, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>


阅读(2707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~