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

阅读更多
实现方式:jsp+javabean
版本 :jfreechart-1.0.8
web-inf:设置
=====================================

  DisplayChart
  
   org.jfree.chart.servlet.DisplayChart
  
 
 
  DisplayChart
  /servlet/DisplayChart
 
======================================
jsp文件:
=========================================================================
<%@ page language="java" contentType="text/html; charset=GBK"
 pageEncoding="GBK"%>




Insert title here





<%
 String fileName = barchart01.drawPic(request.getSession(), out);
 String graphURL = request.getContextPath()
   + "/servlet/DisplayChart?filename=" + fileName;
%>


========================================================================= 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; } }
  • 有热点的JfreeChart柱型图(原创)[转]_第1张图片
  • 大小: 27.5 KB
  • 查看图片附件

你可能感兴趣的:(jfreechart,Servlet,JSP,SQL,UI)