分类:
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
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;
%>