本文實例講述了JavaWeb實現圖形報表折線圖的方法。分享給大家供大家參考,具體如下:
步驟說明:
1. 導入log4j.jar,jfreechart-0.9.18.jar,jdom.jar,jcommon-0.9.3.jar四個jar包
2. 在某個包中寫入Tuxin.Java類
package com.mengya.util; import java.awt.Color; import java.awt.Font; import java.io.PrintWriter; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardLegend; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.LineAndShapeRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.DefaultCategoryDataset; public class Line { private DefaultCategoryDataset dataset = new DefaultCategoryDataset(); public void setValue(int sum, String line, String wfield) { dataset.addValue(sum, line, wfield); } public String generateLineChart(String title,String wfield, String hfield, HttpSession session, PrintWriter pw,int wPhoto,int hPhoto) { String filename = null; try { final JFreeChart chart = ChartFactory.createLineChart ( title, // 圖表標題 wfield, // 橫軸的顯示標簽 hfield, // 縱軸的顯示標簽 dataset, //數據集 PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 true, // 是否顯示圖例 true, // 是否生成提示工具 tooltips false // 是否生成URL鏈接 ); StandardLegend legend = (StandardLegend) chart.getLegend();//生成圖例 legend.setDisplaySeriesShapes(true);//顯示圖例形狀 legend.setShapeScaleX(1.5);//設置圖例X軸的大小 legend.setShapeScaleY(1.5);//設置圖例Y軸的大小 legend.setDisplaySeriesLines(true);//顯示圖示項的橫線 //設置圖片的背景色 chart.setBackgroundPaint(new java.awt.Color(189,235,255)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(new Color(239,251,255));//生成圖片中牆體的背景色 plot.setRangeGridlinePaint(Color.black);//生成圖片中格子線的顏色 // 沒有數據時顯示的消息 plot.setNoDataMessage("沒有相關統計數據"); plot.setNoDataMessageFont(new Font("黑體", Font.CENTER_BASELINE, 16)); plot.setNoDataMessagePaint(Color.RED); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//設置圖中的刻度線的單位 rangeAxis.setAutoRangeIncludesZero(true);//強制在自動選擇的數據范圍中包含0 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setDrawShapes(true);//折線的數據點根據分類使用不同的形狀 renderer.setItemLabelsVisible(true);//顯示每個點上的數據值的提示工具,數據標簽是否可見 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是圖片長度,300是圖片高度 filename = ServletUtilities.saveChartAsPNG(chart, wPhoto, hPhoto, info, session); ChartUtilities.writeImageMap(pw, filename, info); pw.flush(); } catch(Exception e) { e.printStackTrace(); } return filename; } }
3. 配置web.xml,在web.xml中添加如下內空:
<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>/DisplayChart</url-pattern> </servlet-mapping>
4. 在要顯示圖片的jsp中添加內容:
<%@ page language="java" import="java.util.*,com.mengya.util.Line,com.mengya.bean.StuBean" pageEncoding="gbk"%> <%@page import="java.io.PrintWriter;"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>折線圖</title> </head> <body> <% Line tt = new Line(); //第一條折線 List ar = (ArrayList) request.getAttribute("arr1"); for (int i = 0; i < ar.size(); i++) { StuBean bean = (StuBean) ar.get(i); tt.setValue((int) bean.getS_money(), "張明學個人消費比例圖", bean.getS_month()+ ""); } //條二條折線 ar = (ArrayList) request.getAttribute("arr2"); for (int i = 0; i < ar.size(); i++) { StuBean bean = (StuBean) ar.get(i); tt.setValue((int) bean.getS_money(), "夢娅個人消費比例圖", bean.getS_month()+ ""); } //說明:tt.setValue(int a,String b,String c) //其中的第二個參數b是該條折線的名字,每條折線的名字最好不要一樣. String filename = tt.generateLineChart("個人消費比例圖", "月 份", "費 用", session, new PrintWriter(out), 800, 550); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <P ALIGN="CENTER"> <img src="<%=graphURL %>" width=800 height=550 border=1 usemap="#<%=filename%>"> </body> </html>
希望本文所述對大家JSP程序設計有所幫助。