锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
瀹炵幇鏂瑰紡錛歫sp+javabean
鐗堟湰 錛歫freechart-1.0.8
web-inf錛氳緗?br />=====================================
<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>/servlet/DisplayChart</url-pattern>
聽</servlet-mapping>
======================================
jsp鏂囦歡錛?br />=========================================================================
<%@ page language="java" contentType="text/html; charset=GBK"
聽pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
<script language="javascript">
function overlib(s){
聽document.all.ChartTip.style.display="block";
聽top.document.getElementById('ChartTip').innerHTML=s;
聽document.all.ChartTip.style.left=document.body.scrollLeft+event.x;
聽document.all.ChartTip.style.top=document.body.scrollTop+event.y-20;
}
function nd(s){
聽document.all.ChartTip.style.display="none";
}
</script>
<jsp:useBean id="barchart01" scope="session"
聽class="com.mdcl.fso.homepage.chart.BarChart01" />
</head>
<body>
<%
聽String fileName = barchart01.drawPic(request.getSession(), out);
聽String graphURL = request.getContextPath()
聽聽聽+ "/servlet/DisplayChart?filename=" + fileName;
%>
<br />
<img src="<%= graphURL %>" border="0" usemap="#<%=fileName %>" />
<br />
<div id="ChartTip"
聽style="position:absolute; font-family:'瀹嬩綋'; font-size: 12px;line-height: 20px;background-color:#FFFFEC; border: 1px solid #999999;display:none;left:0px;top:1px;">嫻嬭瘯鏄劇ず</div>
</body>
</html>
=========================================================================
java鏂囦歡
=========================================================================
package com.mdcl.fso.homepage.chart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.labels.*;
import org.jfree.ui.*;
import java.util.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import com.mdcl.fso.homepage.*;
public class BarChart01 {
聽private static DefaultCategoryDataset dataset = new DefaultCategoryDataset();
聽private DefaultCategoryDataset getDataset() {// 鍙栨暟鎹?/p>
聽聽List expense = new ArrayList();
聽聽String sql = "select ftr.organ_id ,fs.dept_name,ftr.subject_id,fft.finance_type_name,ftr.rpt_date ,ftr.rpt_data "
聽聽聽聽+ "from fso_t_rpt_expense ftr,fso_sdept fs,fso_finance_type fft "
聽聽聽聽+ "where ftr.type_id='D' "
聽聽聽聽+ "and ftr.organ_id=fs.dept_id聽 "
聽聽聽聽+ "and ftr.rpt_date='2007-08' "
聽聽聽聽+ "and fft.finance_type_id=ftr.subject_id聽 "
聽聽聽聽+ "and ftr.organ_id='1140'";
聽聽expense = DBDao.DbQueryExpense(sql);
聽聽for (int i = 0; i < expense.size(); i++) {
聽聽聽HashMap param = new HashMap();
聽聽聽param = (HashMap) expense.get(i);
聽聽聽double dt = Double.parseDouble(String.valueOf(param.get("rpt_data")));
聽聽聽String a = (String) param.get("organ");
聽聽聽String b = (String) param.get("subject");
聽聽聽dataset.addValue(dt, a, b);
聽聽}
聽聽return dataset;
聽}
聽public String drawPic(HttpSession session, JspWriter out) {
聽聽String fileName = null;
聽聽JFreeChart chart = ChartFactory.createBarChart3D("2007-08鎴愭湰緇熻", "璐圭敤綾誨瀷",
聽聽聽聽"閲戦錛堝崟浣嶏細鍏冿級", getDataset(), PlotOrientation.VERTICAL, true, true,
聽聽聽聽true);
聽聽chart.setBackgroundPaint(Color.WHITE);
聽聽CategoryPlot plot = chart.getCategoryPlot();// 鑾峰彇緇樺浘鍖?/p>
聽聽plot.setBackgroundPaint(new Color(255, 255, 255)); // 璁劇疆緇樺浘鍖鴻儗鏅壊
聽聽plot.setRangeGridlinePaint(Color.gray); // 璁劇疆姘村鉤鏂瑰悜鑳屾櫙綰塊鑹?/p>
聽聽plot.setRangeGridlinesVisible(true); // 璁劇疆鏄惁鏄劇ず姘村鉤鏂瑰悜鑳屾櫙綰?榛樿鍊間負True
聽聽plot.setDomainGridlinePaint(Color.black); // 璁劇疆鍨傜洿鏂瑰悜鑳屾櫙綰塊鑹?/p>
聽聽// plot.setDomainGridlinesVisible(true); // 璁劇疆鏄惁鏄劇ず鍨傜洿鏂瑰悜鑳屾櫙綰?榛樿鍊間負False
聽聽CategoryAxis domainAxis = plot.getDomainAxis();// 鑾峰彇緇熻縐嶇被杞存爣棰橈紙X杞達級
聽聽plot.setDomainAxis(domainAxis);// 娣誨姞X杞?/p>
聽聽BarRenderer3D renderer = new BarRenderer3D();// 鑾峰緱BarRenderer3D綾葷殑瀹炰緥錛岀洰鐨勬槸璁劇疆鏌卞艦鐨勭粯鍒跺睘鎬?/p>
聽聽renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//
聽聽renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("index2.jsp"));// 鐢熸垚鐑偣,鐢ㄤ簬閾炬帴
聽聽renderer.setItemMargin(0.1);// 璁劇疆姣忎釜緇勬墍鍖呭惈鐨勫鉤琛屾煴鐨勪箣闂磋窛紱?/p>
聽聽renderer.setSeriesPaint(0, Color.GREEN);// 璁劇疆鏌卞瓙鐨勯鑹?br />聽聽renderer.setSeriesPaint(1, Color.blue);// 璁劇疆鏌卞瓙鐨勯鑹?/p>
聽聽renderer.setBaseOutlinePaint(Color.BLACK);
聽聽renderer.setWallPaint(Color.gray);// 璁劇疆 Wall 鐨勯鑹?/p>
聽聽renderer.setItemLabelAnchorOffset(10D);// 璁劇疆鏌卞艦鍥句笂鐨勬枃瀛楀亸紱誨?/p>
聽聽renderer.setBaseItemLabelFont(new Font("arial", Font.PLAIN, 10), true);// 璁劇疆鏌卞艦鍥句笂鐨勬枃瀛?/p>
聽聽renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// //鏄劇ず姣忎釜鏌辯殑鏁板鹼紝騫朵慨鏀硅鏁板肩殑瀛椾綋灞炴?/p>
聽聽renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
聽聽renderer.setBaseItemLabelsVisible(true);
聽聽renderer.setMaximumBarWidth(0.050000000000000003D);
聽聽plot.setRenderer(renderer);
聽聽plot.setForegroundAlpha(0.80f);// 璁劇疆鏌辯殑閫忔槑搴?/p>
聽聽// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);//璁劇疆鏄劇ず浣嶇疆
聽聽plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);// 璁劇疆鏄劇ず浣嶇疆
聽聽try {
聽聽聽PrintWriter pw = new PrintWriter(out);
聽聽聽StandardEntityCollection sec = new StandardEntityCollection();
聽聽聽ChartRenderingInfo info = new ChartRenderingInfo(sec);
聽聽聽fileName = ServletUtilities.saveChartAsPNG(chart, 640, 400, info,
聽聽聽聽聽session);
聽聽聽// ChartUtilities.writeChartAsPNG(op,chart, 640, 400, info,true,0);
聽聽聽ChartUtilities.writeImageMap(pw, fileName, info, true);
聽聽} catch (IOException e) {
聽聽聽// TODO Auto-generated catch block
聽聽聽e.printStackTrace();
聽聽}
聽聽return fileName;
聽}
}
=========================================================================