我思故我强

有热点的JfreeChart柱型图(原创)



实现方式:jsp+javabean
版本 :jfreechart-1.0.8
web-inf:设置
=====================================
<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文件:
=========================================================================
<%@ 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() {// 取数据

  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();// 获取绘图区

  plot.setBackgroundPaint(new Color(255, 255, 255)); // 设置绘图区背景色

  plot.setRangeGridlinePaint(Color.gray); // 设置水平方向背景线颜色

  plot.setRangeGridlinesVisible(true); // 设置是否显示水平方向背景线,默认值为True

  plot.setDomainGridlinePaint(Color.black); // 设置垂直方向背景线颜色

  // plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为False

  CategoryAxis domainAxis = plot.getDomainAxis();// 获取统计种类轴标题(X轴)

  plot.setDomainAxis(domainAxis);// 添加X轴

  BarRenderer3D renderer = new BarRenderer3D();// 获得BarRenderer3D类的实例,目的是设置柱形的绘制属性

  renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//

  renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("index2.jsp"));// 生成热点,用于链接

  renderer.setItemMargin(0.1);// 设置每个组所包含的平行柱的之间距离

  renderer.setSeriesPaint(0, Color.GREEN);// 设置柱子的颜色
  renderer.setSeriesPaint(1, Color.blue);// 设置柱子的颜色

  renderer.setBaseOutlinePaint(Color.BLACK);

  renderer.setWallPaint(Color.gray);// 设置 Wall 的颜色

  renderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值

  renderer.setBaseItemLabelFont(new Font("arial", Font.PLAIN, 10), true);// 设置柱形图上的文字

  renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// //显示每个柱的数值,并修改该数值的字体属性

  renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));

  renderer.setBaseItemLabelsVisible(true);

  renderer.setMaximumBarWidth(0.050000000000000003D);

  plot.setRenderer(renderer);

  plot.setForegroundAlpha(0.80f);// 设置柱的透明度

  // 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;

 }

}
=========================================================================


posted on 2007-12-04 09:57 李云泽 阅读(2604) 评论(2)  编辑  收藏 所属分类: Java代码JFreeChart

评论

# re: 有热点的JfreeChart柱型图(原创) 2008-04-07 12:55 qwb

能否把import com.mdcl.fso.homepage.*;这个包也一起提供一下啊  回复  更多评论   

# re: 有热点的JfreeChart柱型图(原创) 2008-05-09 16:58 assun

感谢版主提供的实例   回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: